TensorFlow Serving C++ API Documentation
static_source_router_test.cc
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 #include "tensorflow_serving/core/static_source_router.h"
17 
18 #include <algorithm>
19 #include <memory>
20 #include <utility>
21 #include <vector>
22 
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include "tensorflow/core/lib/core/status.h"
26 #include "tensorflow/core/lib/core/status_test_util.h"
27 #include "tensorflow/core/lib/core/stringpiece.h"
28 #include "tensorflow/core/platform/types.h"
29 #include "tensorflow_serving/core/servable_data.h"
30 #include "tensorflow_serving/core/source.h"
31 #include "tensorflow_serving/core/storage_path.h"
32 #include "tensorflow_serving/core/target.h"
33 #include "tensorflow_serving/core/test_util/mock_storage_path_target.h"
34 
35 using ::testing::ElementsAre;
36 using ::testing::Eq;
37 using ::testing::StrictMock;
38 
39 namespace tensorflow {
40 namespace serving {
41 namespace {
42 
43 TEST(StaticSourceRouterTest, Basic) {
44  const std::vector<string> regexps = {"0th", "1st"};
45  std::unique_ptr<StaticSourceRouter<StoragePath>> router;
46  TF_ASSERT_OK(StaticSourceRouter<string>::Create(regexps, &router));
47  std::vector<Source<StoragePath>*> output_ports = router->GetOutputPorts();
48  ASSERT_EQ(regexps.size() + 1, output_ports.size());
49  std::vector<std::unique_ptr<test_util::MockStoragePathTarget>> targets;
50  for (int i = 0; i < output_ports.size(); ++i) {
51  std::unique_ptr<test_util::MockStoragePathTarget> target(
52  new StrictMock<test_util::MockStoragePathTarget>);
53  ConnectSourceToTarget(output_ports[i], target.get());
54  targets.push_back(std::move(target));
55  }
56 
57  // Matches the 0th regexp. Should go to output port 0.
58  EXPECT_CALL(
59  *targets[0],
60  SetAspiredVersions(Eq("0th_foo"), ElementsAre(ServableData<StoragePath>(
61  {"0th_foo", 7}, "data"))));
62  router->SetAspiredVersions(
63  "0th_foo", {ServableData<StoragePath>({"0th_foo", 7}, "data")});
64 
65  // Matches the 0th and 1st regexps. Should go to output port 0.
66  EXPECT_CALL(*targets[0],
67  SetAspiredVersions(Eq("0th_foo_1st"),
68  ElementsAre(ServableData<StoragePath>(
69  {"0th_foo_1st", 7}, "data"))));
70  router->SetAspiredVersions(
71  "0th_foo_1st", {ServableData<StoragePath>({"0th_foo_1st", 7}, "data")});
72 
73  // Matches the 1st regexp but not the 0th. Should go to output port 1.
74  EXPECT_CALL(
75  *targets[1],
76  SetAspiredVersions(Eq("foo_1st"), ElementsAre(ServableData<StoragePath>(
77  {"foo_1st", 7}, "data"))));
78  router->SetAspiredVersions(
79  "foo_1st", {ServableData<StoragePath>({"foo_1st", 7}, "data")});
80 
81  // Doesn't match any of the regexps. Should go to output port 2.
82  EXPECT_CALL(
83  *targets[2],
84  SetAspiredVersions(Eq("no_match"), ElementsAre(ServableData<StoragePath>(
85  {"no_match", 7}, "data"))));
86  router->SetAspiredVersions(
87  "no_match", {ServableData<StoragePath>({"no_match", 7}, "data")});
88 }
89 
90 } // namespace
91 } // namespace serving
92 } // namespace tensorflow