xref: /aosp_15_r20/external/federated-compute/fcp/client/simple_task_environment_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "fcp/client/simple_task_environment.h"
16 
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include "absl/time/clock.h"
20 #include "absl/time/time.h"
21 #include "fcp/client/test_helpers.h"
22 #include "fcp/testing/testing.h"
23 
24 namespace fcp {
25 namespace client {
26 namespace {
27 
28 using ::testing::Return;
29 using ::testing::StrictMock;
30 
TEST(SimpleTaskEnvironmentTest,TestShouldAbort)31 TEST(SimpleTaskEnvironmentTest, TestShouldAbort) {
32   StrictMock<MockSimpleTaskEnvironment> mock_task_env;
33   EXPECT_CALL(mock_task_env, TrainingConditionsSatisfied())
34       .WillOnce(Return(false));
35   bool result = mock_task_env.ShouldAbort(
36       /*current_time=*/absl::Now(),
37       /*condition_polling_period=*/absl::ZeroDuration());
38   EXPECT_TRUE(result);
39 }
40 
41 // Assert that with a zero condition_polling_period, no throttling in
42 // ShouldAbort takes place, by emulating two calls at the same time.
43 // Both calls should return the mock's value.
TEST(SimpleTaskEnvironmentTest,TestShouldAbortNoThrottling)44 TEST(SimpleTaskEnvironmentTest, TestShouldAbortNoThrottling) {
45   StrictMock<MockSimpleTaskEnvironment> mock_task_env;
46   absl::Time now = absl::Now();
47   EXPECT_CALL(mock_task_env, TrainingConditionsSatisfied())
48       .WillRepeatedly(Return(false));
49   bool result = mock_task_env.ShouldAbort(
50       /*current_time=*/now,
51       /*condition_polling_period=*/absl::ZeroDuration());
52   EXPECT_TRUE(result);
53   result = mock_task_env.ShouldAbort(
54       /*current_time=*/now,
55       /*condition_polling_period=*/absl::ZeroDuration());
56   EXPECT_TRUE(result);
57 }
58 
59 // Verify ShouldAbort throttling for non-zero polling periods.
TEST(SimpleTaskEnvironmentTest,TestShouldAbortThrottling)60 TEST(SimpleTaskEnvironmentTest, TestShouldAbortThrottling) {
61   StrictMock<MockSimpleTaskEnvironment> mock_task_env;
62   EXPECT_CALL(mock_task_env, TrainingConditionsSatisfied())
63       .WillRepeatedly(Return(false));
64   absl::Time now = absl::Now();
65   // First call should be non-throttled (since it assumes last call happened at
66   // UnixEpoch. Second call after 1s will be throttled because polling period is
67   // 1.5s; third call (after 2s) will be non-throttled again.
68   bool result = mock_task_env.ShouldAbort(
69       /*current_time=*/now,
70       /*condition_polling_period=*/absl::Milliseconds(1500));
71   EXPECT_TRUE(result);
72   result = mock_task_env.ShouldAbort(
73       /*current_time=*/now + absl::Seconds(1),
74       /*condition_polling_period=*/absl::Milliseconds(1500));
75   EXPECT_FALSE(result);
76   result = mock_task_env.ShouldAbort(
77       /*current_time=*/now + absl::Seconds(2),
78       /*condition_polling_period=*/absl::Milliseconds(1500));
79   EXPECT_TRUE(result);
80 }
81 
82 }  // anonymous namespace
83 }  // namespace client
84 }  // namespace fcp
85