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::END_MARKER:
92 EXPECT_EQ("unsupported error code for message()", getErrorCodeMessage(ec));
95 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
101 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectSDLInternalError)
103 /* If this test case detects missing error code, remember to add new error code also to AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectClientErrorCode
104 * test case (and add also mapping implementation from InternalError to Error if needed).
108 for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
112 case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
114 EXPECT_TRUE(ec == InternalError::SUCCESS);
116 case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
118 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
120 case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
122 EXPECT_TRUE(ec == InternalError::BACKEND_CONNECTION_LOST);
124 case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
126 EXPECT_TRUE(ec == InternalError::BACKEND_REJECTED_REQUEST);
128 case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
130 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
132 case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
134 EXPECT_TRUE(ec == InternalError::BACKEND_NOT_READY);
136 case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
138 EXPECT_TRUE(ec == InternalError::SDL_NOT_CONNECTED_TO_BACKEND);
140 case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
142 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
144 case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
146 EXPECT_TRUE(ec == InternalError::SDL_ERROR_CODE_LOGIC_ERROR);
149 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
155 TEST_F(ErrorCodesTest, AllErrorCodeEnumsAreMappedToCorrectClientErrorCode)
159 ec = std::error_code();
160 EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
162 ec = AsyncRedisStorage::ErrorCode::SUCCESS;
163 EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
164 ec = AsyncRedisStorage::ErrorCode::REDIS_NOT_YET_DISCOVERED;
165 EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
166 ec = AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE;
167 EXPECT_TRUE(ec == shareddatalayer::Error::REJECTED_BY_SDL);
168 ec = AsyncRedisStorage::ErrorCode::END_MARKER;
169 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
171 ec = AsyncRedisCommandDispatcherErrorCode::SUCCESS;
172 EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
173 ec = AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR;
174 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
175 ec = AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST;
176 EXPECT_TRUE(ec == shareddatalayer::Error::OPERATION_INTERRUPTED);
177 ec = AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR;
178 EXPECT_TRUE(ec == shareddatalayer::Error::REJECTED_BY_BACKEND);
179 ec = AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY;
180 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
181 ec = AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING;
182 EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
183 ec = AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED;
184 EXPECT_TRUE(ec == shareddatalayer::Error::NOT_CONNECTED);
185 ec = AsyncRedisCommandDispatcherErrorCode::IO_ERROR;
186 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
187 ec = AsyncRedisCommandDispatcherErrorCode::END_MARKER;
188 EXPECT_TRUE(ec == shareddatalayer::Error::BACKEND_FAILURE);
191 TEST_F(ErrorCodesTest, ErrorCodeEnumsDoNotMapToIncorrectClientErrorCode)
195 ec = AsyncRedisStorage::ErrorCode::SUCCESS;
196 EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
197 ec = AsyncRedisStorage::ErrorCode::REDIS_NOT_YET_DISCOVERED;
198 EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
199 ec = AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE;
200 EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
201 ec = AsyncRedisStorage::ErrorCode::END_MARKER;
202 EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
204 ec = AsyncRedisCommandDispatcherErrorCode::SUCCESS;
205 EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
206 ec = AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR;
207 EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
208 ec = AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST;
209 EXPECT_TRUE(ec != shareddatalayer::Error::BACKEND_FAILURE);
210 ec = AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR;
211 EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
212 ec = AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY;
213 EXPECT_TRUE(ec != shareddatalayer::Error::NOT_CONNECTED);
214 ec = AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING;
215 EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
216 ec = AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED;
217 EXPECT_TRUE(ec != shareddatalayer::Error::SUCCESS);
218 ec = AsyncRedisCommandDispatcherErrorCode::IO_ERROR;
219 EXPECT_TRUE(ec != shareddatalayer::Error::OPERATION_INTERRUPTED);
220 ec = AsyncRedisCommandDispatcherErrorCode::END_MARKER;
221 EXPECT_TRUE(ec != shareddatalayer::Error::OPERATION_INTERRUPTED);
224 TEST_F(ErrorCodesTest, AllErrorCodeEnumClassesHaveCategory)
226 EXPECT_STREQ("asyncrediscommanddispatcher",
227 std::error_code(AsyncRedisCommandDispatcherErrorCode::SUCCESS).category().name());
229 EXPECT_STREQ("asyncredisstorage",
230 std::error_code(AsyncRedisStorage::ErrorCode::SUCCESS).category().name());