blob: 83151b1a029eadfc886627b87285d2674dbd6b37 [file] [log] [blame]
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02001#include "dbus_environment.hpp"
2#include "mocks/sensor_listener_mock.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01003#include "printers.hpp"
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02004#include "sensor.hpp"
5#include "sensor_cache.hpp"
6#include "stubs/dbus_sensor_object.hpp"
7#include "utils/sensor_id_eq.hpp"
8
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 {
23 sensorObject.setValue(42.7);
24 }
25
26 void TearDown() override
27 {
28 DbusEnvironment::synchronizeIoc();
29 }
30
31 void
32 registerForUpdates(std::shared_ptr<interfaces::SensorListener> listener)
33 {
34 DbusEnvironment::synchronizedPost(
35 [this, listener] { sut->registerForUpdates(listener); });
36 }
37
38 std::chrono::milliseconds notifiesInGivenIntervalAfterSchedule(
39 std::chrono::milliseconds interval);
40
41 stubs::DbusSensorObject sensorObject{DbusEnvironment::getIoc(),
42 DbusEnvironment::getBus(),
43 DbusEnvironment::getObjServer()};
44
45 SensorCache sensorCache;
46 uint64_t timestamp = std::time(0);
47 std::shared_ptr<Sensor> sut = sensorCache.makeSensor<Sensor>(
48 DbusEnvironment::serviceName(), sensorObject.path(),
49 DbusEnvironment::getIoc(), DbusEnvironment::getBus());
50 std::shared_ptr<SensorListenerMock> listenerMock =
51 std::make_shared<StrictMock<SensorListenerMock>>();
52 std::shared_ptr<SensorListenerMock> listenerMock2 =
53 std::make_shared<StrictMock<SensorListenerMock>>();
54};
55
56TEST_F(TestSensor, createsCorretlyViaSensorCache)
57{
58 ASSERT_THAT(sut->id(),
59 sensorIdEq(Sensor::Id("Sensor", DbusEnvironment::serviceName(),
60 sensorObject.path())));
61}
62
63TEST_F(TestSensor, notifiesWithValueAfterRegister)
64{
65 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010066 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020067
68 registerForUpdates(listenerMock);
69
70 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
71}
72
73TEST_F(TestSensor, notifiesOnceWithValueAfterRegister)
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 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010078 .WillOnce(
79 InvokeWithoutArgs(DbusEnvironment::setPromise("async_read2")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020080
81 DbusEnvironment::synchronizedPost([this] {
82 sut->registerForUpdates(listenerMock);
83 sut->registerForUpdates(listenerMock2);
84 });
85
86 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
87 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read2"));
88}
89
90class TestSensorNotification : public TestSensor
91{
92 public:
93 void SetUp() override
94 {
95 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 0.))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +010096 .WillOnce(
97 InvokeWithoutArgs(DbusEnvironment::setPromise("async_read")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020098
99 registerForUpdates(listenerMock);
100
101 ASSERT_TRUE(DbusEnvironment::waitForFuture("async_read"));
102 }
103
104 std::shared_ptr<SensorListenerMock> listenerMock2 =
105 std::make_shared<StrictMock<SensorListenerMock>>();
106};
107
108TEST_F(TestSensorNotification, notifiesListenerWithValueWhenChangeOccurs)
109{
110 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100111 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200112
113 sensorObject.setValue(42.7);
114
115 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
116}
117
118TEST_F(TestSensorNotification, notifiesListenerWithValueWhenNoChangeOccurs)
119{
120 Sequence seq;
121
122 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
123 .InSequence(seq);
124 EXPECT_CALL(*listenerMock, sensorUpdated(Ref(*sut), Ge(timestamp)))
125 .InSequence(seq)
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100126 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200127
128 sensorObject.setValue(42.7);
129 sensorObject.setValue(42.7);
130
131 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
132}
133
134TEST_F(TestSensorNotification, doesntNotifyExpiredListener)
135{
136 Sequence seq;
137 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.))
138 .InSequence(seq);
139 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 42.7))
140 .InSequence(seq)
Wludzik, Jozef0e29f432020-11-17 08:22:33 +0100141 .WillOnce(InvokeWithoutArgs(DbusEnvironment::setPromise("notify")));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +0200142
143 registerForUpdates(listenerMock2);
144 listenerMock = nullptr;
145
146 sensorObject.setValue(42.7);
147
148 ASSERT_TRUE(DbusEnvironment::waitForFuture("notify"));
149}
150
151TEST_F(TestSensorNotification, notifiesWithValueDuringRegister)
152{
153 EXPECT_CALL(*listenerMock2, sensorUpdated(Ref(*sut), Ge(timestamp), 0.));
154
155 registerForUpdates(listenerMock2);
156}