16 #include "tensorflow_serving/core/log_collector.h"
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"
25 namespace tensorflow {
29 class FakeLogCollector :
public LogCollector {
31 Status CollectMessage(
const google::protobuf::Message& message) {
return OkStatus(); }
32 Status Flush()
override {
return OkStatus(); }
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);
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);
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());
57 std::unique_ptr<LogCollector> log_collector;
58 TF_ASSERT_OK(LogCollector::Create(
59 CreateConfig(
"registered",
"filename_prefix"), 0, &log_collector));
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());
67 REGISTER_LOG_COLLECTOR(
"duplicate", duplicate_factory);
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());
76 EXPECT_EQ(status.code(), error::ALREADY_EXISTS);
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());
84 REGISTER_LOG_COLLECTOR(
"creation", creation_factory);
86 TEST(LogCollectorTest, Creation) {
87 std::unique_ptr<LogCollector> log_collector0;
88 TF_ASSERT_OK(LogCollector::Create(CreateConfig(
"creation",
"filename_prefix"),
90 std::unique_ptr<LogCollector> log_collector1;
91 TF_ASSERT_OK(LogCollector::Create(CreateConfig(
"creation",
"filename_prefix"),
93 EXPECT_NE(log_collector0.get(), log_collector1.get());