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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #include <gtest/gtest.h>
23 #include "private/error.hpp"
24 #include "private/redis/asyncredisstorage.hpp"
25 #include "private/syncstorageimpl.hpp"
26 #include "private/tst/asyncstoragemock.hpp"
27 #include "private/tst/systemmock.hpp"
28 #include <sdl/backenderror.hpp>
29 #include <sdl/invalidnamespace.hpp>
30 #include <sdl/notconnected.hpp>
31 #include <sdl/operationinterrupted.hpp>
32 #include <sdl/rejectedbybackend.hpp>
33 #include <sdl/rejectedbysdl.hpp>
35 using namespace shareddatalayer;
36 using namespace shareddatalayer::redis;
37 using namespace shareddatalayer::tst;
38 using namespace testing;
42 class SyncStorageImplTest: public testing::Test
45 std::unique_ptr<SyncStorageImpl> syncStorage;
46 /* AsyncStorageMock ownership will be passed to implementation. To be able to do verification
47 * with the mock object also here after its ownership is passed we take raw pointer to
48 * AsyncStorageMock before passing it to implementation. Works fine, as implementation will
49 * not release injected mock object before test case execution finishes
51 std::unique_ptr<StrictMock<AsyncStorageMock>> asyncStorageMockPassedToImplementation;
52 StrictMock<AsyncStorageMock>* asyncStorageMockRawPtr;
53 StrictMock<SystemMock> systemMock;
54 AsyncStorage::ModifyAck savedModifyAck;
55 AsyncStorage::ModifyIfAck savedModifyIfAck;
56 AsyncStorage::GetAck savedGetAck;
57 AsyncStorage::FindKeysAck savedFindKeysAck;
58 AsyncStorage::ReadyAck savedReadyAck;
60 SyncStorage::DataMap dataMap;
61 SyncStorage::Keys keys;
62 const SyncStorage::Namespace ns;
63 std::chrono::steady_clock::duration TEST_READY_WAIT_TIMEOUT;
64 std::chrono::steady_clock::duration TEST_OPERATION_WAIT_TIMEOUT;
65 int TEST_READY_POLL_WAIT_TIMEOUT;
66 int TEST_OPERATION_POLL_WAIT_TIMEOUT;
67 SyncStorageImplTest():
68 asyncStorageMockPassedToImplementation(new StrictMock<AsyncStorageMock>()),
69 asyncStorageMockRawPtr(asyncStorageMockPassedToImplementation.get()),
71 dataMap({{ "key1", { 0x0a, 0x0b, 0x0c } }, { "key2", { 0x0d, 0x0e, 0x0f, 0xff } }}),
72 keys({ "key1", "key2" }),
73 ns("someKnownNamespace"),
74 TEST_READY_WAIT_TIMEOUT(std::chrono::minutes(1)),
75 TEST_OPERATION_WAIT_TIMEOUT(std::chrono::seconds(1)),
76 TEST_READY_POLL_WAIT_TIMEOUT(std::chrono::duration_cast<std::chrono::milliseconds>(TEST_READY_WAIT_TIMEOUT).count() / 10),
77 TEST_OPERATION_POLL_WAIT_TIMEOUT(std::chrono::duration_cast<std::chrono::milliseconds>(TEST_OPERATION_WAIT_TIMEOUT).count() / 10)
79 expectConstructorCalls();
80 syncStorage.reset(new SyncStorageImpl(std::move(asyncStorageMockPassedToImplementation), systemMock));
83 ~SyncStorageImplTest()
85 syncStorage->setOperationTimeout(std::chrono::steady_clock::duration::zero());
88 void expectConstructorCalls()
91 EXPECT_CALL(*asyncStorageMockRawPtr, fd())
93 .WillOnce(Return(pFd));
96 void expectSdlReadinessCheck(int timeout)
99 expectPollForPendingEvents_ReturnNoEvents();
100 expectWaitReadyAsync();
101 expectPollWait(timeout);
102 expectHandleEvents_callWaitReadyAck();
105 void expectPollForPendingEvents_ReturnNoEvents()
107 EXPECT_CALL(systemMock, poll( _, 1, 0))
109 .WillOnce(Invoke([](struct pollfd *, nfds_t, int)
115 void expectPollWait(int timeout)
117 EXPECT_CALL(systemMock, poll( _, 1, timeout))
119 .WillOnce(Invoke([](struct pollfd *fds, nfds_t, int)
121 fds->revents = POLLIN;
126 void expectPollError()
128 EXPECT_CALL(systemMock, poll( _, 1, -1))
130 .WillOnce(Invoke([](struct pollfd *fds, nfds_t, int)
132 fds->revents = POLLIN;
137 void expectPollExceptionalCondition()
139 EXPECT_CALL(systemMock, poll( _, 1, -1))
141 .WillOnce(Invoke([](struct pollfd *fds, nfds_t, int)
143 fds->revents = POLLPRI;
148 void expectHandleEvents()
150 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
154 void expectHandleEvents_callWaitReadyAck()
156 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
158 .WillOnce(Invoke([this]()
160 savedReadyAck(std::error_code());
164 void expectHandleEvents_callWaitReadyAckWithError()
166 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
168 .WillOnce(Invoke([this]()
170 savedReadyAck(AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED);
174 void expectHandleEvents_callModifyAck()
176 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
178 .WillOnce(Invoke([this]()
180 savedModifyAck(std::error_code());
184 void expectWaitReadyAsync()
186 EXPECT_CALL(*asyncStorageMockRawPtr, waitReadyAsync(ns,_))
188 .WillOnce(SaveArg<1>(&savedReadyAck));
191 void expectModifyAckWithError()
193 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
195 .WillOnce(Invoke([this]()
197 savedModifyAck(AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY);
201 void expectModifyIfAck(const std::error_code& error, bool status)
203 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
205 .WillOnce(Invoke([this, error, status]()
207 savedModifyIfAck(error, status);
211 void expectGetAckWithError()
213 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
215 .WillOnce(Invoke([this]()
217 savedGetAck(AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY, dataMap);
223 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
225 .WillOnce(Invoke([this]()
227 savedGetAck(std::error_code(), dataMap);
231 void expectFindKeysAck()
233 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
235 .WillOnce(Invoke([this]()
237 savedFindKeysAck(std::error_code(), keys);
241 void expectFindKeysAckWithError()
243 EXPECT_CALL(*asyncStorageMockRawPtr, handleEvents())
245 .WillOnce(Invoke([this]()
247 savedFindKeysAck(AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY, keys);
251 void expectSetAsync(const SyncStorage::DataMap& dataMap)
253 EXPECT_CALL(*asyncStorageMockRawPtr, setAsync(ns, dataMap, _))
255 .WillOnce(SaveArg<2>(&savedModifyAck));
258 void expectSetIfAsync(const SyncStorage::Key& key, const SyncStorage::Data& oldData, const SyncStorage::Data& newData)
260 EXPECT_CALL(*asyncStorageMockRawPtr, setIfAsync(ns, key, oldData, newData, _))
262 .WillOnce(SaveArg<4>(&savedModifyIfAck));
265 void expectGetAsync(const SyncStorage::Keys& keys)
267 EXPECT_CALL(*asyncStorageMockRawPtr, getAsync(ns, keys, _))
269 .WillOnce(SaveArg<2>(&savedGetAck));
272 void expectFindKeysAsync()
274 EXPECT_CALL(*asyncStorageMockRawPtr, findKeysAsync(ns, _, _))
276 .WillOnce(SaveArg<2>(&savedFindKeysAck));
279 void expectListKeys()
281 EXPECT_CALL(*asyncStorageMockRawPtr, listKeys(ns, _, _))
283 .WillOnce(SaveArg<2>(&savedFindKeysAck));
286 void expectRemoveAsync(const SyncStorage::Keys& keys)
288 EXPECT_CALL(*asyncStorageMockRawPtr, removeAsync(ns, keys, _))
290 .WillOnce(SaveArg<2>(&savedModifyAck));
293 void expectRemoveIfAsync(const SyncStorage::Key& key, const SyncStorage::Data& data)
295 EXPECT_CALL(*asyncStorageMockRawPtr, removeIfAsync(ns, key, data, _))
297 .WillOnce(SaveArg<3>(&savedModifyIfAck));
300 void expectRemoveAllAsync()
302 EXPECT_CALL(*asyncStorageMockRawPtr, removeAllAsync(ns, _))
304 .WillOnce(SaveArg<1>(&savedModifyAck));
307 void expectSetIfNotExistsAsync(const SyncStorage::Key& key, const SyncStorage::Data& data)
309 EXPECT_CALL(*asyncStorageMockRawPtr, setIfNotExistsAsync(ns, key, data, _))
311 .WillOnce(SaveArg<3>(&savedModifyIfAck));
316 TEST_F(SyncStorageImplTest, IsNotCopyable)
319 EXPECT_FALSE(std::is_copy_constructible<SyncStorageImpl>::value);
320 EXPECT_FALSE(std::is_copy_assignable<SyncStorageImpl>::value);
323 TEST_F(SyncStorageImplTest, ImplementssyncStorage)
326 EXPECT_TRUE((std::is_base_of<SyncStorage, SyncStorageImpl>::value));
329 TEST_F(SyncStorageImplTest, EventsAreNotHandledWhenPollReturnsError)
332 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
333 expectSetAsync(dataMap);
335 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
336 expectHandleEvents_callModifyAck();
337 syncStorage->set(ns, dataMap);
340 TEST_F(SyncStorageImplTest, EventsAreNotHandledWhenThereIsAnExceptionalConditionOnTheFd)
343 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
344 expectSetAsync(dataMap);
345 expectPollExceptionalCondition();
346 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
347 expectHandleEvents_callModifyAck();
348 syncStorage->set(ns, dataMap);
351 TEST_F(SyncStorageImplTest, WaitReadySuccessfully)
354 expectWaitReadyAsync();
355 expectPollWait(TEST_READY_POLL_WAIT_TIMEOUT);
356 expectHandleEvents_callWaitReadyAck();
357 syncStorage->waitReady(ns, TEST_READY_WAIT_TIMEOUT);
360 TEST_F(SyncStorageImplTest, WaitReadyCanThrowRejectedBySdl)
363 expectWaitReadyAsync();
364 EXPECT_THROW(syncStorage->waitReady(ns, std::chrono::nanoseconds(1)), RejectedBySdl);
367 TEST_F(SyncStorageImplTest, WaitReadyCanThrowNotConnected)
370 expectWaitReadyAsync();
371 expectPollWait(TEST_READY_POLL_WAIT_TIMEOUT);
372 expectHandleEvents_callWaitReadyAckWithError();
373 EXPECT_THROW(syncStorage->waitReady(ns, TEST_READY_WAIT_TIMEOUT), NotConnected);
376 TEST_F(SyncStorageImplTest, SetSuccessfully)
379 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
380 expectSetAsync(dataMap);
381 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
382 expectHandleEvents_callModifyAck();
383 syncStorage->set(ns, dataMap);
386 TEST_F(SyncStorageImplTest, SetWithReadinessTimeoutSuccessfully)
389 expectSdlReadinessCheck(TEST_OPERATION_POLL_WAIT_TIMEOUT);
390 expectSetAsync(dataMap);
391 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
392 expectHandleEvents_callModifyAck();
393 syncStorage->setOperationTimeout(TEST_OPERATION_WAIT_TIMEOUT);
394 syncStorage->set(ns, dataMap);
397 TEST_F(SyncStorageImplTest, SetCanThrowBackendError)
400 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
401 expectSetAsync(dataMap);
402 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
403 expectModifyAckWithError();
404 EXPECT_THROW(syncStorage->set(ns, dataMap), BackendError);
407 TEST_F(SyncStorageImplTest, SetIfSuccessfully)
410 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
411 expectSetAsync(dataMap);
412 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
413 expectHandleEvents_callModifyAck();
414 syncStorage->set(ns, dataMap);
415 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
416 expectSetIfAsync("key1", { 0x0a, 0x0b, 0x0c }, { 0x0d, 0x0e, 0x0f });
417 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
418 expectHandleEvents_callModifyAck();
419 syncStorage->setIf(ns, "key1", { 0x0a, 0x0b, 0x0c }, { 0x0d, 0x0e, 0x0f });
422 TEST_F(SyncStorageImplTest, SetIfWithReadinessTimeoutSuccessfully)
425 expectSdlReadinessCheck(TEST_OPERATION_POLL_WAIT_TIMEOUT);
426 expectSetAsync(dataMap);
427 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
428 expectHandleEvents_callModifyAck();
429 syncStorage->setOperationTimeout(TEST_OPERATION_WAIT_TIMEOUT);
430 syncStorage->set(ns, dataMap);
431 expectSdlReadinessCheck(TEST_OPERATION_POLL_WAIT_TIMEOUT);
432 expectSetIfAsync("key1", { 0x0a, 0x0b, 0x0c }, { 0x0d, 0x0e, 0x0f });
433 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
434 expectHandleEvents_callModifyAck();
435 syncStorage->setIf(ns, "key1", { 0x0a, 0x0b, 0x0c }, { 0x0d, 0x0e, 0x0f });
438 TEST_F(SyncStorageImplTest, SetIfCanThrowBackendError)
441 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
442 expectSetAsync(dataMap);
443 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
444 expectHandleEvents_callModifyAck();
445 syncStorage->set(ns, dataMap);
446 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
447 expectSetIfAsync("key1", { 0x0a, 0x0b, 0x0c }, { 0x0d, 0x0e, 0x0f });
448 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
449 expectModifyIfAck(AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY, false);
450 EXPECT_THROW(syncStorage->setIf(ns, "key1", { 0x0a, 0x0b, 0x0c }, { 0x0d, 0x0e, 0x0f }), BackendError);
453 TEST_F(SyncStorageImplTest, SetIfNotExistsSuccessfully)
456 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
457 expectSetIfNotExistsAsync("key1", { 0x0a, 0x0b, 0x0c });
458 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
459 expectModifyIfAck(std::error_code(), true);
460 EXPECT_TRUE(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }));
463 TEST_F(SyncStorageImplTest, SetIfNotExistsIfWithReadinessTimeoutSuccessfully)
466 expectSdlReadinessCheck(TEST_OPERATION_POLL_WAIT_TIMEOUT);
467 expectSetIfNotExistsAsync("key1", { 0x0a, 0x0b, 0x0c });
468 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
469 expectModifyIfAck(std::error_code(), true);
470 syncStorage->setOperationTimeout(TEST_OPERATION_WAIT_TIMEOUT);
471 EXPECT_TRUE(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }));
474 TEST_F(SyncStorageImplTest, SetIfNotExistsReturnsFalseIfKeyAlreadyExists)
477 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
478 expectSetIfNotExistsAsync("key1", { 0x0a, 0x0b, 0x0c });
479 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
480 expectModifyIfAck(std::error_code(), false);
481 EXPECT_FALSE(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }));
484 TEST_F(SyncStorageImplTest, SetIfNotExistsCanThrowBackendError)
487 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
488 expectSetIfNotExistsAsync("key1", { 0x0a, 0x0b, 0x0c });
489 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
490 expectModifyIfAck(AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY, false);
491 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), BackendError);
494 TEST_F(SyncStorageImplTest, GetSuccessfully)
497 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
498 expectGetAsync(keys);
499 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
501 auto map(syncStorage->get(ns, keys));
502 EXPECT_EQ(map, dataMap);
505 TEST_F(SyncStorageImplTest, GetWithReadinessTimeoutSuccessfully)
508 expectSdlReadinessCheck(TEST_OPERATION_POLL_WAIT_TIMEOUT);
509 expectGetAsync(keys);
510 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
512 syncStorage->setOperationTimeout(TEST_OPERATION_WAIT_TIMEOUT);
513 auto map(syncStorage->get(ns, keys));
514 EXPECT_EQ(map, dataMap);
517 TEST_F(SyncStorageImplTest, GetCanThrowBackendError)
520 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
521 expectGetAsync(keys);
522 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
523 expectGetAckWithError();
524 EXPECT_THROW(syncStorage->get(ns, keys), BackendError);
527 TEST_F(SyncStorageImplTest, RemoveSuccessfully)
530 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
531 expectRemoveAsync(keys);
532 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
533 expectHandleEvents_callModifyAck();
534 syncStorage->remove(ns, keys);
537 TEST_F(SyncStorageImplTest, RemoveWithReadinessTimeoutSuccessfully)
540 expectSdlReadinessCheck(TEST_OPERATION_POLL_WAIT_TIMEOUT);
541 expectRemoveAsync(keys);
542 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
543 expectHandleEvents_callModifyAck();
544 syncStorage->setOperationTimeout(TEST_OPERATION_WAIT_TIMEOUT);
545 syncStorage->remove(ns, keys);
548 TEST_F(SyncStorageImplTest, RemoveCanThrowBackendError)
551 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
552 expectRemoveAsync(keys);
553 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
554 expectModifyAckWithError();
555 EXPECT_THROW(syncStorage->remove(ns, keys), BackendError);
558 TEST_F(SyncStorageImplTest, RemoveIfSuccessfully)
561 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
562 expectRemoveIfAsync("key1", { 0x0a, 0x0b, 0x0c });
563 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
564 expectModifyIfAck(std::error_code(), true);
565 EXPECT_TRUE(syncStorage->removeIf(ns, "key1", { 0x0a, 0x0b, 0x0c }));
568 TEST_F(SyncStorageImplTest, RemoveIfWithReadinessTimeoutSuccessfully)
571 expectSdlReadinessCheck(TEST_OPERATION_POLL_WAIT_TIMEOUT);
572 expectRemoveIfAsync("key1", { 0x0a, 0x0b, 0x0c });
573 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
574 expectModifyIfAck(std::error_code(), true);
575 syncStorage->setOperationTimeout(TEST_OPERATION_WAIT_TIMEOUT);
576 EXPECT_TRUE(syncStorage->removeIf(ns, "key1", { 0x0a, 0x0b, 0x0c }));
579 TEST_F(SyncStorageImplTest, RemoveIfReturnsFalseIfKeyDoesnotMatch)
582 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
583 expectRemoveIfAsync("key1", { 0x0a, 0x0b, 0x0c });
584 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
585 expectModifyIfAck(std::error_code(), false);
586 EXPECT_FALSE(syncStorage->removeIf(ns, "key1", { 0x0a, 0x0b, 0x0c }));
589 TEST_F(SyncStorageImplTest, RemoveIfCanThrowBackendError)
592 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
593 expectRemoveIfAsync("key1", { 0x0a, 0x0b, 0x0c });
594 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
595 expectModifyIfAck(AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY, false);
596 EXPECT_THROW(syncStorage->removeIf(ns, "key1", { 0x0a, 0x0b, 0x0c }), BackendError);
599 TEST_F(SyncStorageImplTest, FindKeysSuccessfully)
602 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
603 expectFindKeysAsync();
604 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
606 auto ids(syncStorage->findKeys(ns, "*"));
607 EXPECT_EQ(ids, keys);
610 TEST_F(SyncStorageImplTest, ListKeysSuccessfully)
613 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
615 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
617 auto ids(syncStorage->listKeys(ns, "*"));
618 EXPECT_EQ(ids, keys);
621 TEST_F(SyncStorageImplTest, FindKeysWithReadinessTimeoutSuccessfully)
624 expectSdlReadinessCheck(TEST_OPERATION_POLL_WAIT_TIMEOUT);
625 expectFindKeysAsync();
626 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
628 syncStorage->setOperationTimeout(TEST_OPERATION_WAIT_TIMEOUT);
629 auto ids(syncStorage->findKeys(ns, "*"));
630 EXPECT_EQ(ids, keys);
633 TEST_F(SyncStorageImplTest, FindKeysAckCanThrowBackendError)
636 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
637 expectFindKeysAsync();
638 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
639 expectFindKeysAckWithError();
640 EXPECT_THROW(syncStorage->findKeys(ns, "*"), BackendError);
643 TEST_F(SyncStorageImplTest, RemoveAllSuccessfully)
646 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
647 expectRemoveAllAsync();
648 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
649 expectHandleEvents_callModifyAck();
650 syncStorage->removeAll(ns);
653 TEST_F(SyncStorageImplTest, RemoveAllWithReadinessTimeoutSuccessfully)
656 expectSdlReadinessCheck(TEST_OPERATION_POLL_WAIT_TIMEOUT);
657 expectRemoveAllAsync();
658 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
659 expectHandleEvents_callModifyAck();
660 syncStorage->setOperationTimeout(TEST_OPERATION_WAIT_TIMEOUT);
661 syncStorage->removeAll(ns);
664 TEST_F(SyncStorageImplTest, RemoveAllCanThrowBackendError)
667 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
668 expectRemoveAllAsync();
669 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
670 expectModifyAckWithError();
671 EXPECT_THROW(syncStorage->removeAll(ns), BackendError);
674 TEST_F(SyncStorageImplTest, AllAsyncRedisStorageErrorCodesThrowCorrectException)
679 for (AsyncRedisStorage::ErrorCode arsec = AsyncRedisStorage::ErrorCode::SUCCESS; arsec < AsyncRedisStorage::ErrorCode::END_MARKER; ++arsec)
681 if (arsec != AsyncRedisStorage::ErrorCode::SUCCESS)
683 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
684 expectSetIfNotExistsAsync("key1", { 0x0a, 0x0b, 0x0c });
685 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
690 case AsyncRedisStorage::ErrorCode::SUCCESS:
692 case AsyncRedisStorage::ErrorCode::INVALID_NAMESPACE:
693 expectModifyIfAck(arsec, false);
694 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), InvalidNamespace);
696 case AsyncRedisStorage::ErrorCode::REDIS_NOT_YET_DISCOVERED:
697 expectModifyIfAck(arsec, false);
698 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), NotConnected);
701 FAIL() << "No mapping for AsyncRedisStorage::ErrorCode value: " << arsec;
707 TEST_F(SyncStorageImplTest, AllDispatcherErrorCodesThrowCorrectException)
712 for (AsyncRedisCommandDispatcherErrorCode aec = AsyncRedisCommandDispatcherErrorCode::SUCCESS; aec < AsyncRedisCommandDispatcherErrorCode::END_MARKER; ++aec)
714 if (aec != AsyncRedisCommandDispatcherErrorCode::SUCCESS)
716 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
717 expectSetIfNotExistsAsync("key1", { 0x0a, 0x0b, 0x0c });
718 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
723 case AsyncRedisCommandDispatcherErrorCode::SUCCESS:
725 case AsyncRedisCommandDispatcherErrorCode::UNKNOWN_ERROR:
726 expectModifyIfAck(aec, false);
727 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), BackendError);
729 case AsyncRedisCommandDispatcherErrorCode::CONNECTION_LOST:
730 expectModifyIfAck(aec, false);
731 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), OperationInterrupted);
733 case AsyncRedisCommandDispatcherErrorCode::PROTOCOL_ERROR:
734 expectModifyIfAck(aec, false);
735 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), RejectedByBackend);
737 case AsyncRedisCommandDispatcherErrorCode::OUT_OF_MEMORY:
738 expectModifyIfAck(aec, false);
739 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), BackendError);
741 case AsyncRedisCommandDispatcherErrorCode::DATASET_LOADING:
742 expectModifyIfAck(aec, false);
743 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), NotConnected);
745 case AsyncRedisCommandDispatcherErrorCode::NOT_CONNECTED:
746 expectModifyIfAck(aec, false);
747 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), NotConnected);
749 case AsyncRedisCommandDispatcherErrorCode::IO_ERROR:
750 expectModifyIfAck(aec, false);
751 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), BackendError);
753 case AsyncRedisCommandDispatcherErrorCode::WRITING_TO_SLAVE:
754 expectModifyIfAck(aec, false);
755 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), BackendError);
758 FAIL() << "No mapping for AsyncRedisCommandDispatcherErrorCode value: " << aec;
764 TEST_F(SyncStorageImplTest, CanThrowStdExceptionIfDispatcherErrorCodeCannotBeMappedToSdlException)
767 expectSdlReadinessCheck(SyncStorageImpl::NO_TIMEOUT);
768 expectSetIfNotExistsAsync("key1", { 0x0a, 0x0b, 0x0c });
769 expectPollWait(SyncStorageImpl::NO_TIMEOUT);
770 expectModifyIfAck(std::error_code(1, std::system_category()), false);
771 EXPECT_THROW(syncStorage->setIfNotExists(ns, "key1", { 0x0a, 0x0b, 0x0c }), std::range_error);