TensorFlow Serving C++ API Documentation
mock_server_core.h
1 /* Copyright 2016 Google Inc. All Rights Reserved.
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 
16 // GMock implementation of ServerCore.
17 #ifndef TENSORFLOW_SERVING_MODEL_SERVERS_TEST_UTIL_MOCK_SERVER_CORE_H_
18 #define TENSORFLOW_SERVING_MODEL_SERVERS_TEST_UTIL_MOCK_SERVER_CORE_H_
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "google/protobuf/any.pb.h"
25 #include "google/protobuf/map.h"
26 #include <gmock/gmock.h>
27 #include "absl/status/status.h"
28 #include "tensorflow/core/lib/core/status.h"
29 #include "tensorflow/core/platform/logging.h"
30 #include "tensorflow_serving/apis/logging.proto.h"
31 #include "tensorflow_serving/apis/model.pb.h"
32 #include "tensorflow_serving/config/model_server_config.pb.h"
33 #include "tensorflow_serving/config/platform_config.pb.h"
34 #include "tensorflow_serving/core/aspired_versions_manager.h"
35 #include "tensorflow_serving/core/servable_handle.h"
36 #include "tensorflow_serving/core/servable_id.h"
37 #include "tensorflow_serving/core/servable_state.h"
38 #include "tensorflow_serving/core/servable_state_monitor.h"
39 #include "tensorflow_serving/core/server_request_logger.h"
40 #include "tensorflow_serving/core/test_util/fake_loader_source_adapter.pb.h"
41 #include "tensorflow_serving/core/test_util/servable_handle_test_util.h"
42 #include "tensorflow_serving/model_servers/server_core.h"
43 #include "tensorflow_serving/servables/tensorflow/servable.h"
44 #include "tensorflow_serving/util/event_bus.h"
45 #include "tensorflow_serving/util/unique_ptr_with_deps.h"
46 
47 namespace tensorflow {
48 namespace serving {
49 namespace test_util {
50 
51 class MockServerCore : public ServerCore {
52  public:
53  // Creates a PlatformConfigMap with a FakeLoaderSourceAdapterConfig.
54  static PlatformConfigMap CreateFakeLoaderPlatformConfigMap() {
55  ::google::protobuf::Any source_adapter_config;
56  source_adapter_config.PackFrom(test_util::FakeLoaderSourceAdapterConfig());
57  PlatformConfigMap platform_config_map;
58  (*(*platform_config_map.mutable_platform_configs())["fake_servable"]
59  .mutable_source_adapter_config()) = source_adapter_config;
60  return platform_config_map;
61  }
62 
63  static Options GetOptions(
64  const PlatformConfigMap& platform_config_map,
65  std::unique_ptr<ServerRequestLogger> server_request_logger) {
66  Options options;
67  options.platform_config_map = platform_config_map;
68  options.servable_state_monitor_creator =
69  [](EventBus<ServableState>* event_bus,
70  std::unique_ptr<ServableStateMonitor>* monitor) -> Status {
71  monitor->reset(new ServableStateMonitor(event_bus));
72  return Status();
73  };
74  options.custom_model_config_loader =
75  [](const ::google::protobuf::Any& any,
76  EventBus<ServableState>* event_bus,
77  UniquePtrWithDeps<AspiredVersionsManager>* manager) -> Status {
78  return Status();
79  };
80  if (server_request_logger != nullptr) {
81  options.server_request_logger = std::move(server_request_logger);
82  } else {
83  TF_CHECK_OK(
84  ServerRequestLogger::Create(nullptr, &options.server_request_logger));
85  }
86  return options;
87  }
88 
89  explicit MockServerCore(const PlatformConfigMap& platform_config_map)
90  : MockServerCore(platform_config_map, nullptr) {}
91 
92  MockServerCore(const PlatformConfigMap& platform_config_map,
93  std::unique_ptr<ServerRequestLogger> server_request_logger)
94  : ServerCore(GetOptions(platform_config_map,
95  std::move(server_request_logger))) {}
96 
98  (const, override));
99  MOCK_METHOD(Status, ReloadConfig, (const ModelServerConfig&), (override));
100  MOCK_METHOD(Status, Log,
101  (const google::protobuf::Message& request, const google::protobuf::Message& response,
102  const LogMetadata& log_metadata),
103  (override));
104 
105  // Sets the Servable used by GetServableHandle
106  void SetServable(std::unique_ptr<Servable> servable) {
107  servable_ = std::move(servable);
108  }
109 
110  template <typename T>
111  Status GetServableHandle(const ModelSpec& model_spec,
112  ServableHandle<T>* const handle) {
113  LOG(FATAL) << "Not implemented.";
114  }
115 
116  // Implement GetServable for type Servable. Will return the Servable
117  // set by SetServable, otherwise forwards to base class.
118  virtual Status GetServableHandle(
119  const ModelSpec& model_spec,
120  ServableHandle<Servable>* const handle) override {
121  if (servable_) {
122  const ServableId id = {"servable", 0};
123  *handle = WrapAsHandle<Servable>(id, servable_.get());
124  return absl::OkStatus();
125  } else {
126  return ServerCore::GetServableHandle<Servable>(model_spec, handle);
127  }
128  }
129 
130  std::unique_ptr<Servable> servable_;
131 };
132 
133 } // namespace test_util
134 } // namespace serving
135 } // namespace tensorflow
136 
137 #endif // TENSORFLOW_SERVING_MODEL_SERVERS_TEST_UTIL_MOCK_SERVER_CORE_H_
virtual Status Log(const google::protobuf::Message &request, const google::protobuf::Message &response, const LogMetadata &log_metadata)
Definition: server_core.h:299
virtual Status ReloadConfig(const ModelServerConfig &config) TF_LOCKS_EXCLUDED(config_mu_)
Definition: server_core.cc:447
virtual ServableStateMonitor * servable_state_monitor() const
Returns ServableStateMonitor that can be used to query servable states.
Definition: server_core.h:251
Options for configuring a ServerCore object.
Definition: server_core.h:96