forked from tensorflow/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_loader_test.cc
More file actions
216 lines (194 loc) · 7.81 KB
/
Copy pathsimple_loader_test.cc
File metadata and controls
216 lines (194 loc) · 7.81 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
/* 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/simple_loader.h"
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include "tensorflow/core/lib/core/error_codes.pb.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow_serving/core/servable_data.h"
#include "tensorflow_serving/test_util/test_util.h"
namespace tensorflow {
namespace serving {
namespace {
using test_util::CreateProto;
using test_util::EqualsProto;
// State reflects the current state of a Caller object.
enum class State {
kNone,
kCtor,
kDoStuff,
kDtor,
};
// Caller updates a handle to State as it is created, used and destroyed.
class Caller {
public:
explicit Caller(State* state) : state_(state) { *state_ = State::kCtor; }
void DoStuff() { *state_ = State::kDoStuff; }
~Caller() { *state_ = State::kDtor; }
private:
State* state_;
};
// Move a Loader through its lifetime and ensure the servable is in the state
// we expect.
TEST(SimpleLoaderTest, VerifyServableStates) {
State state = State::kNone;
std::unique_ptr<Loader> loader(new SimpleLoader<Caller>(
[&state](std::unique_ptr<Caller>* caller) {
caller->reset(new Caller(&state));
return Status::OK();
},
SimpleLoader<Caller>::EstimateNoResources()));
EXPECT_EQ(State::kNone, state);
const Status status = loader->Load();
TF_EXPECT_OK(status);
EXPECT_EQ(State::kCtor, state);
AnyPtr servable = loader->servable();
ASSERT_TRUE(servable.get<Caller>() != nullptr);
servable.get<Caller>()->DoStuff();
EXPECT_EQ(State::kDoStuff, state);
loader->Unload();
EXPECT_EQ(State::kDtor, state);
state = State::kNone;
loader.reset(nullptr);
EXPECT_EQ(State::kNone, state);
}
TEST(SimpleLoaderTest, ResourceEstimation) {
const auto want = CreateProto<ResourceAllocation>(
"resource_quantities { "
" resource { "
" device: 'main' "
" kind: 'processing' "
" } "
" quantity: 42 "
"} ");
std::unique_ptr<Loader> loader(new SimpleLoader<int>(
[](std::unique_ptr<int>* servable) {
servable->reset(new int);
return Status::OK();
},
[&want](ResourceAllocation* estimate) {
*estimate = want;
return Status::OK();
}));
for (int i = 0; i < 2; ++i) {
ResourceAllocation got;
TF_ASSERT_OK(loader->EstimateResources(&got));
EXPECT_THAT(got, EqualsProto(want));
}
}
// Verify that the error returned by the Creator is propagates back through
// Load.
TEST(SimpleLoaderTest, LoadError) {
std::unique_ptr<Loader> loader(new SimpleLoader<Caller>(
[](std::unique_ptr<Caller>* caller) {
return errors::InvalidArgument("No way!");
},
SimpleLoader<Caller>::EstimateNoResources()));
const Status status = loader->Load();
EXPECT_EQ(error::INVALID_ARGUMENT, status.code());
EXPECT_EQ("No way!", status.error_message());
}
// A pass-through implementation of SimpleLoaderSourceAdapter, which can be
// instantiated.
template <typename DataType, typename ServableType>
class SimpleLoaderSourceAdapterImpl final
: public SimpleLoaderSourceAdapter<DataType, ServableType> {
public:
SimpleLoaderSourceAdapterImpl(
typename SimpleLoaderSourceAdapter<DataType, ServableType>::Creator
creator,
typename SimpleLoaderSourceAdapter<
DataType, ServableType>::ResourceEstimator resource_estimator)
: SimpleLoaderSourceAdapter<DataType, ServableType>(creator,
resource_estimator) {}
~SimpleLoaderSourceAdapterImpl() override { TargetBase<DataType>::Detach(); }
};
TEST(SimpleLoaderSourceAdapterTest, Basic) {
SimpleLoaderSourceAdapterImpl<string, string> adapter(
[](const string& data, std::unique_ptr<string>* servable) {
servable->reset(new string);
**servable = strings::StrCat(data, "_was_here");
return Status::OK();
},
[](const string& data, ResourceAllocation* output) {
ResourceAllocation::Entry* entry = output->add_resource_quantities();
entry->mutable_resource()->set_device(data);
entry->set_quantity(42);
return Status::OK();
});
const string kServableName = "test_servable_name";
bool callback_called;
adapter.SetAspiredVersionsCallback(
[&](const StringPiece servable_name,
std::vector<ServableData<std::unique_ptr<Loader>>> versions) {
callback_called = true;
EXPECT_EQ(kServableName, servable_name);
EXPECT_EQ(1, versions.size());
TF_ASSERT_OK(versions[0].status());
std::unique_ptr<Loader> loader = versions[0].ConsumeDataOrDie();
ResourceAllocation estimate_given;
TF_ASSERT_OK(loader->EstimateResources(&estimate_given));
EXPECT_THAT(estimate_given, EqualsProto(CreateProto<ResourceAllocation>(
"resource_quantities { "
" resource { "
" device: 'test_data' "
" } "
" quantity: 42 "
"} ")));
TF_ASSERT_OK(loader->Load());
AnyPtr servable = loader->servable();
ASSERT_TRUE(servable.get<string>() != nullptr);
EXPECT_EQ("test_data_was_here", *servable.get<string>());
});
adapter.SetAspiredVersions(
kServableName, {ServableData<string>({kServableName, 0}, "test_data")});
EXPECT_TRUE(callback_called);
}
// This test verifies that deleting a SimpleLoaderSourceAdapter doesn't affect
// the loaders it has emitted. This is a regression test for b/30189916.
TEST(SimpleLoaderSourceAdapterTest, OkayToDeleteAdapter) {
std::unique_ptr<Loader> loader;
{
// Allocate 'adapter' on the heap so ASAN will catch a use-after-free.
auto adapter = std::unique_ptr<SimpleLoaderSourceAdapter<string, string>>(
new SimpleLoaderSourceAdapterImpl<string, string>(
[](const string& data, std::unique_ptr<string>* servable) {
servable->reset(new string);
**servable = strings::StrCat(data, "_was_here");
return Status::OK();
},
SimpleLoaderSourceAdapter<string, string>::EstimateNoResources()));
const string kServableName = "test_servable_name";
adapter->SetAspiredVersionsCallback(
[&](const StringPiece servable_name,
std::vector<ServableData<std::unique_ptr<Loader>>> versions) {
ASSERT_EQ(1, versions.size());
TF_ASSERT_OK(versions[0].status());
loader = versions[0].ConsumeDataOrDie();
});
adapter->SetAspiredVersions(
kServableName, {ServableData<string>({kServableName, 0}, "test_data")});
// Let 'adapter' fall out of scope and be deleted.
}
// We should be able to invoke the resource-estimation and servable-creation
// callbacks, despite the fact that 'adapter' has been deleted.
ResourceAllocation estimate_given;
TF_ASSERT_OK(loader->EstimateResources(&estimate_given));
TF_ASSERT_OK(loader->Load());
}
} // namespace
} // namespace serving
} // namespace tensorflow