forked from tensorflow/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager_test.cc
More file actions
233 lines (191 loc) · 6.79 KB
/
Copy pathmanager_test.cc
File metadata and controls
233 lines (191 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow_serving/core/manager.h"
#include <gtest/gtest.h>
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow_serving/util/any_ptr.h"
namespace tensorflow {
namespace serving {
namespace {
struct TestServable {
int member = 7;
};
class TestHandle : public UntypedServableHandle {
public:
AnyPtr servable() override { return &servable_; }
const ServableId& id() const override { return id_; }
private:
const ServableId id_ = {"servable", 7};
TestServable servable_;
};
// A manager that a returns a TestHandle.
class TestManager : public Manager {
public:
std::vector<ServableId> ListAvailableServableIds() const override {
LOG(FATAL) << "Not expected to be called.";
}
private:
Status GetUntypedServableHandle(
const ServableRequest& request,
std::unique_ptr<UntypedServableHandle>* result) override {
result->reset(new TestHandle);
return Status::OK();
}
std::map<ServableId, std::unique_ptr<UntypedServableHandle>>
GetAvailableUntypedServableHandles() const override {
std::map<ServableId, std::unique_ptr<UntypedServableHandle>> handles;
handles.emplace(ServableId{"Foo", 2},
std::unique_ptr<UntypedServableHandle>(new TestHandle));
return handles;
}
};
TEST(ManagerTest, NoErrors) {
TestManager manager;
ServableHandle<TestServable> handle;
EXPECT_TRUE(manager.GetServableHandle({"Foo", 2}, &handle).ok());
EXPECT_NE(nullptr, handle.get());
}
TEST(ManagerTest, TypeError) {
TestManager manager;
ServableHandle<int> handle;
EXPECT_FALSE(manager.GetServableHandle({"Foo", 2}, &handle).ok());
EXPECT_EQ(nullptr, handle.get());
}
TEST(ManagerTest, GetAvailableServableHandles) {
TestManager manager;
const std::map<ServableId, ServableHandle<TestServable>> handles =
manager.GetAvailableServableHandles<TestServable>();
ASSERT_EQ(1, handles.size());
for (const auto& handle : handles) {
EXPECT_EQ((ServableId{"Foo", 2}), handle.first);
EXPECT_EQ(7, handle.second->member);
}
}
TEST(ManagerTest, GetAvailableServableHandlesWrongType) {
TestManager manager;
const std::map<ServableId, ServableHandle<int>> handles =
manager.GetAvailableServableHandles<int>();
EXPECT_EQ(0, handles.size());
}
// A manager that returns OK even though the result is null. This behavior
// violates the interface of Manager, but it is used to test that this violation
// is handled gracefully rather than a crash or memory corruption.
class ReturnNullManager : public TestManager {
private:
Status GetUntypedServableHandle(
const ServableRequest& request,
std::unique_ptr<UntypedServableHandle>* result) override {
*result = nullptr;
return Status::OK();
}
};
TEST(ManagerTest, NullHandleReturnsError) {
ReturnNullManager manager;
ServableHandle<TestServable> handle;
EXPECT_FALSE(manager.GetServableHandle({"Foo", 2}, &handle).ok());
EXPECT_EQ(nullptr, handle.get());
}
// A manager that returns an error even though the result is non-null.
class ReturnErrorManager : public TestManager {
private:
Status GetUntypedServableHandle(
const ServableRequest& request,
std::unique_ptr<UntypedServableHandle>* result) override {
result->reset(new TestHandle);
return errors::Internal("Something bad happened.");
}
};
TEST(ManagerTest, ErrorReturnsNullHandle) {
ReturnErrorManager manager;
ServableHandle<TestServable> handle;
EXPECT_FALSE(manager.GetServableHandle({"Foo", 2}, &handle).ok());
EXPECT_EQ(nullptr, handle.get());
}
// Wraps a pointer as a servable. Does a bit of type-gymnastics since servable
// handles can only be constructed by managers.
template <typename T>
ServableHandle<T> WrapAsHandle(const ServableId& id, T* t) {
// Perform some type gymnastics to create a handle that points to 't'.
class DummyHandle : public UntypedServableHandle {
public:
explicit DummyHandle(const ServableId& id, T* servable)
: id_(id), servable_(servable) {}
AnyPtr servable() override { return servable_; }
const ServableId& id() const override { return id_; }
private:
const ServableId id_;
T* servable_;
};
// Always returns the same servable.
class DummyManager : public TestManager {
public:
explicit DummyManager(const ServableId& id, T* servable)
: id_(id), servable_(servable) {}
Status GetUntypedServableHandle(
const ServableRequest& request,
std::unique_ptr<UntypedServableHandle>* result) override {
result->reset(new DummyHandle(id_, servable_));
return Status::OK();
}
private:
const ServableId id_;
T* servable_;
};
DummyManager manager{id, t};
ServableHandle<T> handle;
TF_CHECK_OK(manager.GetServableHandle({"Dummy", 0}, &handle));
return handle;
}
TEST(ServableHandleTest, PointerOps) {
TestServable servables[2];
ServableHandle<TestServable> handles[2];
const ServableId id = {"servable", 7};
handles[0] = WrapAsHandle(id, &servables[0]);
handles[1] = WrapAsHandle(id, &servables[1]);
// Equality.
EXPECT_EQ(handles[0], handles[0]);
// Inequality.
EXPECT_NE(handles[0], handles[1]);
// Bool conversion.
EXPECT_TRUE(handles[0]);
// Dereference and get.
EXPECT_EQ(&servables[0], handles[0].get());
EXPECT_EQ(&servables[0], &*handles[0]);
EXPECT_EQ(&servables[0].member, &handles[0]->member);
}
TEST(ServableHandleTest, Id) {
TestServable servables[2];
ServableHandle<TestServable> handles[2];
const ServableId id = {"servable", 7};
handles[0] = WrapAsHandle(id, &servables[0]);
handles[1] = WrapAsHandle(id, &servables[1]);
EXPECT_EQ(id, handles[0].id());
EXPECT_EQ(id, handles[1].id());
}
TEST(ServableRequestTest, Specific) {
const auto request = ServableRequest::Specific("servable", 7);
EXPECT_EQ("servable", request.name);
EXPECT_EQ(7, *request.version);
}
TEST(ServableRequestTest, Latest) {
const auto request = ServableRequest::Latest("servable");
EXPECT_EQ("servable", request.name);
EXPECT_FALSE(request.version);
}
TEST(ServableRequestTest, FromId) {
const auto request = ServableRequest::FromId({"servable", 7});
EXPECT_EQ("servable", request.name);
EXPECT_EQ(7, *request.version);
}
} // namespace
} // namespace serving
} // namespace tensorflow