xref: /openbmc/telemetry/tests/src/test_sensor_cache.cpp (revision 583ba441654657bb4ba9d051b747144a7258c159)
1d2238194SKrzysztof Grobelny #include "helpers.hpp"
27f06f613SKrzysztof Grobelny #include "mocks/sensor_mock.hpp"
37f06f613SKrzysztof Grobelny #include "sensor_cache.hpp"
47f06f613SKrzysztof Grobelny 
57f06f613SKrzysztof Grobelny #include <initializer_list>
67f06f613SKrzysztof Grobelny 
77f06f613SKrzysztof Grobelny #include <gmock/gmock.h>
87f06f613SKrzysztof Grobelny 
97f06f613SKrzysztof Grobelny using namespace testing;
107f06f613SKrzysztof Grobelny 
117f06f613SKrzysztof Grobelny class TestSensorCache : public Test
127f06f613SKrzysztof Grobelny {
137f06f613SKrzysztof Grobelny   public:
147f06f613SKrzysztof Grobelny     SensorCache sut;
157f06f613SKrzysztof Grobelny };
167f06f613SKrzysztof Grobelny 
177f06f613SKrzysztof Grobelny struct IdParam
187f06f613SKrzysztof Grobelny {
197f06f613SKrzysztof Grobelny     IdParam() = default;
IdParamIdParam207f06f613SKrzysztof Grobelny     IdParam(std::string_view service, std::string_view path) :
217f06f613SKrzysztof Grobelny         service(service), path(path)
227f06f613SKrzysztof Grobelny     {}
237f06f613SKrzysztof Grobelny 
PrintTo(const IdParam & param,std::ostream * os)24b3e03d2dSMichal Orzel     friend void PrintTo(const IdParam& param, std::ostream* os)
25b3e03d2dSMichal Orzel     {
26b3e03d2dSMichal Orzel         *os << "(" << param.service << "): " << param.path;
27b3e03d2dSMichal Orzel     }
28b3e03d2dSMichal Orzel 
297f06f613SKrzysztof Grobelny     std::string service;
307f06f613SKrzysztof Grobelny     std::string path;
317f06f613SKrzysztof Grobelny };
327f06f613SKrzysztof Grobelny 
337f06f613SKrzysztof Grobelny class TestSensorCacheP :
347f06f613SKrzysztof Grobelny     public TestSensorCache,
357f06f613SKrzysztof Grobelny     public WithParamInterface<std::vector<IdParam>>
367f06f613SKrzysztof Grobelny {
377f06f613SKrzysztof Grobelny   public:
SetUp()387f06f613SKrzysztof Grobelny     void SetUp() override
397f06f613SKrzysztof Grobelny     {
407f06f613SKrzysztof Grobelny         auto vec = GetParam();
417f06f613SKrzysztof Grobelny         ASSERT_THAT(vec, SizeIs(param.size()));
427f06f613SKrzysztof Grobelny         std::copy(vec.begin(), vec.end(), param.begin());
437f06f613SKrzysztof Grobelny     }
447f06f613SKrzysztof Grobelny 
457f06f613SKrzysztof Grobelny     template <size_t index>
id() const467f06f613SKrzysztof Grobelny     const IdParam& id() const
477f06f613SKrzysztof Grobelny     {
487f06f613SKrzysztof Grobelny         static_assert(index < std::tuple_size_v<decltype(param)>);
497f06f613SKrzysztof Grobelny         return param[index];
507f06f613SKrzysztof Grobelny     }
517f06f613SKrzysztof Grobelny 
527f06f613SKrzysztof Grobelny   private:
537f06f613SKrzysztof Grobelny     std::array<IdParam, 2> param;
547f06f613SKrzysztof Grobelny };
557f06f613SKrzysztof Grobelny 
567f06f613SKrzysztof Grobelny INSTANTIATE_TEST_SUITE_P(
577f06f613SKrzysztof Grobelny     UniqueIds, TestSensorCacheP,
587f06f613SKrzysztof Grobelny     Values(std::vector<IdParam>({IdParam("service1", "path1"),
597f06f613SKrzysztof Grobelny                                  IdParam("service1", "path2")}),
607f06f613SKrzysztof Grobelny            std::vector<IdParam>({IdParam("service1", "path1"),
617f06f613SKrzysztof Grobelny                                  IdParam("service2", "path1")}),
627f06f613SKrzysztof Grobelny            std::vector<IdParam>({IdParam("service1", "path1"),
637f06f613SKrzysztof Grobelny                                  IdParam("service2", "path2")})));
647f06f613SKrzysztof Grobelny 
TEST_P(TestSensorCacheP,shouldReturnDifferentSensorWhenIdsAreDifferent)657f06f613SKrzysztof Grobelny TEST_P(TestSensorCacheP, shouldReturnDifferentSensorWhenIdsAreDifferent)
667f06f613SKrzysztof Grobelny {
67*583ba441SPatrick Williams     auto sensor1 =
68*583ba441SPatrick Williams         sut.makeSensor<NiceMock<SensorMock>>(id<0>().service, id<0>().path);
69*583ba441SPatrick Williams     auto sensor2 =
70*583ba441SPatrick Williams         sut.makeSensor<NiceMock<SensorMock>>(id<1>().service, id<1>().path);
717f06f613SKrzysztof Grobelny 
727f06f613SKrzysztof Grobelny     ASSERT_THAT(sensor1.get(), Not(Eq(sensor2.get())));
737f06f613SKrzysztof Grobelny     ASSERT_THAT(sensor1->mockId, Not(Eq(sensor2->mockId)));
747f06f613SKrzysztof Grobelny }
757f06f613SKrzysztof Grobelny 
TEST_F(TestSensorCache,shouldReturnSameSensorWhenSensorWithSameIdStillExists)767f06f613SKrzysztof Grobelny TEST_F(TestSensorCache, shouldReturnSameSensorWhenSensorWithSameIdStillExists)
777f06f613SKrzysztof Grobelny {
78*583ba441SPatrick Williams     auto sensor1 =
79*583ba441SPatrick Williams         sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path");
80*583ba441SPatrick Williams     auto sensor2 =
81*583ba441SPatrick Williams         sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path");
827f06f613SKrzysztof Grobelny 
837f06f613SKrzysztof Grobelny     ASSERT_THAT(sensor1.get(), Eq(sensor2.get()));
847f06f613SKrzysztof Grobelny     ASSERT_THAT(sensor1->mockId, Eq(sensor2->mockId));
857f06f613SKrzysztof Grobelny }
867f06f613SKrzysztof Grobelny 
TEST_F(TestSensorCache,shouldReturnDifferentSensorWhenPreviousSensorExpired)877f06f613SKrzysztof Grobelny TEST_F(TestSensorCache, shouldReturnDifferentSensorWhenPreviousSensorExpired)
887f06f613SKrzysztof Grobelny {
89*583ba441SPatrick Williams     auto mockId1 =
90*583ba441SPatrick Williams         sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path")
917f06f613SKrzysztof Grobelny             ->mockId;
92*583ba441SPatrick Williams     auto mockId2 =
93*583ba441SPatrick Williams         sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path")
947f06f613SKrzysztof Grobelny             ->mockId;
957f06f613SKrzysztof Grobelny 
967f06f613SKrzysztof Grobelny     ASSERT_THAT(mockId2, Not(Eq(mockId1)));
977f06f613SKrzysztof Grobelny }
987f06f613SKrzysztof Grobelny 
TEST_F(TestSensorCache,shouldCreateSensorWithCorrespondingId)997f06f613SKrzysztof Grobelny TEST_F(TestSensorCache, shouldCreateSensorWithCorrespondingId)
1007f06f613SKrzysztof Grobelny {
101*583ba441SPatrick Williams     auto id =
102*583ba441SPatrick Williams         sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path")
1037f06f613SKrzysztof Grobelny             ->id();
1047f06f613SKrzysztof Grobelny 
1057f06f613SKrzysztof Grobelny     auto expected = SensorMock::makeId("sensor-service", "sensor-path");
1067f06f613SKrzysztof Grobelny 
1076ccfcbf5SKrzysztof Grobelny     ASSERT_THAT(id, Eq(expected));
1087f06f613SKrzysztof Grobelny }
109