TensorFlow Serving C++ API Documentation
session_test_util.cc
1 /* Copyright 2019 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/test_util/session_test_util.h"
17 
18 #include <functional>
19 #include <string>
20 #include <utility>
21 
22 #include "absl/memory/memory.h"
23 #include "absl/strings/strip.h"
24 #include "tensorflow/core/common_runtime/session_factory.h"
25 #include "tensorflow/core/public/session.h"
26 
27 namespace tensorflow {
28 namespace serving {
29 namespace test_util {
30 namespace {
31 
32 using NewSessionHook = std::function<Status(const SessionOptions&)>;
33 NewSessionHook new_session_hook_;
34 
35 NewSessionHook GetNewSessionHook() { return new_session_hook_; }
36 
37 // A DelegatingSessionFactory is used to setup the new-session-hook.
38 //
39 // This SessionFactory accepts "new_session_hook/<actual_session_target>" as the
40 // session target. While returning the created session, it calls the
41 // new-session-hook and returns the session created when target is
42 // "<actual_session_target>".
43 class DelegatingSessionFactory : public SessionFactory {
44  public:
45  DelegatingSessionFactory() {}
46 
47  bool AcceptsOptions(const SessionOptions& options) override {
48  return absl::StartsWith(options.target, "new_session_hook/");
49  }
50 
51  Status NewSession(const SessionOptions& options,
52  Session** out_session) override {
53  auto actual_session_options = options;
54  actual_session_options.target = std::string(
55  absl::StripPrefix(options.target, kNewSessionHookSessionTargetPrefix));
56  auto new_session_hook = GetNewSessionHook();
57  if (new_session_hook) {
58  TF_RETURN_IF_ERROR(new_session_hook(actual_session_options));
59  }
60  Session* actual_session;
61  TF_RETURN_IF_ERROR(
62  tensorflow::NewSession(actual_session_options, &actual_session));
63  *out_session = actual_session;
64  return Status();
65  }
66 };
67 
68 class DelegatingSessionRegistrar {
69  public:
70  DelegatingSessionRegistrar() {
71  SessionFactory::Register("DELEGATING_SESSION",
72  new DelegatingSessionFactory());
73  }
74 };
75 static DelegatingSessionRegistrar registrar;
76 
77 } // namespace
78 
79 const char kNewSessionHookSessionTargetPrefix[] = "new_session_hook/";
80 
81 void SetNewSessionHook(NewSessionHook hook) {
82  new_session_hook_ = std::move(hook);
83 }
84 
85 } // namespace test_util
86 } // namespace serving
87 } // namespace tensorflow