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 <gtest/gtest.h>
18 #include <type_traits>
20 #include <sys/eventfd.h>
21 #include "private/asyncdummystorage.hpp"
22 #include "private/tst/enginemock.hpp"
24 using namespace shareddatalayer;
25 using namespace shareddatalayer::tst;
26 using namespace testing;
30 class AsyncDummyStorageTest: public testing::Test
33 std::shared_ptr<StrictMock<EngineMock>> engineMock;
35 AsyncStorage::Namespace ns;
36 Engine::Callback storedCallback;
37 std::unique_ptr<AsyncDummyStorage> dummyStorage;
39 AsyncDummyStorageTest():
40 engineMock(std::make_shared<StrictMock<EngineMock>>()),
42 ns("someKnownNamespace")
44 dummyStorage.reset(new AsyncDummyStorage(engineMock));
47 MOCK_METHOD1(ack1, void(const std::error_code&));
49 MOCK_METHOD2(ack2, void(const std::error_code&, bool));
51 MOCK_METHOD2(ack3, void(const std::error_code&, const AsyncStorage::DataMap&));
53 MOCK_METHOD2(ack4, void(const std::error_code&, const AsyncStorage::Keys&));
57 EXPECT_CALL(*this, ack1(std::error_code()))
63 EXPECT_CALL(*this, ack2(std::error_code(), true))
69 EXPECT_CALL(*this, ack3(std::error_code(), IsEmpty()))
75 EXPECT_CALL(*this, ack4(std::error_code(), IsEmpty()))
79 void expectPostCallback()
81 EXPECT_CALL(*engineMock, postCallback(_))
83 .WillOnce(SaveArg<0>(&storedCallback));
88 TEST_F(AsyncDummyStorageTest, IsNotCopyableAndIsNotMovable)
90 EXPECT_FALSE(std::is_copy_assignable<AsyncDummyStorage>::value);
91 EXPECT_FALSE(std::is_move_assignable<AsyncDummyStorage>::value);
92 EXPECT_FALSE(std::is_copy_constructible<AsyncDummyStorage>::value);
93 EXPECT_FALSE(std::is_move_constructible<AsyncDummyStorage>::value);
96 TEST_F(AsyncDummyStorageTest, ImplementsAsyncStorage)
98 EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncDummyStorage>::value));
101 TEST_F(AsyncDummyStorageTest, CanGetFd)
103 EXPECT_CALL(*engineMock, fd())
105 .WillOnce(Return(fd));
106 EXPECT_EQ(fd, dummyStorage->fd());
109 TEST_F(AsyncDummyStorageTest, CanHandleEvents)
111 EXPECT_CALL(*engineMock, handleEvents())
113 dummyStorage->handleEvents();
116 TEST_F(AsyncDummyStorageTest, AcksAreImmediatelyScheduled)
120 expectPostCallback();
121 dummyStorage->waitReadyAsync(ns, std::bind(&AsyncDummyStorageTest::ack1,
123 std::placeholders::_1));
127 expectPostCallback();
128 dummyStorage->setAsync(ns, { }, std::bind(&AsyncDummyStorageTest::ack1,
130 std::placeholders::_1));
134 expectPostCallback();
135 dummyStorage->setIfAsync(ns, { }, { }, { }, std::bind(&AsyncDummyStorageTest::ack2,
137 std::placeholders::_1,
138 std::placeholders::_2));
142 expectPostCallback();
143 dummyStorage->setIfNotExistsAsync(ns, { }, { }, std::bind(&AsyncDummyStorageTest::ack2,
145 std::placeholders::_1,
146 std::placeholders::_2));
150 expectPostCallback();
151 dummyStorage->getAsync(ns, { }, std::bind(&AsyncDummyStorageTest::ack3,
153 std::placeholders::_1,
154 std::placeholders::_2));
158 expectPostCallback();
159 dummyStorage->removeAsync(ns, { }, std::bind(&AsyncDummyStorageTest::ack1,
161 std::placeholders::_1));
165 expectPostCallback();
166 dummyStorage->removeIfAsync(ns, { }, { }, std::bind(&AsyncDummyStorageTest::ack2,
168 std::placeholders::_1,
169 std::placeholders::_2));
173 expectPostCallback();
174 dummyStorage->findKeysAsync(ns,
176 std::bind(&AsyncDummyStorageTest::ack4,
178 std::placeholders::_1,
179 std::placeholders::_2));
183 expectPostCallback();
184 dummyStorage->removeAllAsync(ns, std::bind(&AsyncDummyStorageTest::ack1,
186 std::placeholders::_1));