Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 2 | #include "helpers.hpp" |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 3 | #include "mocks/sensor_listener_mock.hpp" |
| 4 | #include "sensor.hpp" |
| 5 | #include "sensor_cache.hpp" |
| 6 | #include "stubs/dbus_sensor_object.hpp" |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 7 | |
| 8 | #include <sdbusplus/asio/property.hpp> |
| 9 | |
| 10 | #include <thread> |
| 11 | |
| 12 | #include <gmock/gmock.h> |
| 13 | |
| 14 | using namespace testing; |
| 15 | using namespace std::chrono_literals; |
| 16 | |
| 17 | class TestSensor : public Test |
| 18 | { |
| 19 | public: |
| 20 | void SetUp() override |
| 21 | { |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 22 | sensorObject->setValue(42.7); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | void TearDown() override |
| 26 | { |
| 27 | DbusEnvironment::synchronizeIoc(); |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | registerForUpdates(std::shared_ptr<interfaces::SensorListener> listener) |
| 32 | { |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 33 | sut->registerForUpdates(listener); |
| 34 | DbusEnvironment::synchronizeIoc(); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 37 | static std::unique_ptr<stubs::DbusSensorObject> makeSensorObject() |
| 38 | { |
| 39 | return std::make_unique<stubs::DbusSensorObject>( |
| 40 | DbusEnvironment::getIoc(), DbusEnvironment::getBus(), |
| 41 | DbusEnvironment::getObjServer()); |
| 42 | } |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 43 | |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 44 | std::unique_ptr<stubs::DbusSensorObject> sensorObject = makeSensorObject(); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 45 | |
| 46 | SensorCache sensorCache; |
| 47 | uint64_t timestamp = std::time(0); |
| 48 | std::shared_ptr<Sensor> sut = sensorCache.makeSensor<Sensor>( |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 49 | DbusEnvironment::serviceName(), sensorObject->path(), |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 50 | DbusEnvironment::getIoc(), DbusEnvironment::getBus()); |
| 51 | std::shared_ptr<SensorListenerMock> listenerMock = |
| 52 | std::make_shared<StrictMock<SensorListenerMock>>(); |
| 53 | std::shared_ptr<SensorListenerMock> listenerMock2 = |
| 54 | std::make_shared<StrictMock<SensorListenerMock>>(); |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 55 | MockFunction<void()> checkPoint; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | TEST_F(TestSensor, createsCorretlyViaSensorCache) |
| 59 | { |
| 60 | ASSERT_THAT(sut->id(), |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 61 | Eq(Sensor::Id("Sensor", DbusEnvironment::serviceName(), |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 62 | sensorObject->path()))); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST_F(TestSensor, notifiesWithValueAfterRegister) |
| 66 | { |
| 67 | EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7)) |
Wludzik, Jozef | 0e29f43 | 2020-11-17 08:22:33 +0100 | [diff] [blame] | 68 | .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("async_read"))); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 69 | |
| 70 | registerForUpdates(listenerMock); |
| 71 | |
| 72 | ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read")); |
| 73 | } |
| 74 | |
| 75 | TEST_F(TestSensor, notifiesOnceWithValueAfterRegister) |
| 76 | { |
| 77 | EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7)) |
Wludzik, Jozef | 0e29f43 | 2020-11-17 08:22:33 +0100 | [diff] [blame] | 78 | .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("async_read"))); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 79 | EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7)) |
Wludzik, Jozef | 0e29f43 | 2020-11-17 08:22:33 +0100 | [diff] [blame] | 80 | .WillOnce( |
| 81 | InvokeWithoutArgs(DbusEnvironment::setPromise("async_read2"))); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 82 | |
| 83 | DbusEnvironment::synchronizedPost([this] { |
| 84 | sut->registerForUpdates(listenerMock); |
| 85 | sut->registerForUpdates(listenerMock2); |
| 86 | }); |
| 87 | |
| 88 | ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read")); |
| 89 | ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read2")); |
| 90 | } |
| 91 | |
| 92 | class TestSensorNotification : public TestSensor |
| 93 | { |
| 94 | public: |
| 95 | void SetUp() override |
| 96 | { |
| 97 | EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 0.)) |
Wludzik, Jozef | 0e29f43 | 2020-11-17 08:22:33 +0100 | [diff] [blame] | 98 | .WillOnce( |
| 99 | InvokeWithoutArgs(DbusEnvironment::setPromise("async_read"))); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 100 | |
| 101 | registerForUpdates(listenerMock); |
| 102 | |
| 103 | ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read")); |
| 104 | } |
| 105 | |
| 106 | std::shared_ptr<SensorListenerMock> listenerMock2 = |
| 107 | std::make_shared<StrictMock<SensorListenerMock>>(); |
| 108 | }; |
| 109 | |
| 110 | TEST_F(TestSensorNotification, notifiesListenerWithValueWhenChangeOccurs) |
| 111 | { |
| 112 | EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7)) |
Wludzik, Jozef | 0e29f43 | 2020-11-17 08:22:33 +0100 | [diff] [blame] | 113 | .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify"))); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 114 | |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 115 | sensorObject->setValue(42.7); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 116 | |
| 117 | ASSERT_TRUE(DbusEnvironment::waitForFuture("notify")); |
| 118 | } |
| 119 | |
| 120 | TEST_F(TestSensorNotification, notifiesListenerWithValueWhenNoChangeOccurs) |
| 121 | { |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 122 | InSequence seq; |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 123 | |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 124 | EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7)); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 125 | EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp))) |
Wludzik, Jozef | 0e29f43 | 2020-11-17 08:22:33 +0100 | [diff] [blame] | 126 | .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify"))); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 127 | |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 128 | sensorObject->setValue(42.7); |
| 129 | sensorObject->setValue(42.7); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 130 | |
| 131 | ASSERT_TRUE(DbusEnvironment::waitForFuture("notify")); |
| 132 | } |
| 133 | |
| 134 | TEST_F(TestSensorNotification, doesntNotifyExpiredListener) |
| 135 | { |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 136 | InSequence seq; |
| 137 | EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.)); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 138 | EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7)) |
Wludzik, Jozef | 0e29f43 | 2020-11-17 08:22:33 +0100 | [diff] [blame] | 139 | .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify"))); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 140 | |
| 141 | registerForUpdates(listenerMock2); |
| 142 | listenerMock = nullptr; |
| 143 | |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 144 | sensorObject->setValue(42.7); |
Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 145 | |
| 146 | ASSERT_TRUE(DbusEnvironment::waitForFuture("notify")); |
| 147 | } |
| 148 | |
| 149 | TEST_F(TestSensorNotification, notifiesWithValueDuringRegister) |
| 150 | { |
| 151 | EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.)); |
| 152 | |
| 153 | registerForUpdates(listenerMock2); |
| 154 | } |
Wludzik, Jozef | 5ade2b1 | 2020-11-16 14:00:23 +0100 | [diff] [blame] | 155 | |
| 156 | TEST_F(TestSensorNotification, |
| 157 | dbusSensorIsAddedToSystemAfterSensorIsCreatedThenValueIsUpdated) |
| 158 | { |
| 159 | InSequence seq; |
| 160 | |
| 161 | EXPECT_CALL(*listenerMock, |
| 162 | sensorUpdated(Ref(*sut), Ge(timestamp), DoubleEq(42.7))) |
| 163 | .WillOnce( |
| 164 | InvokeWithoutArgs(DbusEnvironment::setPromise("notify-change"))); |
| 165 | EXPECT_CALL(checkPoint, Call()); |
| 166 | EXPECT_CALL(*listenerMock, |
| 167 | sensorUpdated(Ref(*sut), Ge(timestamp), DoubleEq(0.))) |
| 168 | .WillOnce( |
| 169 | InvokeWithoutArgs(DbusEnvironment::setPromise("notify-create"))); |
| 170 | |
| 171 | sensorObject->setValue(42.7); |
| 172 | DbusEnvironment::waitForFuture("notify-change"); |
| 173 | |
| 174 | checkPoint.Call(); |
| 175 | |
| 176 | sensorObject = nullptr; |
| 177 | sensorObject = makeSensorObject(); |
| 178 | DbusEnvironment::waitForFuture("notify-create"); |
| 179 | } |