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 <gmock/gmock.h> |
| 25 | #include "private/timer.hpp" |
| 26 | #include "private/tst/enginemock.hpp" |
| 27 | |
| 28 | using namespace shareddatalayer; |
| 29 | using namespace shareddatalayer::tst; |
| 30 | using namespace testing; |
| 31 | |
| 32 | namespace |
| 33 | { |
| 34 | class TimerTest: public testing::Test |
| 35 | { |
| 36 | public: |
| 37 | NiceMock<EngineMock> engineMock; |
| 38 | const Timer::Duration duration; |
| 39 | std::unique_ptr<Timer> timer; |
| 40 | |
| 41 | TimerTest(): |
| 42 | duration(std::chrono::duration_cast<Timer::Duration>(std::chrono::seconds(12))), |
| 43 | timer(new Timer(engineMock)) |
| 44 | { |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | void nop() { } |
| 49 | } |
| 50 | |
| 51 | TEST_F(TimerTest, IsNotCopyableAndIsNotMovable) |
| 52 | { |
| 53 | EXPECT_FALSE(std::is_copy_assignable<Timer>::value); |
| 54 | EXPECT_FALSE(std::is_move_assignable<Timer>::value); |
| 55 | EXPECT_FALSE(std::is_copy_constructible<Timer>::value); |
| 56 | EXPECT_FALSE(std::is_move_constructible<Timer>::value); |
| 57 | } |
| 58 | |
| 59 | TEST_F(TimerTest, ArmCallsArmOfAssociatedTimerService) |
| 60 | { |
| 61 | EXPECT_CALL(engineMock, armTimer(_, duration, _)) |
| 62 | .Times(1); |
| 63 | timer->arm(duration, nop); |
| 64 | } |
| 65 | |
| 66 | TEST_F(TimerTest, DisarmingCallsDisarmOfAssociatedTimerService) |
| 67 | { |
| 68 | timer->arm(duration, nop); |
| 69 | EXPECT_CALL(engineMock, disarmTimer(_)) |
| 70 | .Times(1); |
| 71 | timer->disarm(); |
| 72 | } |
| 73 | |
| 74 | TEST_F(TimerTest, DisarmingUnArmedTimerDoesNothing) |
| 75 | { |
| 76 | EXPECT_CALL(engineMock, disarmTimer(_)) |
| 77 | .Times(0); |
| 78 | timer->disarm(); |
| 79 | } |
| 80 | |
| 81 | TEST_F(TimerTest, DoubleDisarmingDoesNothing) |
| 82 | { |
| 83 | timer->arm(duration, nop); |
| 84 | EXPECT_CALL(engineMock, disarmTimer(_)) |
| 85 | .Times(1); |
| 86 | timer->disarm(); |
| 87 | timer->disarm(); |
| 88 | } |
| 89 | |
| 90 | TEST_F(TimerTest, DoubleArmingDisarmsFirst) |
| 91 | { |
| 92 | InSequence dummy; |
| 93 | EXPECT_CALL(engineMock, armTimer(_, duration, _)) |
| 94 | .Times(1); |
| 95 | timer->arm(duration, nop); |
| 96 | EXPECT_CALL(engineMock, disarmTimer(_)) |
| 97 | .Times(1); |
| 98 | EXPECT_CALL(engineMock, armTimer(_, duration, _)) |
| 99 | .Times(1); |
| 100 | timer->arm(duration, nop); |
| 101 | Mock::VerifyAndClear(&engineMock); |
| 102 | } |
| 103 | |
| 104 | TEST_F(TimerTest, DestructorDisarms) |
| 105 | { |
| 106 | timer->arm(duration, nop); |
| 107 | EXPECT_CALL(engineMock, disarmTimer(_)) |
| 108 | .Times(1); |
| 109 | timer.reset(); |
| 110 | } |
| 111 | |
| 112 | TEST_F(TimerTest, AfterTimerHasExpiredItIsNotArmedAnymore) |
| 113 | { |
| 114 | Timer::Callback savedCb; |
| 115 | ON_CALL(engineMock, armTimer(_, _, _)) |
| 116 | .WillByDefault(SaveArg<2>(&savedCb)); |
| 117 | timer->arm(duration, nop); |
| 118 | savedCb(); |
| 119 | EXPECT_FALSE(timer->isArmed()); |
| 120 | } |
| 121 | |
| 122 | TEST_F(TimerTest, ArmingNullCallbackCallsSHAREDDATALAYER_ABORT) |
| 123 | { |
| 124 | EXPECT_EXIT(timer->arm(duration, Timer::Callback()), |
| 125 | KilledBySignal(SIGABRT), "timer\\.cpp"); |
| 126 | } |