TensorFlow Serving C++ API Documentation
log_collector_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/log_collector.h"
17 
18 #include <memory>
19 
20 #include <gtest/gtest.h>
21 #include "tensorflow/core/lib/core/status_test_util.h"
22 #include "tensorflow/core/protobuf/error_codes.pb.h"
23 #include "tensorflow_serving/config/log_collector_config.pb.h"
24 
25 namespace tensorflow {
26 namespace serving {
27 namespace {
28 
29 class FakeLogCollector : public LogCollector {
30  public:
31  Status CollectMessage(const google::protobuf::Message& message) { return OkStatus(); }
32  Status Flush() override { return OkStatus(); }
33 };
34 
35 LogCollectorConfig CreateConfig(const string& type,
36  const string& filename_prefix) {
37  LogCollectorConfig config;
38  config.set_type(type);
39  config.set_filename_prefix(filename_prefix);
40  return config;
41 }
42 
43 TEST(LogCollectorTest, NotRegistered) {
44  std::unique_ptr<LogCollector> log_collector;
45  const auto status = LogCollector::Create(
46  CreateConfig("notregistered", "filename_prefix"), 0, &log_collector);
47  EXPECT_EQ(status.code(), error::NOT_FOUND);
48 }
49 
50 TEST(LogCollectorTest, Registration) {
51  TF_ASSERT_OK(LogCollector::RegisterFactory(
52  "registered", [](const LogCollectorConfig& config, const uint32 id,
53  std::unique_ptr<LogCollector>* log_collector) {
54  *log_collector = std::unique_ptr<LogCollector>(new FakeLogCollector());
55  return OkStatus();
56  }));
57  std::unique_ptr<LogCollector> log_collector;
58  TF_ASSERT_OK(LogCollector::Create(
59  CreateConfig("registered", "filename_prefix"), 0, &log_collector));
60 }
61 
62 auto duplicate_factory = [](const LogCollectorConfig& config, const uint32 id,
63  std::unique_ptr<LogCollector>* log_collector) {
64  *log_collector = std::unique_ptr<LogCollector>(new FakeLogCollector());
65  return OkStatus();
66 };
67 REGISTER_LOG_COLLECTOR("duplicate", duplicate_factory);
68 
69 TEST(LogCollectorTest, DuplicateRegistration) {
70  const auto status = LogCollector::RegisterFactory(
71  "duplicate", [](const LogCollectorConfig& config, const uint32 id,
72  std::unique_ptr<LogCollector>* log_collector) {
73  *log_collector = std::unique_ptr<LogCollector>(new FakeLogCollector());
74  return OkStatus();
75  });
76  EXPECT_EQ(status.code(), error::ALREADY_EXISTS);
77 }
78 
79 auto creation_factory = [](const LogCollectorConfig& config, const uint32 id,
80  std::unique_ptr<LogCollector>* log_collector) {
81  *log_collector = std::unique_ptr<LogCollector>(new FakeLogCollector());
82  return OkStatus();
83 };
84 REGISTER_LOG_COLLECTOR("creation", creation_factory);
85 
86 TEST(LogCollectorTest, Creation) {
87  std::unique_ptr<LogCollector> log_collector0;
88  TF_ASSERT_OK(LogCollector::Create(CreateConfig("creation", "filename_prefix"),
89  0, &log_collector0));
90  std::unique_ptr<LogCollector> log_collector1;
91  TF_ASSERT_OK(LogCollector::Create(CreateConfig("creation", "filename_prefix"),
92  0, &log_collector1));
93  EXPECT_NE(log_collector0.get(), log_collector1.get());
94 }
95 
96 } // namespace
97 } // namespace serving
98 } // namespace tensorflow