TensorFlow Serving C++ API Documentation
fake_loader.h
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 #ifndef TENSORFLOW_SERVING_CORE_TEST_UTIL_FAKE_LOADER_H_
17 #define TENSORFLOW_SERVING_CORE_TEST_UTIL_FAKE_LOADER_H_
18 
19 #include "tensorflow/core/lib/core/status.h"
20 #include "tensorflow/core/platform/mutex.h"
21 #include "tensorflow/core/platform/thread_annotations.h"
22 #include "tensorflow_serving/core/loader.h"
23 #include "tensorflow_serving/util/any_ptr.h"
24 
25 namespace tensorflow {
26 namespace serving {
27 namespace test_util {
28 
29 // A fake loader to be used in tests.
30 //
31 // Useful for -
32 // 1. Erroring out on load.
33 // 2. Counting the total number of fake loaders alive.
34 // 3. Detecting if deletion of the loader happened on a particular thread.
35 //
36 // This class is thread-safe.
38  public:
39  explicit FakeLoader(int64_t servable, const Status load_status = Status());
40 
41  ~FakeLoader() override;
42 
43  // The status returned during load.
44  Status load_status();
45 
46  Status Load() override;
47 
48  void Unload() override;
49 
50  AnyPtr servable() override;
51 
52  static int num_fake_loaders();
53 
54  // Returns true if a loader was deleted in this thread.
55  static bool was_deleted_in_this_thread();
56 
57  private:
58  // Used to detect the thread in which deletion of a loader occurs.
59  static thread_local bool was_deleted_in_this_thread_;
60 
61  // Counts the number of FakeLoader objects alive.
62  static int num_fake_loaders_ TF_GUARDED_BY(num_fake_loaders_mu_);
63  static mutex num_fake_loaders_mu_;
64 
65  // The servable returned from this loader.
66  //
67  // Don't make const or you'll have to change the handle type to 'const int64'.
68  int64_t servable_;
69  // The status returned during load.
70  const Status load_status_;
71 };
72 
73 } // namespace test_util
74 } // namespace serving
75 } // namespace tensorflow
76 
77 #endif // TENSORFLOW_SERVING_CORE_TEST_UTIL_FAKE_LOADER_H_