1 /*
2 * Copyright 2020 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 #ifndef FCP_CLIENT_TESTING_UTILS_H_
18 #define FCP_CLIENT_TESTING_UTILS_H_
19
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include "google/protobuf/repeated_field.h"
26 #include "absl/status/status.h"
27 #include "absl/status/statusor.h"
28 #include "absl/strings/str_cat.h"
29 #include "absl/strings/string_view.h"
30 #include "absl/time/time.h"
31 #include "fcp/base/monitoring.h"
32 #include "fcp/base/platform.h"
33 #include "fcp/client/engine/engine.pb.h"
34 #include "fcp/client/files.h"
35 #include "fcp/client/simple_task_environment.h"
36 #include "fcp/protos/federated_api.pb.h"
37 #include "fcp/protos/plan.pb.h"
38
39 namespace fcp::client::testing {
40
41 using google::internal::federated::plan::Dataset;
42 using google::internal::federated::plan::ExampleSelector;
43 using google::internal::federated::plan::Plan;
44 using google::internal::federatedml::v2::RetryWindow;
45
MakeTestFileName(absl::string_view dir,absl::string_view prefix,absl::string_view suffix)46 inline std::string MakeTestFileName(absl::string_view dir,
47 absl::string_view prefix,
48 absl::string_view suffix) {
49 return ConcatPath(StripTrailingPathSeparator(dir),
50 absl::StrCat(prefix, suffix));
51 }
52
53 // Basic implementation of ExampleIterator for testing purposes.
54 // It iterates over examples from a given dataset.
55 class TestExampleIterator : public ExampleIterator {
56 public:
TestExampleIterator(const Dataset::ClientDataset * dataset)57 explicit TestExampleIterator(const Dataset::ClientDataset* dataset)
58 : next_example_(dataset->example().begin()),
59 end_(dataset->example().end()) {}
60
Next()61 absl::StatusOr<std::string> Next() override {
62 if (next_example_ == end_) {
63 return absl::OutOfRangeError("");
64 }
65 return *(next_example_++);
66 }
67
Close()68 void Close() override {}
69
70 private:
71 google::protobuf::RepeatedPtrField<std::string>::const_iterator next_example_;
72 google::protobuf::RepeatedPtrField<std::string>::const_iterator end_;
73 };
74
75 // Implementation of TaskEnvironment, the interface by which the client plan
76 // engine interacts with the environment, that allows tests to provide a dataset
77 // as input and consume the output checkpoint.
78 class TestTaskEnvironment : public SimpleTaskEnvironment {
79 public:
TestTaskEnvironment(const Dataset::ClientDataset * dataset,const std::string & base_dir)80 explicit TestTaskEnvironment(const Dataset::ClientDataset* dataset,
81 const std::string& base_dir)
82 : dataset_(dataset), base_dir_(base_dir) {}
83
CreateExampleIterator(const google::internal::federated::plan::ExampleSelector & example_selector)84 absl::StatusOr<std::unique_ptr<ExampleIterator>> CreateExampleIterator(
85 const google::internal::federated::plan::ExampleSelector&
86 example_selector) override {
87 SelectorContext unused;
88 return CreateExampleIterator(example_selector, unused);
89 }
90
CreateExampleIterator(const ExampleSelector & example_selector,const SelectorContext & selector_context)91 absl::StatusOr<std::unique_ptr<ExampleIterator>> CreateExampleIterator(
92 const ExampleSelector& example_selector,
93 const SelectorContext& selector_context) override {
94 std::unique_ptr<ExampleIterator> iter =
95 std::make_unique<TestExampleIterator>(dataset_);
96 return std::move(iter);
97 }
98
GetBaseDir()99 std::string GetBaseDir() override { return base_dir_; }
100
GetCacheDir()101 std::string GetCacheDir() override { return base_dir_; }
102
103 private:
TrainingConditionsSatisfied()104 bool TrainingConditionsSatisfied() override { return true; }
105
106 const Dataset::ClientDataset* dataset_;
107 std::string base_dir_;
108 std::string checkpoint_file_;
109 };
110
111 // Implementation of client file API that creates files in a temporary test
112 // directory.
113 class TestFiles : public Files {
114 public:
TestFiles(absl::string_view test_dir)115 explicit TestFiles(absl::string_view test_dir) : test_dir_(test_dir) {}
CreateTempFile(const std::string & prefix,const std::string & suffix)116 absl::StatusOr<std::string> CreateTempFile(
117 const std::string& prefix, const std::string& suffix) override {
118 return MakeTestFileName(test_dir_, prefix, suffix);
119 }
120
121 private:
122 std::string test_dir_;
123 };
124
125 } // namespace fcp::client::testing
126
127 #endif // FCP_CLIENT_TESTING_UTILS_H_
128