2 Copyright (c) 2018-2019 Nokia.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #include <type_traits>
25 #include <arpa/inet.h>
26 #include <gtest/gtest.h>
27 #include "private/createlogger.hpp"
28 #include "private/databaseconfigurationimpl.hpp"
29 #include "private/logger.hpp"
30 #include "private/redis/asynchiredisdatabasediscovery.hpp"
31 #include "private/redis/databaseinfo.hpp"
32 #include "private/tst/enginemock.hpp"
34 using namespace shareddatalayer;
35 using namespace shareddatalayer::redis;
36 using namespace shareddatalayer::tst;
37 using namespace testing;
41 class AsyncHiredisDatabaseDiscoveryBaseTest: public testing::Test
44 shareddatalayer::DatabaseConfiguration::Addresses someKnownStandaloneDatabaseAddress;
45 shareddatalayer::DatabaseConfiguration::Addresses someKnownClusterDatabaseAddresses;
46 std::shared_ptr<StrictMock<EngineMock>> engineMock;
47 const std::string defaultNamespaceName;
48 std::unique_ptr<AsyncHiredisDatabaseDiscovery> standaloneDiscovery;
49 std::unique_ptr<AsyncHiredisDatabaseDiscovery> clusterDiscovery;
50 Engine::Callback postedCallback;
51 std::shared_ptr<Logger> logger;
53 MOCK_METHOD1(stateChangedCb, void(const DatabaseInfo&));
55 AsyncHiredisDatabaseDiscoveryBaseTest():
56 someKnownStandaloneDatabaseAddress({ HostAndPort("addr1", htons(28416)) }),
57 someKnownClusterDatabaseAddresses({ HostAndPort("addr1", htons(28416)),
58 HostAndPort("addr2", htons(56832)) }),
59 engineMock(std::make_shared<StrictMock<EngineMock>>()),
60 defaultNamespaceName("namespace"),
61 logger(createLogger(SDL_LOG_PREFIX))
65 virtual ~AsyncHiredisDatabaseDiscoveryBaseTest()
69 void expectStateChangedCbForStandalone()
71 EXPECT_CALL(*this, stateChangedCb(_))
73 .WillOnce(Invoke([this](const DatabaseInfo& databaseInfo)
75 EXPECT_THAT(someKnownStandaloneDatabaseAddress, ContainerEq(databaseInfo.hosts));
76 EXPECT_EQ(DatabaseInfo::Type::SINGLE, databaseInfo.type);
77 EXPECT_STREQ(defaultNamespaceName.c_str(), databaseInfo.ns->c_str());
81 void expectStateChangedCbForCluster()
83 EXPECT_CALL(*this, stateChangedCb(_))
85 .WillOnce(Invoke([this](const DatabaseInfo& databaseInfo)
87 EXPECT_THAT(someKnownClusterDatabaseAddresses, ContainerEq(databaseInfo.hosts));
88 EXPECT_EQ(DatabaseInfo::Type::CLUSTER, databaseInfo.type);
89 EXPECT_EQ(DatabaseInfo::Discovery::HIREDIS, databaseInfo.discovery);
90 EXPECT_STREQ(defaultNamespaceName.c_str(), databaseInfo.ns->c_str());
94 void expectPostStateChangedCb()
96 EXPECT_CALL(*engineMock, postCallback(_))
98 .WillOnce(SaveArg<0>(&postedCallback));
101 void callPostedCallback()
103 ASSERT_NE(postedCallback, nullptr);
108 class AsyncHiredisDatabaseDiscoveryTest: public AsyncHiredisDatabaseDiscoveryBaseTest
111 AsyncHiredisDatabaseDiscoveryTest()
114 standaloneDiscovery.reset(new AsyncHiredisDatabaseDiscovery(engineMock,
115 defaultNamespaceName,
116 DatabaseInfo::Type::SINGLE,
117 someKnownStandaloneDatabaseAddress,
119 clusterDiscovery.reset(new AsyncHiredisDatabaseDiscovery(engineMock,
120 defaultNamespaceName,
121 DatabaseInfo::Type::CLUSTER,
122 someKnownClusterDatabaseAddresses,
126 ~AsyncHiredisDatabaseDiscoveryTest()
132 TEST_F(AsyncHiredisDatabaseDiscoveryBaseTest, IsNotCopyable)
135 EXPECT_FALSE(std::is_copy_constructible<AsyncHiredisDatabaseDiscovery>::value);
136 EXPECT_FALSE(std::is_copy_assignable<AsyncHiredisDatabaseDiscovery>::value);
139 TEST_F(AsyncHiredisDatabaseDiscoveryBaseTest, ImplementsAsyncDatabaseDiscovery)
142 EXPECT_TRUE((std::is_base_of<AsyncDatabaseDiscovery, AsyncHiredisDatabaseDiscovery>::value));
145 TEST_F(AsyncHiredisDatabaseDiscoveryTest, StateChangedCallbackIsPostedImmediately)
148 expectPostStateChangedCb();
149 clusterDiscovery->setStateChangedCb(std::bind(&AsyncHiredisDatabaseDiscoveryTest::stateChangedCb,
151 std::placeholders::_1));
152 expectStateChangedCbForCluster();
153 callPostedCallback();
155 expectPostStateChangedCb();
156 standaloneDiscovery->setStateChangedCb(std::bind(&AsyncHiredisDatabaseDiscoveryTest::stateChangedCb,
158 std::placeholders::_1));
159 expectStateChangedCbForStandalone();
160 callPostedCallback();