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

tests: add multithread test

parent ba9a4452
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
#include "dhtrunnertester.h" #include "dhtrunnertester.h"
#include <opendht/thread_pool.h>
#include <chrono> #include <chrono>
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
...@@ -298,4 +300,39 @@ DhtRunnerTester::testListenLotOfBytes() { ...@@ -298,4 +300,39 @@ DhtRunnerTester::testListenLotOfBytes() {
node3.cancelListen(foo, ftokenfoo.get()); node3.cancelListen(foo, ftokenfoo.get());
} }
void
DhtRunnerTester::testMultithread() {
std::mutex mutex;
std::condition_variable cv;
unsigned putCount(0);
unsigned putOkCount(0);
constexpr unsigned N = 2048;
for (unsigned i=0; i<N; i++) {
dht::ThreadPool::computation().run([&]{
node2.put(dht::InfoHash::get("123" + std::to_string(i)), "hehe", [&](bool ok) {
std::lock_guard<std::mutex> lock(mutex);
putCount++;
if (ok) putOkCount++;
cv.notify_all();
});
node2.get(dht::InfoHash::get("123" + std::to_string(N-i-1)), [](const std::shared_ptr<dht::Value>&){
return true;
}, [&](bool ok) {
std::lock_guard<std::mutex> lock(mutex);
putCount++;
if (ok) putOkCount++;
cv.notify_all();
});
});
}
std::unique_lock<std::mutex> lk(mutex);
CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&]{ return putCount == 2*N; }));
CPPUNIT_ASSERT_EQUAL(2*N, putOkCount);
}
} // namespace test } // namespace test
...@@ -67,6 +67,11 @@ class DhtRunnerTester : public CppUnit::TestFixture { ...@@ -67,6 +67,11 @@ class DhtRunnerTester : public CppUnit::TestFixture {
* Test listen method with lot of datas * Test listen method with lot of datas
*/ */
void testListenLotOfBytes(); void testListenLotOfBytes();
/**
* Test multithread
*/
void testMultithread();
}; };
} // 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