Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 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 | |
Timo Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 22 | #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 | |
| 31 | using namespace shareddatalayer; |
| 32 | using namespace shareddatalayer::redis; |
| 33 | using namespace shareddatalayer::tst; |
| 34 | using namespace testing; |
| 35 | |
| 36 | namespace |
| 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 | |
| 92 | TEST_F(HiredisEpollAdapterTest, IsNotCopyable) |
| 93 | { |
| 94 | EXPECT_FALSE(std::is_copy_constructible<HiredisEpollAdapter>::value); |
| 95 | EXPECT_FALSE(std::is_copy_assignable<HiredisEpollAdapter>::value); |
| 96 | } |
| 97 | |
| 98 | TEST_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 | |
| 108 | TEST_F(HiredisEpollAdapterAttachedTest, DoesNotHandleInputEventIfNotReading) |
| 109 | { |
| 110 | InSequence dummy; |
| 111 | EXPECT_CALL(hiredisSystemMock, redisAsyncHandleRead(&ac)) |
| 112 | .Times(0); |
| 113 | savedEventHandler(EngineMock::EVENT_IN); |
| 114 | } |
| 115 | |
| 116 | TEST_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 | |
| 126 | TEST_F(HiredisEpollAdapterAttachedTest, DoesNotHandleOutputEventIfNotWriting) |
| 127 | { |
| 128 | InSequence dummy; |
| 129 | EXPECT_CALL(hiredisSystemMock, redisAsyncHandleWrite(&ac)) |
| 130 | .Times(0); |
| 131 | savedEventHandler(EngineMock::EVENT_OUT); |
| 132 | } |
| 133 | |
| 134 | TEST_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 | |
| 160 | TEST_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 | |
| 171 | TEST_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 | |
| 179 | TEST_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 | |
| 187 | TEST_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 | |
| 196 | TEST_F(HiredisEpollAdapterTest, DoesNotMakeCleanupIfDestructedWhileNotAttahced) |
| 197 | { |
| 198 | InSequence dummy; |
| 199 | adapter.reset(new HiredisEpollAdapter(engineMock, hiredisSystemMock)); |
| 200 | } |