Skip to content
Snippets Groups Projects
Commit c4ec1e78 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

tests: fix occasional testListen failure

parent e86d717e
No related branches found
No related tags found
No related merge requests found
...@@ -19,9 +19,9 @@ ...@@ -19,9 +19,9 @@
#include "dhtrunnertester.h" #include "dhtrunnertester.h"
// std #include <chrono>
#include <iostream> #include <mutex>
#include <string> #include <condition_variable>
namespace test { namespace test {
CPPUNIT_TEST_SUITE_REGISTRATION(DhtRunnerTester); CPPUNIT_TEST_SUITE_REGISTRATION(DhtRunnerTester);
...@@ -62,13 +62,16 @@ DhtRunnerTester::testGetPut() { ...@@ -62,13 +62,16 @@ DhtRunnerTester::testGetPut() {
void void
DhtRunnerTester::testListen() { DhtRunnerTester::testListen() {
std::mutex mutex;
std::condition_variable cv;
std::atomic_uint valueCount(0); std::atomic_uint valueCount(0);
std::atomic_uint putCount(0); unsigned putCount(0);
unsigned putOkCount(0);
auto a = dht::InfoHash::get("234"); auto a = dht::InfoHash::get("234");
auto b = dht::InfoHash::get("2345"); auto b = dht::InfoHash::get("2345");
auto c = dht::InfoHash::get("23456"); auto c = dht::InfoHash::get("23456");
constexpr unsigned N = 32; constexpr unsigned N = 64;
auto ftokena = node1.listen(a, [&](const std::shared_ptr<dht::Value>&) { auto ftokena = node1.listen(a, [&](const std::shared_ptr<dht::Value>&) {
valueCount++; valueCount++;
...@@ -86,11 +89,31 @@ DhtRunnerTester::testListen() { ...@@ -86,11 +89,31 @@ DhtRunnerTester::testListen() {
}); });
for (unsigned i=0; i<N; i++) { for (unsigned i=0; i<N; i++) {
node2.put(a, dht::Value("v1"), [&](bool ok) { if (ok) putCount++; }); node2.put(a, dht::Value("v1"), [&](bool ok) {
node2.put(b, dht::Value("v2"), [&](bool ok) { if (ok) putCount++; }); {
std::lock_guard<std::mutex> lock(mutex);
putCount++;
if (ok) putOkCount++;
}
cv.notify_all();
});
node2.put(b, dht::Value("v2"), [&](bool ok) {
{
std::lock_guard<std::mutex> lock(mutex);
putCount++;
if (ok) putOkCount++;
}
cv.notify_all();
});
}
{
std::unique_lock<std::mutex> lk(mutex);
cv.wait_for(lk, std::chrono::seconds(30), [&]{ return putCount == N * 2u; });
CPPUNIT_ASSERT_EQUAL(N * 2u, putCount);
CPPUNIT_ASSERT_EQUAL(N * 2u, putOkCount);
} }
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto tokena = ftokena.get(); auto tokena = ftokena.get();
auto tokenb = ftokenb.get(); auto tokenb = ftokenb.get();
auto tokenc = ftokenc.get(); auto tokenc = ftokenc.get();
...@@ -98,8 +121,11 @@ DhtRunnerTester::testListen() { ...@@ -98,8 +121,11 @@ DhtRunnerTester::testListen() {
CPPUNIT_ASSERT(tokena); CPPUNIT_ASSERT(tokena);
CPPUNIT_ASSERT(tokenb); CPPUNIT_ASSERT(tokenb);
CPPUNIT_ASSERT(tokenc); CPPUNIT_ASSERT(tokenc);
CPPUNIT_ASSERT_EQUAL(N * 2u, putCount.load());
CPPUNIT_ASSERT_EQUAL(N + 1u, valueCount.load()); CPPUNIT_ASSERT_EQUAL(N + 1u, valueCount.load());
node1.cancelListen(a, tokena);
node1.cancelListen(b, tokena);
node1.cancelListen(c, tokena);
} }
} // namespace test } // namespace test
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment