blob: 5547cf05d5c3b8e7214b7160a8f666efeb1fee45 [file] [log] [blame]
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02001#include "dbus_environment.hpp"
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +00002#include "helpers.hpp"
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02003#include "mocks/sensor_listener_mock.hpp"
4#include "sensor.hpp"
5#include "sensor_cache.hpp"
6#include "stubs/dbus_sensor_object.hpp"
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02007
8#include <sdbusplus/asio/property.hpp>
9
10#include <thread>
11
12#include <gmock/gmock.h>
13
14using namespace testing;
15using namespace std::chrono_literals;
16
17class TestSensor : public Test
18{
19 public:
20 void SetUp() override
21 {
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010022 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020023 }
24
25 void TearDown() override
26 {
27 DbusEnvironment::synchronizeIoc();
28 }
29
30 void
31 registerForUpdates(std::shared_ptr<interfaces::SensorListener> listener)
32 {
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010033 sut->registerForUpdates(listener);
34 DbusEnvironment::synchronizeIoc();
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020035 }
36
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020037 void unregisterFromUpdates(
38 std::shared_ptr<interfaces::SensorListener> listener)
39 {
40 sut->unregisterFromUpdates(listener);
41 DbusEnvironment::synchronizeIoc();
42 }
43
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010044 static std::unique_ptr<stubs::DbusSensorObject> makeSensorObject()
45 {
46 return std::make_unique<stubs::DbusSensorObject>(
47 DbusEnvironment::getIoc(), DbusEnvironment::getBus(),
48 DbusEnvironment::getObjServer());
49 }
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020050
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010051 std::unique_ptr<stubs::DbusSensorObject> sensorObject = makeSensorObject();
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020052
53 SensorCache sensorCache;
54 uint64_t timestamp = std::time(0);
55 std::shared_ptr<Sensor> sut = sensorCache.makeSensor<Sensor>(
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010056 DbusEnvironment::serviceName(), sensorObject->path(),
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020057 DbusEnvironment::getIoc(), DbusEnvironment::getBus());
58 std::shared_ptr<SensorListenerMock> listenerMock =
59 std::make_shared<StrictMock<SensorListenerMock>>();
60 std::shared_ptr<SensorListenerMock> listenerMock2 =
61 std::make_shared<StrictMock<SensorListenerMock>>();
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010062 MockFunction<void()> checkPoint;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020063};
64
65TEST_F(TestSensor, createsCorretlyViaSensorCache)
66{
67 ASSERT_THAT(sut->id(),
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010068 Eq(Sensor::Id("Sensor", DbusEnvironment::serviceName(),
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010069 sensorObject->path())));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020070}
71
72TEST_F(TestSensor, notifiesWithValueAfterRegister)
73{
74 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010075 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020076
77 registerForUpdates(listenerMock);
78
79 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
80}
81
82TEST_F(TestSensor, notifiesOnceWithValueAfterRegister)
83{
84 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010085 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020086 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010087 .WillOnce(
88 InvokeWithoutArgs(DbusEnvironment::setPromise("async_read2")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020089
90 DbusEnvironment::synchronizedPost([this] {
91 sut->registerForUpdates(listenerMock);
92 sut->registerForUpdates(listenerMock2);
93 });
94
95 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
96 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read2"));
97}
98
99class TestSensorNotification : public TestSensor
100{
101 public:
102 void SetUp() override
103 {
104 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 0.))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100105 .WillOnce(
106 InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200107
108 registerForUpdates(listenerMock);
109
110 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
111 }
112
113 std::shared_ptr<SensorListenerMock> listenerMock2 =
114 std::make_shared<StrictMock<SensorListenerMock>>();
115};
116
117TEST_F(TestSensorNotification, notifiesListenerWithValueWhenChangeOccurs)
118{
119 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100120 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200121
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100122 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200123
124 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
125}
126
127TEST_F(TestSensorNotification, notifiesListenerWithValueWhenNoChangeOccurs)
128{
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100129 InSequence seq;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200130
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100131 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200132 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp)))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100133 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200134
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100135 sensorObject->setValue(42.7);
136 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200137
138 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
139}
140
141TEST_F(TestSensorNotification, doesntNotifyExpiredListener)
142{
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100143 InSequence seq;
144 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200145 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100146 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200147
148 registerForUpdates(listenerMock2);
149 listenerMock = nullptr;
150
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100151 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200152
153 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
154}
155
156TEST_F(TestSensorNotification, notifiesWithValueDuringRegister)
157{
158 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.));
159
160 registerForUpdates(listenerMock2);
161}
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100162
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200163TEST_F(TestSensorNotification, notNotifiesWithValueWhenUnregistered)
164{
165 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.));
166 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
167 .Times(0);
168 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
169 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
170
171 registerForUpdates(listenerMock2);
172 unregisterFromUpdates(listenerMock);
173
174 sensorObject->setValue(42.7);
175
176 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
177}
178
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100179TEST_F(TestSensorNotification,
180 dbusSensorIsAddedToSystemAfterSensorIsCreatedThenValueIsUpdated)
181{
182 InSequence seq;
183
184 EXPECT_CALL(*listenerMock,
185 sensorUpdated(Ref(*sut), Ge(timestamp), DoubleEq(42.7)))
186 .WillOnce(
187 InvokeWithoutArgs(DbusEnvironment::setPromise("notify-change")));
188 EXPECT_CALL(checkPoint, Call());
189 EXPECT_CALL(*listenerMock,
190 sensorUpdated(Ref(*sut), Ge(timestamp), DoubleEq(0.)))
191 .WillOnce(
192 InvokeWithoutArgs(DbusEnvironment::setPromise("notify-create")));
193
194 sensorObject->setValue(42.7);
195 DbusEnvironment::waitForFuture("notify-change");
196
197 checkPoint.Call();
198
199 sensorObject = nullptr;
200 sensorObject = makeSensorObject();
201 DbusEnvironment::waitForFuture("notify-create");
202}