Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2018-2019 Nokia. |
| 3 | |
| 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 |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 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. |
| 15 | */ |
| 16 | |
Timo Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 22 | #include <type_traits> |
| 23 | #include <memory> |
| 24 | #include <cstring> |
| 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" |
| 33 | |
| 34 | using namespace shareddatalayer; |
| 35 | using namespace shareddatalayer::redis; |
| 36 | using namespace shareddatalayer::tst; |
| 37 | using namespace testing; |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | class AsyncHiredisDatabaseDiscoveryBaseTest: public testing::Test |
| 42 | { |
| 43 | public: |
| 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; |
| 52 | |
| 53 | MOCK_METHOD1(stateChangedCb, void(const DatabaseInfo&)); |
| 54 | |
| 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)) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | virtual ~AsyncHiredisDatabaseDiscoveryBaseTest() |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | void expectStateChangedCbForStandalone() |
| 70 | { |
| 71 | EXPECT_CALL(*this, stateChangedCb(_)) |
| 72 | .Times(1) |
| 73 | .WillOnce(Invoke([this](const DatabaseInfo& databaseInfo) |
| 74 | { |
| 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()); |
| 78 | })); |
| 79 | } |
| 80 | |
| 81 | void expectStateChangedCbForCluster() |
| 82 | { |
| 83 | EXPECT_CALL(*this, stateChangedCb(_)) |
| 84 | .Times(1) |
| 85 | .WillOnce(Invoke([this](const DatabaseInfo& databaseInfo) |
| 86 | { |
| 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()); |
| 91 | })); |
| 92 | } |
| 93 | |
| 94 | void expectPostStateChangedCb() |
| 95 | { |
| 96 | EXPECT_CALL(*engineMock, postCallback(_)) |
| 97 | .Times(1) |
| 98 | .WillOnce(SaveArg<0>(&postedCallback)); |
| 99 | } |
| 100 | |
| 101 | void callPostedCallback() |
| 102 | { |
| 103 | ASSERT_NE(postedCallback, nullptr); |
| 104 | postedCallback(); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | class AsyncHiredisDatabaseDiscoveryTest: public AsyncHiredisDatabaseDiscoveryBaseTest |
| 109 | { |
| 110 | public: |
| 111 | AsyncHiredisDatabaseDiscoveryTest() |
| 112 | { |
| 113 | InSequence dummy; |
| 114 | standaloneDiscovery.reset(new AsyncHiredisDatabaseDiscovery(engineMock, |
| 115 | defaultNamespaceName, |
| 116 | DatabaseInfo::Type::SINGLE, |
| 117 | someKnownStandaloneDatabaseAddress, |
| 118 | logger)); |
| 119 | clusterDiscovery.reset(new AsyncHiredisDatabaseDiscovery(engineMock, |
| 120 | defaultNamespaceName, |
| 121 | DatabaseInfo::Type::CLUSTER, |
| 122 | someKnownClusterDatabaseAddresses, |
| 123 | logger)); |
| 124 | } |
| 125 | |
| 126 | ~AsyncHiredisDatabaseDiscoveryTest() |
| 127 | { |
| 128 | } |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | TEST_F(AsyncHiredisDatabaseDiscoveryBaseTest, IsNotCopyable) |
| 133 | { |
| 134 | InSequence dummy; |
| 135 | EXPECT_FALSE(std::is_copy_constructible<AsyncHiredisDatabaseDiscovery>::value); |
| 136 | EXPECT_FALSE(std::is_copy_assignable<AsyncHiredisDatabaseDiscovery>::value); |
| 137 | } |
| 138 | |
| 139 | TEST_F(AsyncHiredisDatabaseDiscoveryBaseTest, ImplementsAsyncDatabaseDiscovery) |
| 140 | { |
| 141 | InSequence dummy; |
| 142 | EXPECT_TRUE((std::is_base_of<AsyncDatabaseDiscovery, AsyncHiredisDatabaseDiscovery>::value)); |
| 143 | } |
| 144 | |
| 145 | TEST_F(AsyncHiredisDatabaseDiscoveryTest, StateChangedCallbackIsPostedImmediately) |
| 146 | { |
| 147 | InSequence dummy; |
| 148 | expectPostStateChangedCb(); |
| 149 | clusterDiscovery->setStateChangedCb(std::bind(&AsyncHiredisDatabaseDiscoveryTest::stateChangedCb, |
| 150 | this, |
| 151 | std::placeholders::_1)); |
| 152 | expectStateChangedCbForCluster(); |
| 153 | callPostedCallback(); |
| 154 | |
| 155 | expectPostStateChangedCb(); |
| 156 | standaloneDiscovery->setStateChangedCb(std::bind(&AsyncHiredisDatabaseDiscoveryTest::stateChangedCb, |
| 157 | this, |
| 158 | std::placeholders::_1)); |
| 159 | expectStateChangedCbForStandalone(); |
| 160 | callPostedCallback(); |
| 161 | } |