blob: 820602ce5f758273eab1e9cb08fd9b91b57ca51f [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
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010037 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 Grobelnyb5645942020-09-29 11:52:45 +020043
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010044 std::unique_ptr<stubs::DbusSensorObject> sensorObject = makeSensorObject();
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020045
46 SensorCache sensorCache;
47 uint64_t timestamp = std::time(0);
48 std::shared_ptr<Sensor> sut = sensorCache.makeSensor<Sensor>(
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010049 DbusEnvironment::serviceName(), sensorObject->path(),
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020050 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, Jozef5ade2b12020-11-16 14:00:23 +010055 MockFunction<void()> checkPoint;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020056};
57
58TEST_F(TestSensor, createsCorretlyViaSensorCache)
59{
60 ASSERT_THAT(sut->id(),
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +010061 Eq(Sensor::Id("Sensor", DbusEnvironment::serviceName(),
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010062 sensorObject->path())));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020063}
64
65TEST_F(TestSensor, notifiesWithValueAfterRegister)
66{
67 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010068 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020069
70 registerForUpdates(listenerMock);
71
72 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
73}
74
75TEST_F(TestSensor, notifiesOnceWithValueAfterRegister)
76{
77 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010078 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020079 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010080 .WillOnce(
81 InvokeWithoutArgs(DbusEnvironment::setPromise("async_read2")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020082
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
92class TestSensorNotification : public TestSensor
93{
94 public:
95 void SetUp() override
96 {
97 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 0.))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010098 .WillOnce(
99 InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200100
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
110TEST_F(TestSensorNotification, notifiesListenerWithValueWhenChangeOccurs)
111{
112 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100113 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200114
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100115 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200116
117 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
118}
119
120TEST_F(TestSensorNotification, notifiesListenerWithValueWhenNoChangeOccurs)
121{
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100122 InSequence seq;
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200123
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100124 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200125 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp)))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100126 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200127
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100128 sensorObject->setValue(42.7);
129 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200130
131 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
132}
133
134TEST_F(TestSensorNotification, doesntNotifyExpiredListener)
135{
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100136 InSequence seq;
137 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200138 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100139 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200140
141 registerForUpdates(listenerMock2);
142 listenerMock = nullptr;
143
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100144 sensorObject->setValue(42.7);
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200145
146 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
147}
148
149TEST_F(TestSensorNotification, notifiesWithValueDuringRegister)
150{
151 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.));
152
153 registerForUpdates(listenerMock2);
154}
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +0100155
156TEST_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}