TensorFlow Serving C++ API Documentation
availability_test_util.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/test_util/availability_test_util.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include "absl/types/optional.h"
22 #include "absl/types/span.h"
23 #include "tensorflow/core/platform/env.h"
24 
25 namespace tensorflow {
26 namespace serving {
27 namespace test_util {
28 
29 namespace {
30 
31 // Determines whether WaitUntilServableManagerStateIsOneOf()'s condition is
32 // satisfied. (See that function's documentation.)
33 bool ServableManagerStateIsOneOf(
34  const ServableStateMonitor& monitor, const ServableId& servable,
35  const std::vector<ServableState::ManagerState>& states) {
36  absl::optional<ServableState> maybe_state = monitor.GetState(servable);
37  if (!maybe_state) {
38  return false;
39  }
40  const ServableState state = *maybe_state;
41 
42  for (const ServableState::ManagerState& desired_manager_state : states) {
43  if (state.manager_state == desired_manager_state) {
44  return true;
45  }
46  }
47  return false;
48 }
49 
50 } // namespace
51 
52 void WaitUntilServableManagerStateIsOneOf(
53  const ServableStateMonitor& monitor, const ServableId& servable,
54  const std::vector<ServableState::ManagerState>& states) {
55  while (!ServableManagerStateIsOneOf(monitor, servable, states)) {
56  Env::Default()->SleepForMicroseconds(50 * 1000 /* 50 ms */);
57  }
58 }
59 
60 void WaitUntilVersionsAvailable(
61  const ServableStateMonitor& monitor, const string& servable_id_name,
62  absl::Span<const int64_t> servable_id_versions) {
63  for (int64_t version : servable_id_versions) {
64  const ServableId servable_id = {servable_id_name, version};
65  WaitUntilServableManagerStateIsOneOf(
66  monitor, servable_id, {ServableState::ManagerState::kAvailable});
67  }
68 }
69 
70 } // namespace test_util
71 } // namespace serving
72 } // namespace tensorflow