/Users/deen/code/yugabyte-db/src/yb/util/status-test.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | // Some portions Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
33 | | // Use of this source code is governed by a BSD-style license that can be |
34 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
35 | | |
36 | | #include <array> |
37 | | #include <string> |
38 | | #include <vector> |
39 | | |
40 | | #include <glog/logging.h> |
41 | | #include <gtest/gtest.h> |
42 | | |
43 | | #include "yb/util/errno.h" |
44 | | #include "yb/util/monotime.h" |
45 | | #include "yb/util/random_util.h" |
46 | | #include "yb/util/status.h" |
47 | | #include "yb/util/test_macros.h" |
48 | | |
49 | | using std::string; |
50 | | using namespace std::literals; |
51 | | |
52 | | namespace yb { |
53 | | |
54 | | template <uint8_t category> |
55 | | struct TestErrorTag : IntegralErrorTag<int64_t> { |
56 | | static constexpr uint8_t kCategory = category; |
57 | | |
58 | 0 | static std::string ToMessage(Value value) { |
59 | 0 | return std::to_string(value); |
60 | 0 | } Unexecuted instantiation: _ZN2yb12TestErrorTagILh202EE9ToMessageEx Unexecuted instantiation: _ZN2yb12TestErrorTagILh203EE9ToMessageEx Unexecuted instantiation: _ZN2yb12TestErrorTagILh204EE9ToMessageEx Unexecuted instantiation: _ZN2yb12TestErrorTagILh205EE9ToMessageEx Unexecuted instantiation: _ZN2yb12TestErrorTagILh206EE9ToMessageEx Unexecuted instantiation: _ZN2yb12TestErrorTagILh207EE9ToMessageEx Unexecuted instantiation: _ZN2yb12TestErrorTagILh208EE9ToMessageEx Unexecuted instantiation: _ZN2yb12TestErrorTagILh209EE9ToMessageEx Unexecuted instantiation: _ZN2yb12TestErrorTagILh210EE9ToMessageEx |
61 | | }; |
62 | | |
63 | | template <uint8_t category> |
64 | | using TestError = StatusErrorCodeImpl<TestErrorTag<category>>; |
65 | | |
66 | | class TestErrorDescriptor { |
67 | | public: |
68 | | virtual void ApplyTo(Status* status, int value) = 0; |
69 | | virtual void Check(const Status& status, int value) = 0; |
70 | 99 | virtual ~TestErrorDescriptor() = default; |
71 | | }; |
72 | | |
73 | | template <uint8_t category> |
74 | | class TestErrorDescriptorImpl : public TestErrorDescriptor { |
75 | | public: |
76 | 5.49k | void ApplyTo(Status* status, int value) override { |
77 | 5.49k | *status = status->CloneAndAddErrorCode(TestError<category>(value)); |
78 | 5.49k | } _ZN2yb23TestErrorDescriptorImplILh202EE7ApplyToEPNS_6StatusEi Line | Count | Source | 76 | 530 | void ApplyTo(Status* status, int value) override { | 77 | 530 | *status = status->CloneAndAddErrorCode(TestError<category>(value)); | 78 | 530 | } |
_ZN2yb23TestErrorDescriptorImplILh203EE7ApplyToEPNS_6StatusEi Line | Count | Source | 76 | 585 | void ApplyTo(Status* status, int value) override { | 77 | 585 | *status = status->CloneAndAddErrorCode(TestError<category>(value)); | 78 | 585 | } |
_ZN2yb23TestErrorDescriptorImplILh204EE7ApplyToEPNS_6StatusEi Line | Count | Source | 76 | 627 | void ApplyTo(Status* status, int value) override { | 77 | 627 | *status = status->CloneAndAddErrorCode(TestError<category>(value)); | 78 | 627 | } |
_ZN2yb23TestErrorDescriptorImplILh205EE7ApplyToEPNS_6StatusEi Line | Count | Source | 76 | 620 | void ApplyTo(Status* status, int value) override { | 77 | 620 | *status = status->CloneAndAddErrorCode(TestError<category>(value)); | 78 | 620 | } |
_ZN2yb23TestErrorDescriptorImplILh206EE7ApplyToEPNS_6StatusEi Line | Count | Source | 76 | 632 | void ApplyTo(Status* status, int value) override { | 77 | 632 | *status = status->CloneAndAddErrorCode(TestError<category>(value)); | 78 | 632 | } |
_ZN2yb23TestErrorDescriptorImplILh207EE7ApplyToEPNS_6StatusEi Line | Count | Source | 76 | 622 | void ApplyTo(Status* status, int value) override { | 77 | 622 | *status = status->CloneAndAddErrorCode(TestError<category>(value)); | 78 | 622 | } |
_ZN2yb23TestErrorDescriptorImplILh208EE7ApplyToEPNS_6StatusEi Line | Count | Source | 76 | 614 | void ApplyTo(Status* status, int value) override { | 77 | 614 | *status = status->CloneAndAddErrorCode(TestError<category>(value)); | 78 | 614 | } |
_ZN2yb23TestErrorDescriptorImplILh209EE7ApplyToEPNS_6StatusEi Line | Count | Source | 76 | 604 | void ApplyTo(Status* status, int value) override { | 77 | 604 | *status = status->CloneAndAddErrorCode(TestError<category>(value)); | 78 | 604 | } |
_ZN2yb23TestErrorDescriptorImplILh210EE7ApplyToEPNS_6StatusEi Line | Count | Source | 76 | 657 | void ApplyTo(Status* status, int value) override { | 77 | 657 | *status = status->CloneAndAddErrorCode(TestError<category>(value)); | 78 | 657 | } |
|
79 | | |
80 | 49.4k | void Check(const Status& status, int value) override { |
81 | 49.4k | TestError<category> ec(status); |
82 | 49.4k | ASSERT_EQ(ec.value(), value); |
83 | 49.4k | } _ZN2yb23TestErrorDescriptorImplILh202EE5CheckERKNS_6StatusEi Line | Count | Source | 80 | 5.49k | void Check(const Status& status, int value) override { | 81 | 5.49k | TestError<category> ec(status); | 82 | 5.49k | ASSERT_EQ(ec.value(), value); | 83 | 5.49k | } |
_ZN2yb23TestErrorDescriptorImplILh203EE5CheckERKNS_6StatusEi Line | Count | Source | 80 | 5.49k | void Check(const Status& status, int value) override { | 81 | 5.49k | TestError<category> ec(status); | 82 | 5.49k | ASSERT_EQ(ec.value(), value); | 83 | 5.49k | } |
_ZN2yb23TestErrorDescriptorImplILh204EE5CheckERKNS_6StatusEi Line | Count | Source | 80 | 5.49k | void Check(const Status& status, int value) override { | 81 | 5.49k | TestError<category> ec(status); | 82 | 5.49k | ASSERT_EQ(ec.value(), value); | 83 | 5.49k | } |
_ZN2yb23TestErrorDescriptorImplILh205EE5CheckERKNS_6StatusEi Line | Count | Source | 80 | 5.49k | void Check(const Status& status, int value) override { | 81 | 5.49k | TestError<category> ec(status); | 82 | 5.49k | ASSERT_EQ(ec.value(), value); | 83 | 5.49k | } |
_ZN2yb23TestErrorDescriptorImplILh206EE5CheckERKNS_6StatusEi Line | Count | Source | 80 | 5.49k | void Check(const Status& status, int value) override { | 81 | 5.49k | TestError<category> ec(status); | 82 | 5.49k | ASSERT_EQ(ec.value(), value); | 83 | 5.49k | } |
_ZN2yb23TestErrorDescriptorImplILh207EE5CheckERKNS_6StatusEi Line | Count | Source | 80 | 5.49k | void Check(const Status& status, int value) override { | 81 | 5.49k | TestError<category> ec(status); | 82 | 5.49k | ASSERT_EQ(ec.value(), value); | 83 | 5.49k | } |
_ZN2yb23TestErrorDescriptorImplILh208EE5CheckERKNS_6StatusEi Line | Count | Source | 80 | 5.49k | void Check(const Status& status, int value) override { | 81 | 5.49k | TestError<category> ec(status); | 82 | 5.49k | ASSERT_EQ(ec.value(), value); | 83 | 5.49k | } |
_ZN2yb23TestErrorDescriptorImplILh209EE5CheckERKNS_6StatusEi Line | Count | Source | 80 | 5.49k | void Check(const Status& status, int value) override { | 81 | 5.49k | TestError<category> ec(status); | 82 | 5.49k | ASSERT_EQ(ec.value(), value); | 83 | 5.49k | } |
_ZN2yb23TestErrorDescriptorImplILh210EE5CheckERKNS_6StatusEi Line | Count | Source | 80 | 5.49k | void Check(const Status& status, int value) override { | 81 | 5.49k | TestError<category> ec(status); | 82 | 5.49k | ASSERT_EQ(ec.value(), value); | 83 | 5.49k | } |
|
84 | | }; |
85 | | |
86 | | constexpr uint8_t kMinTestError = 202; |
87 | | constexpr uint8_t kMaxTestError = 210; |
88 | | |
89 | | std::array<std::unique_ptr<TestErrorDescriptor>, kMaxTestError + 1> kTestErrorDescriptors; |
90 | | |
91 | | template <uint8_t category> |
92 | 99 | StatusCategoryRegisterer& RegisterTestError() { |
93 | 99 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); |
94 | | |
95 | 99 | static StatusCategoryRegisterer test_error_category_registerer( |
96 | 99 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); |
97 | 99 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); |
98 | 99 | return test_error_category_registerer; |
99 | 99 | } _ZN2yb17RegisterTestErrorILh202EEERNS_24StatusCategoryRegistererEv Line | Count | Source | 92 | 11 | StatusCategoryRegisterer& RegisterTestError() { | 93 | 11 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); | 94 | | | 95 | 11 | static StatusCategoryRegisterer test_error_category_registerer( | 96 | 11 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); | 97 | 11 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); | 98 | 11 | return test_error_category_registerer; | 99 | 11 | } |
_ZN2yb17RegisterTestErrorILh203EEERNS_24StatusCategoryRegistererEv Line | Count | Source | 92 | 11 | StatusCategoryRegisterer& RegisterTestError() { | 93 | 11 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); | 94 | | | 95 | 11 | static StatusCategoryRegisterer test_error_category_registerer( | 96 | 11 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); | 97 | 11 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); | 98 | 11 | return test_error_category_registerer; | 99 | 11 | } |
_ZN2yb17RegisterTestErrorILh204EEERNS_24StatusCategoryRegistererEv Line | Count | Source | 92 | 11 | StatusCategoryRegisterer& RegisterTestError() { | 93 | 11 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); | 94 | | | 95 | 11 | static StatusCategoryRegisterer test_error_category_registerer( | 96 | 11 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); | 97 | 11 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); | 98 | 11 | return test_error_category_registerer; | 99 | 11 | } |
_ZN2yb17RegisterTestErrorILh205EEERNS_24StatusCategoryRegistererEv Line | Count | Source | 92 | 11 | StatusCategoryRegisterer& RegisterTestError() { | 93 | 11 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); | 94 | | | 95 | 11 | static StatusCategoryRegisterer test_error_category_registerer( | 96 | 11 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); | 97 | 11 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); | 98 | 11 | return test_error_category_registerer; | 99 | 11 | } |
_ZN2yb17RegisterTestErrorILh206EEERNS_24StatusCategoryRegistererEv Line | Count | Source | 92 | 11 | StatusCategoryRegisterer& RegisterTestError() { | 93 | 11 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); | 94 | | | 95 | 11 | static StatusCategoryRegisterer test_error_category_registerer( | 96 | 11 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); | 97 | 11 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); | 98 | 11 | return test_error_category_registerer; | 99 | 11 | } |
_ZN2yb17RegisterTestErrorILh207EEERNS_24StatusCategoryRegistererEv Line | Count | Source | 92 | 11 | StatusCategoryRegisterer& RegisterTestError() { | 93 | 11 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); | 94 | | | 95 | 11 | static StatusCategoryRegisterer test_error_category_registerer( | 96 | 11 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); | 97 | 11 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); | 98 | 11 | return test_error_category_registerer; | 99 | 11 | } |
_ZN2yb17RegisterTestErrorILh208EEERNS_24StatusCategoryRegistererEv Line | Count | Source | 92 | 11 | StatusCategoryRegisterer& RegisterTestError() { | 93 | 11 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); | 94 | | | 95 | 11 | static StatusCategoryRegisterer test_error_category_registerer( | 96 | 11 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); | 97 | 11 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); | 98 | 11 | return test_error_category_registerer; | 99 | 11 | } |
_ZN2yb17RegisterTestErrorILh209EEERNS_24StatusCategoryRegistererEv Line | Count | Source | 92 | 11 | StatusCategoryRegisterer& RegisterTestError() { | 93 | 11 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); | 94 | | | 95 | 11 | static StatusCategoryRegisterer test_error_category_registerer( | 96 | 11 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); | 97 | 11 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); | 98 | 11 | return test_error_category_registerer; | 99 | 11 | } |
_ZN2yb17RegisterTestErrorILh210EEERNS_24StatusCategoryRegistererEv Line | Count | Source | 92 | 11 | StatusCategoryRegisterer& RegisterTestError() { | 93 | 11 | static const std::string kTestErrorCategoryName = "test error " + std::to_string(category); | 94 | | | 95 | 11 | static StatusCategoryRegisterer test_error_category_registerer( | 96 | 11 | StatusCategoryDescription::Make<TestErrorTag<category>>(&kTestErrorCategoryName)); | 97 | 11 | kTestErrorDescriptors[category] = std::make_unique<TestErrorDescriptorImpl<category>>(); | 98 | 11 | return test_error_category_registerer; | 99 | 11 | } |
|
100 | | |
101 | | template <uint8_t category> |
102 | | struct RegisterTestErrors { |
103 | 88 | RegisterTestErrors() { |
104 | 88 | RegisterTestErrors<category - 1> temp; |
105 | 88 | RegisterTestError<category>(); |
106 | 88 | } _ZN2yb18RegisterTestErrorsILh210EEC2Ev Line | Count | Source | 103 | 11 | RegisterTestErrors() { | 104 | 11 | RegisterTestErrors<category - 1> temp; | 105 | 11 | RegisterTestError<category>(); | 106 | 11 | } |
_ZN2yb18RegisterTestErrorsILh209EEC2Ev Line | Count | Source | 103 | 11 | RegisterTestErrors() { | 104 | 11 | RegisterTestErrors<category - 1> temp; | 105 | 11 | RegisterTestError<category>(); | 106 | 11 | } |
_ZN2yb18RegisterTestErrorsILh208EEC2Ev Line | Count | Source | 103 | 11 | RegisterTestErrors() { | 104 | 11 | RegisterTestErrors<category - 1> temp; | 105 | 11 | RegisterTestError<category>(); | 106 | 11 | } |
_ZN2yb18RegisterTestErrorsILh207EEC2Ev Line | Count | Source | 103 | 11 | RegisterTestErrors() { | 104 | 11 | RegisterTestErrors<category - 1> temp; | 105 | 11 | RegisterTestError<category>(); | 106 | 11 | } |
_ZN2yb18RegisterTestErrorsILh206EEC2Ev Line | Count | Source | 103 | 11 | RegisterTestErrors() { | 104 | 11 | RegisterTestErrors<category - 1> temp; | 105 | 11 | RegisterTestError<category>(); | 106 | 11 | } |
_ZN2yb18RegisterTestErrorsILh205EEC2Ev Line | Count | Source | 103 | 11 | RegisterTestErrors() { | 104 | 11 | RegisterTestErrors<category - 1> temp; | 105 | 11 | RegisterTestError<category>(); | 106 | 11 | } |
_ZN2yb18RegisterTestErrorsILh204EEC2Ev Line | Count | Source | 103 | 11 | RegisterTestErrors() { | 104 | 11 | RegisterTestErrors<category - 1> temp; | 105 | 11 | RegisterTestError<category>(); | 106 | 11 | } |
_ZN2yb18RegisterTestErrorsILh203EEC2Ev Line | Count | Source | 103 | 11 | RegisterTestErrors() { | 104 | 11 | RegisterTestErrors<category - 1> temp; | 105 | 11 | RegisterTestError<category>(); | 106 | 11 | } |
|
107 | | }; |
108 | | |
109 | | template <> |
110 | | struct RegisterTestErrors<kMinTestError> { |
111 | 11 | RegisterTestErrors() { |
112 | 11 | RegisterTestError<kMinTestError>(); |
113 | 11 | } |
114 | | }; |
115 | | |
116 | | RegisterTestErrors<kMaxTestError> register_test_errors; |
117 | | |
118 | | class ErrorDelayTraits { |
119 | | public: |
120 | | typedef MonoDelta ValueType; |
121 | | typedef int64_t RepresentationType; |
122 | | |
123 | 2 | static MonoDelta FromRepresentation(RepresentationType source) { |
124 | 2 | return MonoDelta::FromNanoseconds(source); |
125 | 2 | } |
126 | | |
127 | 1 | static RepresentationType ToRepresentation(MonoDelta value) { |
128 | 1 | return value.ToNanoseconds(); |
129 | 1 | } |
130 | | |
131 | 1 | static std::string ToString(MonoDelta value) { |
132 | 1 | return value.ToString(); |
133 | 1 | } |
134 | | }; |
135 | | |
136 | | class ErrorDelayTag : public IntegralBackedErrorTag<ErrorDelayTraits> { |
137 | | public: |
138 | | static constexpr uint8_t kCategory = kMaxTestError + 1; |
139 | | |
140 | 1 | static std::string ToMessage(MonoDelta value) { |
141 | 1 | return value.ToString(); |
142 | 1 | } |
143 | | }; |
144 | | |
145 | | typedef StatusErrorCodeImpl<ErrorDelayTag> ErrorDelay; |
146 | | |
147 | | const std::string kErrorDelayCategoryName = "error delay"; |
148 | | |
149 | | static StatusCategoryRegisterer error_delay_category_registerer( |
150 | | StatusCategoryDescription::Make<ErrorDelayTag>(&kErrorDelayCategoryName)); |
151 | | |
152 | | class StringVectorErrorTag : public StringVectorBackedErrorTag { |
153 | | public: |
154 | | static constexpr uint8_t kCategory = kMaxTestError + 2; |
155 | | |
156 | 4 | static std::string ToMessage(Value value) { |
157 | 4 | return AsString(value); |
158 | 4 | } |
159 | | }; |
160 | | |
161 | | typedef StatusErrorCodeImpl<StringVectorErrorTag> StringVectorError; |
162 | | |
163 | | const std::string kStringVectorErrorCategoryName = "string vector error"; |
164 | | |
165 | | static StatusCategoryRegisterer string_vector_error_category_registerer( |
166 | | StatusCategoryDescription::Make<StringVectorErrorTag>(&kStringVectorErrorCategoryName)); |
167 | | |
168 | 1 | TEST(StatusTest, TestPosixCode) { |
169 | 1 | Status ok = Status::OK(); |
170 | 1 | ASSERT_EQ(0, Errno(ok)); |
171 | 1 | Status file_error = STATUS(IOError, "file error", Slice(), Errno(ENOTDIR)); |
172 | 1 | ASSERT_EQ(ENOTDIR, Errno(file_error)); |
173 | 1 | } |
174 | | |
175 | 1 | TEST(StatusTest, TestToString) { |
176 | 1 | Status file_error = STATUS(IOError, "file error", Slice(), Errno(ENOTDIR)); |
177 | 1 | ASSERT_EQ("IO error: file error (system error 20)", file_error.ToString(false)); |
178 | 1 | } |
179 | | |
180 | 1 | TEST(StatusTest, TestClonePrepend) { |
181 | 1 | Status file_error = STATUS(IOError, "file error", "msg2", Errno(ENOTDIR)); |
182 | 1 | Status appended = file_error.CloneAndPrepend("Heading"); |
183 | 1 | ASSERT_EQ("IO error: Heading: file error: msg2 (system error 20)", |
184 | 1 | appended.ToString(false)); |
185 | 1 | } |
186 | | |
187 | 1 | TEST(StatusTest, TestCloneAppend) { |
188 | 1 | Status remote_error = STATUS(RemoteError, "Application error"); |
189 | 1 | Status appended = remote_error.CloneAndAppend(STATUS(NotFound, "Unknown tablet").ToString(false)); |
190 | 1 | ASSERT_EQ(string("Remote error: Application error: Not found: Unknown tablet"), |
191 | 1 | appended.ToString(false)); |
192 | 1 | } |
193 | | |
194 | 1 | TEST(StatusTest, CloneAndAddErrorCode) { |
195 | 1 | Status remote_error = STATUS(RemoteError, "ERROR", Slice(), Errno(ENOTDIR)); |
196 | 1 | Status appended = remote_error.CloneAndAddErrorCode(TestError<kMinTestError>(42)); |
197 | 1 | ASSERT_EQ(Format("Remote error: ERROR (system error 20) (test error $0 42)", kMinTestError), |
198 | 1 | appended.ToString(false)); |
199 | 1 | } |
200 | | |
201 | 1 | TEST(StatusTest, RandomErrorCodes) { |
202 | 1.00k | for (int i = 0; i != 1000; ++i) { |
203 | 1.00k | auto status = STATUS(RemoteError, "ERROR"); |
204 | 1.00k | int num_extra_errors = RandomUniformInt(1, 10); |
205 | 1.00k | std::map<uint8_t, int> extra_errors; |
206 | 1.00k | LOG(INFO) << "==================================================="; |
207 | 6.49k | for (int j = 0; j != num_extra_errors; ++j) { |
208 | 5.49k | auto error_code = RandomUniformInt(kMinTestError, kMaxTestError); |
209 | 5.49k | auto value = RandomUniformInt(1, 1000); |
210 | 5.49k | LOG(INFO) << "Add: " << static_cast<int>(error_code) << ", " << value; |
211 | 5.49k | extra_errors[error_code] = value; |
212 | 5.49k | kTestErrorDescriptors[error_code]->ApplyTo(&status, value); |
213 | | |
214 | 5.49k | std::string str = "Remote error: ERROR"; |
215 | 54.9k | for (auto ec = kMinTestError; ec <= kMaxTestError; ++ec) { |
216 | 49.4k | SCOPED_TRACE(Format("Error code: $0", static_cast<int>(ec))); |
217 | 49.4k | auto it = extra_errors.find(ec); |
218 | 49.4k | if (it != extra_errors.end()) { |
219 | 17.2k | ASSERT_NO_FATALS(kTestErrorDescriptors[ec]->Check(status, it->second)); |
220 | 17.2k | str += Format(" (test error $0 $1)", ec, it->second); |
221 | 32.1k | } else { |
222 | 64.3k | ASSERT_NO_FATALS(kTestErrorDescriptors[ec]->Check(status, 0)) |
223 | 64.3k | << "Extra errors: " << yb::ToString(extra_errors); |
224 | 32.1k | } |
225 | 49.4k | } |
226 | 5.49k | ASSERT_EQ(str, status.ToString(false)); |
227 | 5.49k | LOG(INFO) << "To string: " << str; |
228 | 5.49k | } |
229 | 1.00k | } |
230 | 1 | } |
231 | | |
232 | | |
233 | | |
234 | 1 | TEST(StatusTest, TestMemoryUsage) { |
235 | 1 | ASSERT_EQ(0, static_cast<Status>(Status::OK()).memory_footprint_excluding_this()); |
236 | 1 | auto status = STATUS(IOError, "file error", "some other thing", Errno(ENOTDIR)); |
237 | 1 | ASSERT_GT(status.memory_footprint_excluding_this(), 0); |
238 | 1 | } |
239 | | |
240 | 1 | TEST(StatusTest, TestMoveConstructor) { |
241 | | // OK->OK move should do nothing. |
242 | 1 | { |
243 | 1 | Status src = Status::OK(); |
244 | 1 | Status dst = std::move(src); |
245 | 1 | ASSERT_OK(src); |
246 | 1 | ASSERT_OK(dst); |
247 | 1 | } |
248 | | |
249 | | // Moving a not-OK status into a new one should make the moved status |
250 | | // "OK". |
251 | 1 | { |
252 | 1 | Status src = STATUS(NotFound, "foo"); |
253 | 1 | Status dst = std::move(src); |
254 | 1 | ASSERT_OK(src); |
255 | 1 | ASSERT_EQ("Not found: foo", dst.ToString(false)); |
256 | 1 | } |
257 | 1 | } |
258 | | |
259 | 1 | TEST(StatusTest, TestMoveAssignment) { |
260 | | // OK->Bad move should clear the source status and also make the |
261 | | // destination status OK. |
262 | 1 | { |
263 | 1 | Status src = Status::OK(); |
264 | 1 | Status dst = STATUS(NotFound, "orig dst"); |
265 | 1 | dst = std::move(src); |
266 | 1 | ASSERT_OK(src); |
267 | 1 | ASSERT_OK(dst); |
268 | 1 | } |
269 | | |
270 | | // Bad->Bad move. |
271 | 1 | { |
272 | 1 | Status src = STATUS(NotFound, "orig src"); |
273 | 1 | Status dst = STATUS(NotFound, "orig dst"); |
274 | 1 | dst = std::move(src); |
275 | 1 | ASSERT_OK(src); |
276 | 1 | ASSERT_EQ("Not found: orig src", dst.ToString(false)); |
277 | 1 | } |
278 | | |
279 | | // Bad->OK move |
280 | 1 | { |
281 | 1 | Status src = STATUS(NotFound, "orig src"); |
282 | 1 | Status dst = Status::OK(); |
283 | 1 | dst = std::move(src); |
284 | 1 | ASSERT_OK(src); |
285 | 1 | ASSERT_EQ("Not found: orig src", dst.ToString(false)); |
286 | 1 | } |
287 | 1 | } |
288 | | |
289 | 1 | TEST(StatusTest, IntegralBackedError) { |
290 | 1 | MonoDelta delay = 100ms; |
291 | 1 | auto status = STATUS(TimedOut, "TEST", ErrorDelay(delay)); |
292 | 1 | LOG(INFO) << status; |
293 | 1 | ASSERT_EQ(ErrorDelay(status), delay); |
294 | 1 | } |
295 | | |
296 | 1 | TEST(StatusTest, StringVectorError) { |
297 | 1 | std::vector<std::string> vector; |
298 | 5 | for (int i = 0; i <= 3; ++i) { |
299 | 4 | auto status = STATUS(TimedOut, "TEST", StringVectorError(vector)); |
300 | 4 | LOG(INFO) << status; |
301 | 4 | ASSERT_EQ(StringVectorError(status), vector); |
302 | 4 | std::string str("TEST_"); |
303 | 14 | for (int j = 0; j <= i; ++j) { |
304 | 10 | str.append(AsString(j)); |
305 | 10 | } |
306 | 4 | vector.push_back(str); |
307 | 4 | } |
308 | 1 | } |
309 | | |
310 | | } // namespace yb |