2 Copyright (c) 2018-2019 Nokia.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <type_traits>
20 #include <gtest/gtest.h>
22 #include <sdl/errorqueries.hpp>
23 #include "private/redis/asyncredisstorage.hpp"
24 #include "private/error.hpp"
26 using namespace shareddatalayer;
27 using namespace shareddatalayer::redis;
28 using namespace testing;
32 std::string getErrorCodeMessage(std::error_code ec)
37 class ErrorCodesTest: public testing::Test
44 virtual ~ErrorCodesTest()
50 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesHaveCorrectDescriptionMessage)
54 for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
58 case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
60 EXPECT_EQ(std::error_code().message(), getErrorCodeMessage(ec));
62 case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
64 EXPECT_EQ("redis error", getErrorCodeMessage(ec));
66 case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
68 EXPECT_EQ("redis connection lost", getErrorCodeMessage(ec));
70 case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
72 EXPECT_EQ("redis protocol error", getErrorCodeMessage(ec));
74 case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
76 EXPECT_EQ("redis out of memory", getErrorCodeMessage(ec));
78 case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
80 EXPECT_EQ("redis dataset still being loaded into memory", getErrorCodeMessage(ec));
82 case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
84 EXPECT_EQ("not connected to redis, SDL operation not started", getErrorCodeMessage(ec));
86 case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
88 EXPECT_EQ("redis I/O error", getErrorCodeMessage(ec));
90 case AsyncRedisCommandDispatcherErrorCode::WRITING_TO_SLAVE:
92 EXPECT_EQ("writing to slave", getErrorCodeMessage(ec));
94 case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
96 EXPECT_EQ("unsupported error code for message()", getErrorCodeMessage(ec));
99 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
105 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectSDLInternalError)
107 /* If this test case detects missing error code, remember to add new error code also to AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectClientErrorCode
108 * test case (and add also mapping implementation from InternalError to Error if needed).
112 for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
116 case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
118 EXPECT_TRUE(ec == InternalError::SUCCESS);
120 case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
122 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
124 case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
126 EXPECT_TRUE(ec == InternalError::BACKEND_CONNECTION_LOST);
128 case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
130 EXPECT_TRUE(ec == InternalError::BACKEND_REJECTED_REQUEST);
132 case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
134 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
136 case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
138 EXPECT_TRUE(ec == InternalError::BACKEND_NOT_READY);
140 case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
142 EXPECT_TRUE(ec == InternalError::SDL_NOT_CONNECTED_TO_BACKEND);
144 case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
146 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
148 case AsyncRedisCommandDispatcherErrorCode::WRITING_TO_SLAVE:
150 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
152 case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
154 EXPECT_TRUE(ec == InternalError::SDL_ERROR_CODE_LOGIC_ERROR);
157 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
163 TEST_F(ErrorCodesTest, AllErrorCodeEnumsAreMappedToCorrectClientErrorCode)
167 ec = std::error_code();
168 EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
170 ec = AsyncRedisStorage::ErrorCode::SUCCESS;
171 EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
172 ec = AsyncRedisStorage::ErrorCode::REDIS_NOT_YET_DISCOVERED;
173 EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
174 ec = AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE;
175 EXPECT_TRUE(ec == shareddatalayer::Error::REJECTED_BY_SDL);
176 ec = AsyncRedisStorage::ErrorCode::END_MARKER;
177 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
179 ec = AsyncRedisCommandDispatcherErrorCode::SUCCESS;
180 EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
181 ec = AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR;
182 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
183 ec = AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST;
184 EXPECT_TRUE(ec == shareddatalayer::Error::OPERATION_INTERRUPTED);
185 ec = AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR;
186 EXPECT_TRUE(ec == shareddatalayer::Error::REJECTED_BY_BACKEND);
187 ec = AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY;
188 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
189 ec = AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING;
190 EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
191 ec = AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED;
192 EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
193 ec = AsyncRedisCommandDispatcherErrorCode::IO_ERROR;
194 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
195 ec = AsyncRedisCommandDispatcherErrorCode::END_MARKER;
196 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
199 TEST_F(ErrorCodesTest, ErrorCodeEnumsDoNotMapToIncorrectClientErrorCode)
203 ec = AsyncRedisStorage::ErrorCode::SUCCESS;
204 EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
205 ec = AsyncRedisStorage::ErrorCode::REDIS_NOT_YET_DISCOVERED;
206 EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
207 ec = AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE;
208 EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
209 ec = AsyncRedisStorage::ErrorCode::END_MARKER;
210 EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
212 ec = AsyncRedisCommandDispatcherErrorCode::SUCCESS;
213 EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
214 ec = AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR;
215 EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
216 ec = AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST;
217 EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
218 ec = AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR;
219 EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
220 ec = AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY;
221 EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
222 ec = AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING;
223 EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
224 ec = AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED;
225 EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
226 ec = AsyncRedisCommandDispatcherErrorCode::IO_ERROR;
227 EXPECT_TRUE(ec != shareddatalayer::Error::OPERATION_INTERRUPTED);
228 ec = AsyncRedisCommandDispatcherErrorCode::END_MARKER;
229 EXPECT_TRUE(ec != shareddatalayer::Error::OPERATION_INTERRUPTED);
232 TEST_F(ErrorCodesTest, AllErrorCodeEnumClassesHaveCategory)
234 EXPECT_STREQ("asyncrediscommanddispatcher",
235 std::error_code(AsyncRedisCommandDispatcherErrorCode::SUCCESS).category().name());
237 EXPECT_STREQ("asyncredisstorage",
238 std::error_code(AsyncRedisStorage::ErrorCode::SUCCESS).category().name());