This is an automated email from the ASF dual-hosted git repository.
jbarrett pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-native.git
The following commit(s) were added to refs/heads/develop by this push:
new 9ef4490 GEODE-7528: Fixes race condition in tests. (#559)
9ef4490 is described below
commit 9ef4490fbc4da6aea076fad75544b496e1f0ef55
Author: Jacob Barrett <[email protected]>
AuthorDate: Wed Dec 4 23:43:42 2019 -0800
GEODE-7528: Fixes race condition in tests. (#559)
* Replace counters with latch.
---
cppcache/integration/test/CMakeLists.txt | 1 +
.../integration/test/CqPlusAuthInitializeTest.cpp | 39 ++++++++++------------
cppcache/integration/test/CqTest.cpp | 31 +++++++++--------
cppcache/integration/test/SimpleCqListener.cpp | 20 +++++------
cppcache/integration/test/SimpleCqListener.hpp | 18 +++++-----
dependencies/boost/CMakeLists.txt | 1 +
6 files changed, 54 insertions(+), 56 deletions(-)
diff --git a/cppcache/integration/test/CMakeLists.txt
b/cppcache/integration/test/CMakeLists.txt
index 4df6c91..19a0f51 100644
--- a/cppcache/integration/test/CMakeLists.txt
+++ b/cppcache/integration/test/CMakeLists.txt
@@ -63,6 +63,7 @@ target_link_libraries(cpp-integration-test
Boost::system
Boost::log
Boost::filesystem
+ Boost::chrono
PRIVATE
_WarningsAsError
internal
diff --git a/cppcache/integration/test/CqPlusAuthInitializeTest.cpp
b/cppcache/integration/test/CqPlusAuthInitializeTest.cpp
index e591b3d..d0a5577 100644
--- a/cppcache/integration/test/CqPlusAuthInitializeTest.cpp
+++ b/cppcache/integration/test/CqPlusAuthInitializeTest.cpp
@@ -16,19 +16,17 @@
*/
#include <chrono>
-#include <future>
#include <iostream>
-#include <random>
#include <thread>
+#include <boost/thread/latch.hpp>
+
#include <gtest/gtest.h>
#include <geode/AuthInitialize.hpp>
#include <geode/Cache.hpp>
-#include <geode/CqAttributes.hpp>
#include <geode/CqAttributesFactory.hpp>
#include <geode/CqEvent.hpp>
-#include <geode/CqListener.hpp>
#include <geode/PoolManager.hpp>
#include <geode/QueryService.hpp>
#include <geode/RegionFactory.hpp>
@@ -38,8 +36,6 @@
#include "SimpleAuthInitialize.hpp"
#include "SimpleCqListener.hpp"
#include "framework/Cluster.h"
-#include "framework/Framework.h"
-#include "framework/Gfsh.h"
namespace {
@@ -131,8 +127,16 @@ TEST(CqPlusAuthInitializeTest,
putInALoopWhileSubscribedAndAuthenticated) {
auto queryService = cache.getQueryService();
+ auto createLatch =
+ std::make_shared<boost::latch>(CQ_PLUS_AUTH_TEST_REGION_ENTRY_COUNT);
+ auto updateLatch =
+ std::make_shared<boost::latch>(CQ_PLUS_AUTH_TEST_REGION_ENTRY_COUNT);
+ auto destroyLatch =
+ std::make_shared<boost::latch>(CQ_PLUS_AUTH_TEST_REGION_ENTRY_COUNT);
+ auto testListener = std::make_shared<SimpleCqListener>(
+ createLatch, updateLatch, destroyLatch);
+
CqAttributesFactory attributesFactory;
- auto testListener = std::make_shared<SimpleCqListener>();
attributesFactory.addCqListener(testListener);
auto cqAttributes = attributesFactory.create();
@@ -186,21 +190,14 @@ TEST(CqPlusAuthInitializeTest,
putInALoopWhileSubscribedAndAuthenticated) {
FAIL();
}
- for (i = 0; i < 1000; i++) {
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- if (testListener->getDestructionCount() ==
- CQ_PLUS_AUTH_TEST_REGION_ENTRY_COUNT) {
- break;
- }
- }
+ EXPECT_EQ(boost::cv_status::no_timeout,
+ createLatch->wait_for(boost::chrono::seconds(30)));
+ EXPECT_EQ(boost::cv_status::no_timeout,
+ updateLatch->wait_for(boost::chrono::seconds(30)));
+ EXPECT_EQ(boost::cv_status::no_timeout,
+ destroyLatch->wait_for(boost::chrono::seconds(30)));
- ASSERT_EQ(testListener->getCreationCount(),
- CQ_PLUS_AUTH_TEST_REGION_ENTRY_COUNT);
- ASSERT_EQ(testListener->getUpdateCount(),
- CQ_PLUS_AUTH_TEST_REGION_ENTRY_COUNT);
- ASSERT_EQ(testListener->getDestructionCount(),
- CQ_PLUS_AUTH_TEST_REGION_ENTRY_COUNT);
- ASSERT_GT(authInitialize->getGetCredentialsCallCount(), 0);
+ EXPECT_GT(authInitialize->getGetCredentialsCallCount(), 0);
}
} // namespace
diff --git a/cppcache/integration/test/CqTest.cpp
b/cppcache/integration/test/CqTest.cpp
index ac2b2fe..75c366e 100644
--- a/cppcache/integration/test/CqTest.cpp
+++ b/cppcache/integration/test/CqTest.cpp
@@ -16,11 +16,10 @@
*/
#include <chrono>
-#include <future>
-#include <iostream>
-#include <random>
#include <thread>
+#include <boost/thread/latch.hpp>
+
#include <gtest/gtest.h>
#include <geode/CqAttributesFactory.hpp>
@@ -31,8 +30,6 @@
#include "CacheRegionHelper.hpp"
#include "SimpleCqListener.hpp"
#include "framework/Cluster.h"
-#include "framework/Framework.h"
-#include "framework/Gfsh.h"
namespace {
@@ -95,8 +92,14 @@ TEST(CqTest, testCqCreateUpdateDestroy) {
auto region = setupRegion(cache, pool);
auto queryService = cache.getQueryService();
+ auto createLatch =
std::make_shared<boost::latch>(CQ_TEST_REGION_ENTRY_COUNT);
+ auto updateLatch =
std::make_shared<boost::latch>(CQ_TEST_REGION_ENTRY_COUNT);
+ auto destroyLatch =
+ std::make_shared<boost::latch>(CQ_TEST_REGION_ENTRY_COUNT);
+ auto testListener = std::make_shared<SimpleCqListener>(
+ createLatch, updateLatch, destroyLatch);
+
CqAttributesFactory attributesFactory;
- auto testListener = std::make_shared<SimpleCqListener>();
attributesFactory.addCqListener(testListener);
auto cqAttributes = attributesFactory.create();
@@ -121,16 +124,12 @@ TEST(CqTest, testCqCreateUpdateDestroy) {
region->destroy("key" + std::to_string(i));
}
- for (int i = 0; i < 100; i++) {
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- if (testListener->getCreationCount() == CQ_TEST_REGION_ENTRY_COUNT) {
- break;
- }
- }
-
- ASSERT_EQ(testListener->getCreationCount(), CQ_TEST_REGION_ENTRY_COUNT);
- ASSERT_EQ(testListener->getUpdateCount(), CQ_TEST_REGION_ENTRY_COUNT);
- ASSERT_EQ(testListener->getDestructionCount(), CQ_TEST_REGION_ENTRY_COUNT);
+ EXPECT_EQ(boost::cv_status::no_timeout,
+ createLatch->wait_for(boost::chrono::seconds(30)));
+ EXPECT_EQ(boost::cv_status::no_timeout,
+ updateLatch->wait_for(boost::chrono::seconds(30)));
+ EXPECT_EQ(boost::cv_status::no_timeout,
+ destroyLatch->wait_for(boost::chrono::seconds(30)));
}
} // namespace
diff --git a/cppcache/integration/test/SimpleCqListener.cpp
b/cppcache/integration/test/SimpleCqListener.cpp
index 77e9778..1ff9d11 100644
--- a/cppcache/integration/test/SimpleCqListener.cpp
+++ b/cppcache/integration/test/SimpleCqListener.cpp
@@ -22,19 +22,23 @@
#include <geode/CqListener.hpp>
#include <geode/CqOperation.hpp>
-SimpleCqListener::SimpleCqListener()
- : creationCount_(0), updateCount_(0), destructionCount_(0) {}
+SimpleCqListener::SimpleCqListener(std::shared_ptr<boost::latch> createLatch,
+ std::shared_ptr<boost::latch> updateLatch,
+ std::shared_ptr<boost::latch> destroyLatch)
+ : createLatch_(createLatch),
+ updateLatch_(updateLatch),
+ destroyLatch_(destroyLatch) {}
void SimpleCqListener::onEvent(const apache::geode::client::CqEvent& cqEvent) {
switch (cqEvent.getQueryOperation()) {
case apache::geode::client::CqOperation::OP_TYPE_CREATE:
- creationCount_++;
+ createLatch_->count_down();
break;
case apache::geode::client::CqOperation::OP_TYPE_UPDATE:
- updateCount_++;
+ updateLatch_->count_down();
break;
case apache::geode::client::CqOperation::OP_TYPE_DESTROY:
- destructionCount_++;
+ destroyLatch_->count_down();
break;
default:
break;
@@ -52,9 +56,3 @@ void SimpleCqListener::onError(const
apache::geode::client::CqEvent& cqEvent) {
void SimpleCqListener::close() {
std::cout << __FUNCTION__ << " called" << std::endl;
}
-
-int32_t SimpleCqListener::getCreationCount() { return creationCount_; }
-
-int32_t SimpleCqListener::getUpdateCount() { return updateCount_; }
-
-int32_t SimpleCqListener::getDestructionCount() { return destructionCount_; }
diff --git a/cppcache/integration/test/SimpleCqListener.hpp
b/cppcache/integration/test/SimpleCqListener.hpp
index e42d381..043e7c0 100644
--- a/cppcache/integration/test/SimpleCqListener.hpp
+++ b/cppcache/integration/test/SimpleCqListener.hpp
@@ -20,25 +20,27 @@
#ifndef SIMPLE_CQ_LISTENER_H
#define SIMPLE_CQ_LISTENER_H
+#include <memory>
+
+#include <boost/thread/latch.hpp>
+
#include <geode/CacheableString.hpp>
#include <geode/CqListener.hpp>
#include <geode/CqOperation.hpp>
class SimpleCqListener : public apache::geode::client::CqListener {
public:
- SimpleCqListener();
+ SimpleCqListener(std::shared_ptr<boost::latch> createLatch,
+ std::shared_ptr<boost::latch> updateLatch,
+ std::shared_ptr<boost::latch> destroyLatch);
void onEvent(const apache::geode::client::CqEvent& cqEvent) override;
void onError(const apache::geode::client::CqEvent& cqEvent) override;
void close() override;
- int32_t getCreationCount();
- int32_t getUpdateCount();
- int32_t getDestructionCount();
-
private:
- int32_t creationCount_;
- int32_t updateCount_;
- int32_t destructionCount_;
+ std::shared_ptr<boost::latch> createLatch_;
+ std::shared_ptr<boost::latch> updateLatch_;
+ std::shared_ptr<boost::latch> destroyLatch_;
};
#endif // SIMPLE_CQ_LISTENER_H
diff --git a/dependencies/boost/CMakeLists.txt
b/dependencies/boost/CMakeLists.txt
index 6fd3e99..afe54a9 100644
--- a/dependencies/boost/CMakeLists.txt
+++ b/dependencies/boost/CMakeLists.txt
@@ -136,3 +136,4 @@ add_boost_library(thread DEPENDENCIES Threads::Threads
Boost::atomic Boost::boos
add_boost_library(filesystem DEPENDENCIES Boost::system Boost::boost)
add_boost_library(log DEPENDENCIES Boost::thread Boost::filesystem
Boost::boost)
add_boost_library(log_setup DEPENDENCIES Boost::log)
+add_boost_library(chrono DEPENDENCIES Boost::chrono)