Add Sentinel change notification handling logic
[ric-plt/sdl.git] / tst / error_test.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
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 <type_traits>
18 #include <memory>
19 #include <cstring>
20 #include <gtest/gtest.h>
21 #include <async.h>
22 #include <sdl/errorqueries.hpp>
23 #include "private/redis/asyncredisstorage.hpp"
24 #include "private/error.hpp"
25
26 using namespace shareddatalayer;
27 using namespace shareddatalayer::redis;
28 using namespace testing;
29
30 namespace
31 {
32     std::string getErrorCodeMessage(std::error_code ec)
33     {
34         return ec.message();
35     }
36
37     class ErrorCodesTest: public testing::Test
38     {
39     public:
40         ErrorCodesTest()
41         {
42         }
43
44         virtual ~ErrorCodesTest()
45         {
46         }
47     };
48 }
49
50 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesHaveCorrectDescriptionMessage)
51 {
52     std::error_code ec;
53
54     for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
55     {
56         switch (aec)
57         {
58             case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
59                 ec = aec;
60                 EXPECT_EQ(std::error_code().message(), getErrorCodeMessage(ec));
61                 break;
62             case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
63                 ec = aec;
64                 EXPECT_EQ("redis error", getErrorCodeMessage(ec));
65                 break;
66             case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
67                 ec = aec;
68                 EXPECT_EQ("redis connection lost", getErrorCodeMessage(ec));
69                 break;
70             case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
71                 ec = aec;
72                 EXPECT_EQ("redis protocol error", getErrorCodeMessage(ec));
73                 break;
74             case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
75                 ec = aec;
76                 EXPECT_EQ("redis out of memory", getErrorCodeMessage(ec));
77                 break;
78             case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
79                 ec = aec;
80                 EXPECT_EQ("redis dataset still being loaded into memory", getErrorCodeMessage(ec));
81                 break;
82             case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
83                 ec = aec;
84                 EXPECT_EQ("not connected to redis, SDL operation not started", getErrorCodeMessage(ec));
85                 break;
86             case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
87                 ec = aec;
88                 EXPECT_EQ("redis I/O error", getErrorCodeMessage(ec));
89                 break;
90             case AsyncRedisCommandDispatcherErrorCode::WRITING_TO_SLAVE:
91                 ec = aec;
92                 EXPECT_EQ("writing to slave", getErrorCodeMessage(ec));
93                 break;
94             case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
95                 ec = aec;
96                 EXPECT_EQ("unsupported error code for message()", getErrorCodeMessage(ec));
97                 break;
98             default:
99                 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
100                 break;
101         }
102     }
103 }
104
105 TEST_F(ErrorCodesTest, AllAsyncRedisCommandDispatcherErrorCodesAreMappedToCorrectSDLInternalError)
106 {
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).
109      */
110     std::error_code ec;
111
112     for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec != AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
113     {
114         switch (aec)
115         {
116             case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
117                 ec = aec;
118                 EXPECT_TRUE(ec == InternalError::SUCCESS);
119                 break;
120             case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
121                 ec = aec;
122                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
123                 break;
124             case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
125                 ec = aec;
126                 EXPECT_TRUE(ec == InternalError::BACKEND_CONNECTION_LOST);
127                 break;
128             case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
129                 ec = aec;
130                 EXPECT_TRUE(ec == InternalError::BACKEND_REJECTED_REQUEST);
131                 break;
132             case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
133                 ec = aec;
134                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
135                 break;
136             case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
137                 ec = aec;
138                 EXPECT_TRUE(ec == InternalError::BACKEND_NOT_READY);
139                 break;
140             case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
141                 ec = aec;
142                 EXPECT_TRUE(ec == InternalError::SDL_NOT_CONNECTED_TO_BACKEND);
143                 break;
144             case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
145                 ec = aec;
146                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
147                 break;
148             case AsyncRedisCommandDispatcherErrorCode::WRITING_TO_SLAVE:
149                 ec = aec;
150                 EXPECT_TRUE(ec == InternalError::BACKEND_ERROR);
151                 break;
152             case AsyncRedisCommandDispatcherErrorCode::END_MARKER:
153                 ec = aec;
154                 EXPECT_TRUE(ec == InternalError::SDL_ERROR_CODE_LOGIC_ERROR);
155                 break;
156             default:
157                 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
158                 break;
159         }
160     }
161 }
162
163 TEST_F(ErrorCodesTest, AllErrorCodeEnumsAreMappedToCorrectClientErrorCode)
164 {
165     std::error_code ec;
166
167     ec = std::error_code();
168     EXPECT_TRUE(ec == shareddatalayer::Error::SUCCESS);
169
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);
178
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);
197 }
198
199 TEST_F(ErrorCodesTest, ErrorCodeEnumsDoNotMapToIncorrectClientErrorCode)
200 {
201     std::error_code ec;
202
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);
211
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);
230 }
231
232 TEST_F(ErrorCodesTest, AllErrorCodeEnumClassesHaveCategory)
233 {
234     EXPECT_STREQ("asyncrediscommanddispatcher",
235                  std::error_code(AsyncRedisCommandDispatcherErrorCode::SUCCESS).category().name());
236
237     EXPECT_STREQ("asyncredisstorage",
238                  std::error_code(AsyncRedisStorage::ErrorCode::SUCCESS).category().name());
239 }