diff --git a/cppcache/integration/test/RegisterKeysTest.cpp b/cppcache/integration/test/RegisterKeysTest.cpp index deae4c5d02..f1dfaa2512 100644 --- a/cppcache/integration/test/RegisterKeysTest.cpp +++ b/cppcache/integration/test/RegisterKeysTest.cpp @@ -16,9 +16,6 @@ #include -#include -#include - #include #include @@ -33,6 +30,7 @@ #include "framework/Cluster.h" #include "framework/Framework.h" #include "framework/Gfsh.h" +#include "gmock_actions.hpp" #include "mock/CacheListenerMock.hpp" #include "util/concurrent/binary_semaphore.hpp" @@ -54,10 +52,6 @@ using ::testing::DoAll; using ::testing::InvokeWithoutArgs; using ::testing::Return; -ACTION_P(ReleaseSem, sem) { sem->release(); } -ACTION_P(AcquireSem, sem) { sem->acquire(); } -ACTION_P(CountDownLatch, latch) { latch->count_down(); } - Cache createTestCache() { CacheFactory cacheFactory; return cacheFactory.set("log-level", "none") diff --git a/cppcache/integration/test/ServerDisconnectWithListener.cpp b/cppcache/integration/test/ServerDisconnectWithListener.cpp index d54e85eb04..7b054520bf 100644 --- a/cppcache/integration/test/ServerDisconnectWithListener.cpp +++ b/cppcache/integration/test/ServerDisconnectWithListener.cpp @@ -14,34 +14,36 @@ * limitations under the License. */ -#include +#include #include #include #include +#include #include +#include #include #include #include "framework/Cluster.h" -#include "framework/Framework.h" #include "framework/Gfsh.h" +#include "gmock_actions.hpp" +#include "mock/CacheListenerMock.hpp" namespace { +using apache::geode::client::binary_semaphore; using apache::geode::client::Cache; using apache::geode::client::CacheableInt16; using apache::geode::client::CacheFactory; using apache::geode::client::CacheListener; +using apache::geode::client::CacheListenerMock; using apache::geode::client::NotConnectedException; using apache::geode::client::Region; using apache::geode::client::RegionShortcut; -static bool isDisconnected = false; - -class RegionDisconnectedListener : public CacheListener { - void afterRegionDisconnected(Region&) override { isDisconnected = true; } -}; +using ::testing::_; +using ::testing::NiceMock; Cache createTestCache() { CacheFactory cacheFactory; @@ -72,22 +74,25 @@ TEST(ServerDisconnect, WithRegionDisconnectedListener) { auto pool = poolFactory.create("pool"); auto regionFactory = cache.createRegionFactory(RegionShortcut::CACHING_PROXY); - auto regionDisconnectedListener = - std::make_shared(); - auto region = regionFactory.setPoolName("pool") - .setCacheListener(regionDisconnectedListener) - .create("region"); + binary_semaphore live_sem{0}; + binary_semaphore shut_sem{1}; + auto listener = std::make_shared>(); + EXPECT_CALL(*listener, afterRegionLive(_)) + .WillRepeatedly(AcquireSem(&shut_sem)); + EXPECT_CALL(*listener, afterRegionDisconnected(_)) + .WillRepeatedly(ReleaseSem(&shut_sem)); + auto region = + regionFactory.setPoolName("pool").setCacheListener(listener).create( + "region"); region->put("one", std::make_shared(1)); auto& servers = cluster.getServers(); servers[0].stop(); + shut_sem.acquire(); + shut_sem.release(); - try { - region->put("two", std::make_shared(2)); - } catch (const NotConnectedException&) { - } - - ASSERT_EQ(isDisconnected, true); + EXPECT_THROW(region->put("two", std::make_shared(2)), + NotConnectedException); } } // namespace diff --git a/cppcache/integration/test/gmock_actions.hpp b/cppcache/integration/test/gmock_actions.hpp new file mode 100644 index 0000000000..5d7f4544b3 --- /dev/null +++ b/cppcache/integration/test/gmock_actions.hpp @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#ifndef GMOCK_ACTIONS_H_ +#define GMOCK_ACTIONS_H_ + +#include + +ACTION_P(ReleaseSem, sem) { sem->release(); } +ACTION_P(AcquireSem, sem) { sem->acquire(); } +ACTION_P(CountDownLatch, latch) { latch->count_down(); } + +#endif // GMOCK_ACTIONS_H_