blob: 0eae668f2b20f58e78a62ee37b2c486682822f3b [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 Grobelnyb8cc78d2021-11-29 15:54:53 +01007#include "utils/clock.hpp"
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02008
9#include <sdbusplus/asio/property.hpp>
10
11#include <thread>
12
13#include <gmock/gmock.h>
14
15using namespace testing;
16using namespace std::chrono_literals;
17
18class TestSensor : public Test
19{
20 public:
21 void SetUp() override
22 {
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010023 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020024 }
25
26 void TearDown() override
27 {
28 DbusEnvironment::synchronizeIoc();
29 }
30
31 void
32 registerForUpdates(std::shared_ptr<interfaces::SensorListener> listener)
33 {
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010034 sut->registerForUpdates(listener);
35 DbusEnvironment::synchronizeIoc();
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020036 }
37
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020038 void unregisterFromUpdates(
39 std::shared_ptr<interfaces::SensorListener> listener)
40 {
41 sut->unregisterFromUpdates(listener);
42 DbusEnvironment::synchronizeIoc();
43 }
44
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010045 static std::unique_ptr<stubs::DbusSensorObject> makeSensorObject()
46 {
47 return std::make_unique<stubs::DbusSensorObject>(
48 DbusEnvironment::getIoc(), DbusEnvironment::getBus(),
49 DbusEnvironment::getObjServer());
50 }
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020051
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010052 std::unique_ptr<stubs::DbusSensorObject> sensorObject = makeSensorObject();
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020053
54 SensorCache sensorCache;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010055 Milliseconds timestamp = Clock().steadyTimestamp();
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020056 std::shared_ptr<Sensor> sut = sensorCache.makeSensor<Sensor>(
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010057 DbusEnvironment::serviceName(), sensorObject->path(), "metadata",
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020058 DbusEnvironment::getIoc(), DbusEnvironment::getBus());
59 std::shared_ptr<SensorListenerMock> listenerMock =
60 std::make_shared<StrictMock<SensorListenerMock>>();
61 std::shared_ptr<SensorListenerMock> listenerMock2 =
62 std::make_shared<StrictMock<SensorListenerMock>>();
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010063 MockFunction<void()> checkPoint;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020064};
65
66TEST_F(TestSensor, createsCorretlyViaSensorCache)
67{
68 ASSERT_THAT(sut->id(),
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010069 Eq(Sensor::Id("Sensor", DbusEnvironment::serviceName(),
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010070 sensorObject->path())));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020071}
72
73TEST_F(TestSensor, notifiesWithValueAfterRegister)
74{
75 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010076 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020077
78 registerForUpdates(listenerMock);
79
80 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
81}
82
83TEST_F(TestSensor, notifiesOnceWithValueAfterRegister)
84{
85 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010086 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020087 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010088 .WillOnce(
89 InvokeWithoutArgs(DbusEnvironment::setPromise("async_read2")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020090
91 DbusEnvironment::synchronizedPost([this] {
92 sut->registerForUpdates(listenerMock);
93 sut->registerForUpdates(listenerMock2);
94 });
95
96 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
97 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read2"));
98}
99
100class TestSensorNotification : public TestSensor
101{
102 public:
103 void SetUp() override
104 {
105 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 0.))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100106 .WillOnce(
107 InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200108
109 registerForUpdates(listenerMock);
110
111 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
112 }
113
114 std::shared_ptr<SensorListenerMock> listenerMock2 =
115 std::make_shared<StrictMock<SensorListenerMock>>();
116};
117
118TEST_F(TestSensorNotification, notifiesListenerWithValueWhenChangeOccurs)
119{
120 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100121 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200122
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100123 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200124
125 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
126}
127
128TEST_F(TestSensorNotification, notifiesListenerWithValueWhenNoChangeOccurs)
129{
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100130 InSequence seq;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200131
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100132 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200133 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp)))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100134 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200135
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100136 sensorObject->setValue(42.7);
137 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200138
139 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
140}
141
142TEST_F(TestSensorNotification, doesntNotifyExpiredListener)
143{
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100144 InSequence seq;
145 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200146 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100147 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200148
149 registerForUpdates(listenerMock2);
150 listenerMock = nullptr;
151
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100152 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200153
154 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
155}
156
157TEST_F(TestSensorNotification, notifiesWithValueDuringRegister)
158{
159 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.));
160
161 registerForUpdates(listenerMock2);
162}
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100163
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200164TEST_F(TestSensorNotification, notNotifiesWithValueWhenUnregistered)
165{
166 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.));
167 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
168 .Times(0);
169 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
170 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
171
172 registerForUpdates(listenerMock2);
173 unregisterFromUpdates(listenerMock);
174
175 sensorObject->setValue(42.7);
176
177 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
178}
179
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100180TEST_F(TestSensorNotification,
181 dbusSensorIsAddedToSystemAfterSensorIsCreatedThenValueIsUpdated)
182{
183 InSequence seq;
184
185 EXPECT_CALL(*listenerMock,
186 sensorUpdated(Ref(*sut), Ge(timestamp), DoubleEq(42.7)))
187 .WillOnce(
188 InvokeWithoutArgs(DbusEnvironment::setPromise("notify-change")));
189 EXPECT_CALL(checkPoint, Call());
190 EXPECT_CALL(*listenerMock,
191 sensorUpdated(Ref(*sut), Ge(timestamp), DoubleEq(0.)))
192 .WillOnce(
193 InvokeWithoutArgs(DbusEnvironment::setPromise("notify-create")));
194
195 sensorObject->setValue(42.7);
196 DbusEnvironment::waitForFuture("notify-change");
197
198 checkPoint.Call();
199
200 sensorObject = nullptr;
201 sensorObject = makeSensorObject();
202 DbusEnvironment::waitForFuture("notify-create");
203}