xref: /aosp_15_r20/external/federated-compute/fcp/client/opstats/opstats_utils_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fcp/client/opstats/opstats_utils.h"
18 
19 #include <utility>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "fcp/client/test_helpers.h"
24 
25 namespace fcp {
26 namespace client {
27 namespace opstats {
28 namespace {
29 
30 constexpr char kTaskName[] = "task";
31 OperationalStats::Event::EventKind kUploadStartedEvent =
32     OperationalStats::Event::EVENT_KIND_RESULT_UPLOAD_STARTED;
33 OperationalStats::Event::EventKind kUploadServerAbortedEvent =
34     OperationalStats::Event::EVENT_KIND_RESULT_UPLOAD_SERVER_ABORTED;
35 
CreateEvent(OperationalStats::Event::EventKind event_kind,int64_t event_time_seconds)36 OperationalStats::Event CreateEvent(
37     OperationalStats::Event::EventKind event_kind, int64_t event_time_seconds) {
38   OperationalStats::Event event;
39   event.set_event_type(event_kind);
40   google::protobuf::Timestamp t;
41   t.set_seconds(event_time_seconds);
42   *event.mutable_timestamp() = t;
43   return event;
44 }
45 
TEST(OpStatsUtils,GetLastSuccessfulContributionTimeReturnsUploadStartedTimestamp)46 TEST(OpStatsUtils,
47      GetLastSuccessfulContributionTimeReturnsUploadStartedTimestamp) {
48   OperationalStats stats;
49   stats.set_task_name(kTaskName);
50 
51   int64_t upload_started_time_sec = 1000;
52   stats.mutable_events()->Add(
53       CreateEvent(kUploadStartedEvent, upload_started_time_sec));
54 
55   OpStatsSequence opstats_sequence;
56   *opstats_sequence.add_opstats() = std::move(stats);
57 
58   auto last_time =
59       GetLastSuccessfulContributionTime(opstats_sequence, kTaskName);
60   EXPECT_EQ(last_time->seconds(), upload_started_time_sec);
61 }
62 
TEST(OpStatsUtils,GetLastSuccessfulContributionTimeReturnNotFoundForUnknownTask)63 TEST(OpStatsUtils,
64      GetLastSuccessfulContributionTimeReturnNotFoundForUnknownTask) {
65   OperationalStats stats;
66   stats.set_task_name(kTaskName);
67 
68   int64_t upload_started_time_sec = 1000;
69   stats.mutable_events()->Add(
70       CreateEvent(kUploadStartedEvent, upload_started_time_sec));
71 
72   OpStatsSequence opstats_sequence;
73   *opstats_sequence.add_opstats() = std::move(stats);
74   EXPECT_FALSE(
75       GetLastSuccessfulContributionTime(opstats_sequence, "task_name_not_found")
76           .has_value());
77 }
78 
TEST(OpStatsUtils,GetLastSuccessfulContributionTimeReturnsMostRecentUploadStartedTimestamp)79 TEST(OpStatsUtils,
80      GetLastSuccessfulContributionTimeReturnsMostRecentUploadStartedTimestamp) {
81   OpStatsSequence opstats_sequence;
82 
83   OperationalStats old_stats;
84   old_stats.set_task_name(kTaskName);
85   old_stats.mutable_events()->Add(CreateEvent(kUploadStartedEvent, 1000));
86   *opstats_sequence.add_opstats() = std::move(old_stats);
87 
88   OperationalStats new_stats;
89   new_stats.set_task_name(kTaskName);
90   int64_t new_upload_started_sec = 2000;
91   new_stats.mutable_events()->Add(
92       CreateEvent(kUploadStartedEvent, new_upload_started_sec));
93   *opstats_sequence.add_opstats() = std::move(new_stats);
94 
95   auto last_time =
96       GetLastSuccessfulContributionTime(opstats_sequence, kTaskName);
97   EXPECT_EQ(last_time->seconds(), new_upload_started_sec);
98 }
99 
TEST(OpStatsUtils,GetLastSuccessfulContributionTimeReturnNotFoundIfAbortedByServer)100 TEST(OpStatsUtils,
101      GetLastSuccessfulContributionTimeReturnNotFoundIfAbortedByServer) {
102   OperationalStats stats;
103   stats.set_task_name(kTaskName);
104   stats.mutable_events()->Add(CreateEvent(kUploadStartedEvent, 1000));
105   stats.mutable_events()->Add(CreateEvent(kUploadServerAbortedEvent, 1001));
106 
107   OpStatsSequence opstats_sequence;
108   *opstats_sequence.add_opstats() = std::move(stats);
109 
110   EXPECT_FALSE(GetLastSuccessfulContributionTime(opstats_sequence, kTaskName)
111                    .has_value());
112 }
113 
TEST(OpStatsUtils,GetLastSuccessfulContributionTimeReturnOlderIfNewerAbortedByServer)114 TEST(OpStatsUtils,
115      GetLastSuccessfulContributionTimeReturnOlderIfNewerAbortedByServer) {
116   OpStatsSequence opstats_sequence;
117 
118   OperationalStats old_stats;
119   old_stats.set_task_name(kTaskName);
120   int64_t expected_time_sec = 1000;
121   old_stats.mutable_events()->Add(
122       CreateEvent(kUploadStartedEvent, expected_time_sec));
123   *opstats_sequence.add_opstats() = std::move(old_stats);
124 
125   OperationalStats new_stats;
126   new_stats.set_task_name(kTaskName);
127   new_stats.mutable_events()->Add(CreateEvent(
128       OperationalStats::Event::EVENT_KIND_RESULT_UPLOAD_STARTED, 2000));
129   new_stats.mutable_events()->Add(CreateEvent(
130       OperationalStats::Event::EVENT_KIND_RESULT_UPLOAD_SERVER_ABORTED, 2001));
131   *opstats_sequence.add_opstats() = std::move(new_stats);
132 
133   auto last_time =
134       GetLastSuccessfulContributionTime(opstats_sequence, kTaskName);
135   EXPECT_EQ(last_time->seconds(), expected_time_sec);
136 }
137 
TEST(OpStatsUtils,GetLastSuccessfulContributionTimeReturnNotFoundIfNoUploadStarted)138 TEST(OpStatsUtils,
139      GetLastSuccessfulContributionTimeReturnNotFoundIfNoUploadStarted) {
140   OperationalStats stats;
141   stats.set_task_name(kTaskName);
142   stats.mutable_events()->Add(
143       CreateEvent(OperationalStats::Event::EVENT_KIND_CHECKIN_STARTED, 1000));
144 
145   OpStatsSequence opstats_sequence;
146   *opstats_sequence.add_opstats() = std::move(stats);
147   EXPECT_FALSE(GetLastSuccessfulContributionTime(opstats_sequence, kTaskName)
148                    .has_value());
149 }
150 
151 }  // namespace
152 }  // namespace opstats
153 }  // namespace client
154 }  // namespace fcp
155