TensorFlow Serving C++ API Documentation
servable_handle_test_util.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 #ifndef TENSORFLOW_SERVING_CORE_TEST_UTIL_SERVABLE_HANDLE_TEST_UTIL_H_
17 #define TENSORFLOW_SERVING_CORE_TEST_UTIL_SERVABLE_HANDLE_TEST_UTIL_H_
18 
19 #include "tensorflow/core/lib/core/status.h"
20 #include "tensorflow_serving/core/manager.h"
21 #include "tensorflow_serving/core/servable_handle.h"
22 #include "tensorflow_serving/core/servable_id.h"
23 
24 namespace tensorflow {
25 namespace serving {
26 namespace test_util {
27 
28 // Wraps a pointer as a servable.
29 //
30 // The ownership of @p t is *not* transferred to this function; t must outlive
31 // the returned ServableHandle.
32 //
33 // Does a bit of type-gymnastics since ServableHandles can only be constructed
34 // by Managers.
35 template <typename T>
36 static ServableHandle<T> WrapAsHandle(const ServableId& id, T* t) {
37  // A basic Handle that wraps a ServableId and a pointer to a servable.
38  //
39  // Exactly the same objects that are used to construct the DummyHandle are
40  // returned when the appropriate getter functions are invoked.
41  class DummyHandle : public UntypedServableHandle {
42  public:
43  explicit DummyHandle(const ServableId& id, T* servable)
44  : id_(id), servable_(servable) {}
45 
46  AnyPtr servable() override { return servable_; }
47 
48  const ServableId& id() const override { return id_; }
49 
50  private:
51  const ServableId id_;
52  T* servable_;
53  };
54 
55  // A Manager that always returns the same servable when
56  // GetUntypedServableHandle is invoked.
57  class DummyManager : public Manager {
58  public:
59  explicit DummyManager(const ServableId& id, T* servable)
60  : id_(id), servable_(servable) {}
61 
62  // Resets the UntypedServableHandle to a new DummyHandle that wraps the
63  // Servable and ServableId which this DummyManager was created with.
64  //
65  // Always returns OK status.
66  Status GetUntypedServableHandle(
67  const ServableRequest& request,
68  std::unique_ptr<UntypedServableHandle>* result) override {
69  result->reset(new DummyHandle(id_, servable_));
70  return Status();
71  }
72 
73  // Unimplemented: always returns an empty map.
74  std::map<ServableId, std::unique_ptr<UntypedServableHandle>>
75  GetAvailableUntypedServableHandles() const override {
76  return {};
77  }
78 
79  // Unimplemented: always returns an empty vector.
80  std::vector<ServableId> ListAvailableServableIds() const override {
81  return {};
82  }
83 
84  private:
85  const ServableId id_;
86  T* servable_;
87  };
88 
89  DummyManager manager{id, t};
90  ServableHandle<T> handle;
91  TF_CHECK_OK(manager.GetServableHandle({"Dummy", 0}, &handle));
92  return handle;
93 }
94 
95 } // namespace test_util
96 } // namespace serving
97 } // namespace tensorflow
98 
99 #endif // TENSORFLOW_SERVING_CORE_TEST_UTIL_SERVABLE_HANDLE_TEST_UTIL_H_