blob: 34e1ed20cc59e01ae9a0214f9ea17d1be115854b [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
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
Timo Tietavainena0745d22019-11-28 09:55:22 +020017/*
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
20*/
21
Rolf Badorekef2bf512019-08-20 11:17:15 +030022#include <type_traits>
23#include <memory>
24#include <sys/epoll.h>
25#include <async.h>
26#include <gtest/gtest.h>
27#include "private/redis/hiredisepolladapter.hpp"
28#include "private/tst/enginemock.hpp"
29#include "private/tst/hiredissystemmock.hpp"
30
31using namespace shareddatalayer;
32using namespace shareddatalayer::redis;
33using namespace shareddatalayer::tst;
34using namespace testing;
35
36namespace
37{
38 class HiredisEpollAdapterTest: public testing::Test
39 {
40 public:
41 StrictMock<EngineMock> engineMock;
42 StrictMock<HiredisSystemMock> hiredisSystemMock;
43 redisAsyncContext ac;
44 int acFd;
45 std::unique_ptr<HiredisEpollAdapter> adapter;
46 Engine::EventHandler savedEventHandler;
47
48 HiredisEpollAdapterTest():
49 ac { },
50 acFd(10)
51 {
52 ac.c.fd = acFd;
53 adapter.reset(new HiredisEpollAdapter(engineMock, hiredisSystemMock));
54 }
55
56 void expectAddMonitoredFD(int fd, unsigned int events)
57 {
58 EXPECT_CALL(engineMock, addMonitoredFD(fd,events,_))
59 .Times(1)
60 .WillOnce(SaveArg<2>(&savedEventHandler));
61 }
62
63 void expectModifyMonitoredFD(int fd, unsigned int events)
64 {
65 EXPECT_CALL(engineMock, modifyMonitoredFD(fd,events))
66 .Times(1);
67 }
68
69 void expectDeleteMonitoredFD(int fd)
70 {
71 EXPECT_CALL(engineMock, deleteMonitoredFD(fd))
72 .Times(1);
73 }
74 };
75
76 class HiredisEpollAdapterAttachedTest: public HiredisEpollAdapterTest
77 {
78 public:
79 HiredisEpollAdapterAttachedTest()
80 {
81 expectAddMonitoredFD(ac.c.fd, 0);
82 adapter->attach(&ac);
83 }
84
85 ~HiredisEpollAdapterAttachedTest()
86 {
87 expectDeleteMonitoredFD(ac.c.fd);
88 }
89 };
90}
91
92TEST_F(HiredisEpollAdapterTest, IsNotCopyable)
93{
94 EXPECT_FALSE(std::is_copy_constructible<HiredisEpollAdapter>::value);
95 EXPECT_FALSE(std::is_copy_assignable<HiredisEpollAdapter>::value);
96}
97
98TEST_F(HiredisEpollAdapterAttachedTest, CanHandleInputEvents)
99{
100 InSequence dummy;
101 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_IN);
102 ac.ev.addRead(ac.ev.data);
103 EXPECT_CALL(hiredisSystemMock, redisAsyncHandleRead(&ac))
104 .Times(1);
105 savedEventHandler(EngineMock::EVENT_IN);
106}
107
108TEST_F(HiredisEpollAdapterAttachedTest, DoesNotHandleInputEventIfNotReading)
109{
110 InSequence dummy;
111 EXPECT_CALL(hiredisSystemMock, redisAsyncHandleRead(&ac))
112 .Times(0);
113 savedEventHandler(EngineMock::EVENT_IN);
114}
115
116TEST_F(HiredisEpollAdapterAttachedTest, CanHandleOutputEvents)
117{
118 InSequence dummy;
119 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_OUT);
120 ac.ev.addWrite(ac.ev.data);
121 EXPECT_CALL(hiredisSystemMock, redisAsyncHandleWrite(&ac))
122 .Times(1);
123 savedEventHandler(EngineMock::EVENT_OUT);
124}
125
126TEST_F(HiredisEpollAdapterAttachedTest, DoesNotHandleOutputEventIfNotWriting)
127{
128 InSequence dummy;
129 EXPECT_CALL(hiredisSystemMock, redisAsyncHandleWrite(&ac))
130 .Times(0);
131 savedEventHandler(EngineMock::EVENT_OUT);
132}
133
134TEST_F(HiredisEpollAdapterAttachedTest, CanBeAttachedAndEventStateChangedIdempotently)
135{
136 InSequence dummy;
137 EXPECT_EQ(adapter.get(), ac.ev.data);
138 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_IN);
139 ac.ev.addRead(ac.ev.data);
140 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_IN | EngineMock::EVENT_OUT);
141 ac.ev.addWrite(ac.ev.data);
142 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_OUT);
143 ac.ev.delRead(ac.ev.data);
144 expectModifyMonitoredFD(ac.c.fd, 0);
145 ac.ev.delWrite(ac.ev.data);
146 expectModifyMonitoredFD(ac.c.fd,EngineMock::EVENT_IN);
147 ac.ev.addRead(ac.ev.data);
148 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_IN | EngineMock::EVENT_OUT);
149 ac.ev.addWrite(ac.ev.data);
150 expectDeleteMonitoredFD(ac.c.fd);
151 ac.ev.cleanup(ac.ev.data);
152 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_OUT);
153 ac.ev.addWrite(ac.ev.data);
154 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_IN | EngineMock::EVENT_OUT);
155 ac.ev.addRead(ac.ev.data);
156 expectAddMonitoredFD(ac.c.fd, 0);
157 adapter->attach(&ac);
158}
159
160TEST_F(HiredisEpollAdapterAttachedTest, FurtherAttachementsResetEventState)
161{
162 InSequence dummy;
163 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_IN);
164 ac.ev.addRead(ac.ev.data);
165 expectAddMonitoredFD(ac.c.fd, 0);
166 adapter->attach(&ac);
167 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_OUT);
168 ac.ev.addWrite(ac.ev.data);
169}
170
171TEST_F(HiredisEpollAdapterAttachedTest, InputEventIsSetOnce)
172{
173 InSequence dummy;
174 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_IN);
175 ac.ev.addRead(ac.ev.data);
176 ac.ev.addRead(ac.ev.data);
177}
178
179TEST_F(HiredisEpollAdapterAttachedTest, OutputEventIsSetOnce)
180{
181 InSequence dummy;
182 expectModifyMonitoredFD(ac.c.fd, EngineMock::EVENT_OUT);
183 ac.ev.addWrite(ac.ev.data);
184 ac.ev.addWrite(ac.ev.data);
185}
186
187TEST_F(HiredisEpollAdapterAttachedTest, MakesCleanupIfDestructedWhileAttahced)
188{
189 InSequence dummy;
190 expectDeleteMonitoredFD(ac.c.fd);
191 adapter.reset(new HiredisEpollAdapter(engineMock, hiredisSystemMock));
192 expectAddMonitoredFD(ac.c.fd, 0);
193 adapter->attach(&ac);
194}
195
196TEST_F(HiredisEpollAdapterTest, DoesNotMakeCleanupIfDestructedWhileNotAttahced)
197{
198 InSequence dummy;
199 adapter.reset(new HiredisEpollAdapter(engineMock, hiredisSystemMock));
200}