TensorFlow Serving C++ API Documentation
retrier_test.cc
1 /* Copyright 2017 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/util/retrier.h"
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include "absl/status/status.h"
21 #include "xla/tsl/lib/core/status_test_util.h"
22 #include "tensorflow/core/lib/core/errors.h"
23 #include "tensorflow/core/lib/core/status.h"
24 #include "tensorflow/core/platform/errors.h"
25 
26 namespace tensorflow {
27 namespace serving {
28 namespace {
29 
30 using ::testing::HasSubstr;
31 
32 TEST(RetrierTest, RetryFinallySucceeds) {
33  int call_count = 0;
34  auto retried_fn = [&]() {
35  ++call_count;
36  if (call_count == 7) {
37  return absl::OkStatus();
38  }
39  return errors::Unknown("Error");
40  };
41 
42  TF_EXPECT_OK(Retry("RetryFinallySucceeds", 10 /* max_num_retries */,
43  1 /* retry_interval_micros */, retried_fn));
44  EXPECT_EQ(7, call_count);
45 }
46 
47 TEST(RetrierTest, RetryFinallyFails) {
48  int call_count = 0;
49  auto retried_fn = [&]() {
50  ++call_count;
51  return errors::Unknown("Error");
52  };
53 
54  const auto status = Retry("RetryFinallyFails", 10 /* max_num_retries */,
55  0 /* retry_interval_micros */, retried_fn);
56  EXPECT_THAT(status.message(), HasSubstr("Error"));
57  EXPECT_EQ(11, call_count);
58 }
59 
60 TEST(RetrierTest, RetryCancelled) {
61  int call_count = 0;
62  auto retried_fn = [&]() {
63  ++call_count;
64  return errors::Unknown("Error");
65  };
66  const auto status = Retry(
67  "RetryCancelled", 10 /* max_num_retries */, 0 /* retry_interval_micros */,
68  retried_fn, [](absl::Status status) { return false; } /* should retry */);
69  EXPECT_THAT(status.message(), HasSubstr("Error"));
70  EXPECT_EQ(1, call_count);
71 }
72 
73 TEST(RetrierTest, RetryCancelledOnUnimplementedError) {
74  int call_count = 0;
75  auto retried_fn = [&]() {
76  ++call_count;
77  if (call_count == 5) {
78  return errors::Unimplemented("Unimplemented");
79  }
80  return errors::DeadlineExceeded("DeadlineExceeded");
81  };
82 
83  const auto status =
84  Retry("RetryCancelledOnUnimplementedError", 10 /* max_num_retries */,
85  0 /* retry_interval_micros */, retried_fn, [](absl::Status status) {
86  return status.code() != absl::StatusCode::kUnimplemented;
87  });
88  EXPECT_EQ(5, call_count);
89  EXPECT_THAT(status.message(), HasSubstr("Unimplemented"));
90 }
91 
92 } // namespace
93 } // namespace serving
94 } // namespace tensorflow