]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include "jaegertracing/Logging.h" | |
18 | #include "jaegertracing/Sender.h" | |
19 | #include "jaegertracing/utils/ErrorUtil.h" | |
20 | #include <algorithm> | |
21 | #include <gtest/gtest.h> | |
22 | #include <iosfwd> | |
23 | #include <iterator> | |
24 | #include <stdexcept> | |
25 | #include <string> | |
26 | #include <system_error> | |
27 | ||
28 | namespace jaegertracing { | |
29 | namespace utils { | |
30 | namespace { | |
31 | ||
32 | class TestLogger : public logging::Logger { | |
33 | public: | |
34 | void info(const std::string&) override {} | |
35 | ||
36 | void error(const std::string& message) override { _message = message; } | |
37 | ||
38 | const std::string& str() const { return _message; } | |
39 | ||
40 | private: | |
41 | std::string _message; | |
42 | }; | |
43 | ||
44 | bool endsWith(const std::string& str, const std::string& suffix) | |
45 | { | |
46 | if (str.size() < suffix.size()) { | |
47 | return false; | |
48 | } | |
49 | ||
50 | return std::equal( | |
51 | std::begin(suffix), std::end(suffix), std::end(str) - suffix.size()); | |
52 | } | |
53 | ||
54 | } // anonymous namespace | |
55 | ||
56 | TEST(ErrorUtil, test) | |
57 | { | |
58 | std::ostringstream oss; | |
59 | TestLogger logger; | |
60 | std::runtime_error stdEx("runtime error"); | |
61 | std::system_error sysEx(-1, std::generic_category()); | |
62 | Sender::Exception transportEx("test", 5); | |
63 | for (auto i = 0; i < 4; ++i) { | |
64 | try { | |
65 | switch (i) { | |
66 | case 0: | |
67 | throw stdEx; | |
68 | case 1: | |
69 | throw sysEx; | |
70 | case 2: | |
71 | throw transportEx; | |
72 | default: | |
73 | ASSERT_EQ(3, i); | |
74 | throw 5; | |
75 | } | |
76 | } catch (...) { | |
77 | ErrorUtil::logError(logger, "test"); | |
78 | switch (i) { | |
79 | case 0: { | |
80 | ASSERT_EQ("test: runtime error", logger.str()); | |
81 | } break; | |
82 | case 1: { | |
83 | ASSERT_TRUE(endsWith(logger.str(), ", code=-1")); | |
84 | } break; | |
85 | case 2: { | |
86 | ASSERT_EQ("test: test, numFailed=5", logger.str()); | |
87 | } break; | |
88 | default: { | |
89 | ASSERT_EQ(3, i); | |
90 | ASSERT_EQ("test", logger.str()); | |
91 | } break; | |
92 | } | |
93 | } | |
94 | } | |
95 | } | |
96 | ||
97 | } // namespace utils | |
98 | } // namespace jaegertracing |