diff --git a/tests/dhtproxytester.cpp b/tests/dhtproxytester.cpp index ff421688b7414b87bffbbb18f6e60b01a026555e..1f128ef45b1a58d014b282be20c6b680d6ef7604 100644 --- a/tests/dhtproxytester.cpp +++ b/tests/dhtproxytester.cpp @@ -51,27 +51,79 @@ DhtProxyTester::tearDown() { nodePeer.join(); nodeClient->join(); server->stop(); + server = nullptr; nodeProxy->join(); } void DhtProxyTester::testGetPut() { - auto cv = std::make_shared<std::condition_variable>(); + bool done = false; + std::condition_variable cv; std::mutex cv_m; auto key = dht::InfoHash::get("GLaDOs"); - dht::Value val {"Hei! It's been a long time. How have you been?"}; + dht::Value val {"Hey! It's been a long time. How have you been?"}; auto val_data = val.data; - nodePeer.put(key, std::move(val), [cv](bool) { - cv->notify_all(); + nodePeer.put(key, std::move(val), [&](bool) { + done = true; + cv.notify_all(); }); std::unique_lock<std::mutex> lk(cv_m); - cv->wait_for(lk, std::chrono::seconds(10)); + cv.wait_for(lk, std::chrono::seconds(10), [&]{ return done; }); auto vals = nodeClient->get(key).get(); CPPUNIT_ASSERT(not vals.empty()); CPPUNIT_ASSERT(vals.front()->data == val_data); } + +void +DhtProxyTester::testListen() { + bool done = false; + std::condition_variable cv; + std::mutex cv_m; + std::unique_lock<std::mutex> lk(cv_m); + auto key = dht::InfoHash::get("GLaDOs"); + + // If a peer send a value, the listen operation from the client + // should retrieve this value + dht::Value firstVal {"Hey! It's been a long time. How have you been?"}; + auto firstVal_data = firstVal.data; + nodePeer.put(key, std::move(firstVal), [&](bool) { + done = true; + cv.notify_all(); + }); + cv.wait_for(lk, std::chrono::seconds(10), [&]{ return done; }); + done = false; + + auto values = std::vector<dht::Blob>(); + nodeClient->listen(key, [&](const std::vector<std::shared_ptr<dht::Value>>& v, bool) { + for (const auto& value : v) + values.emplace_back(value->data); + done = true; + cv.notify_all(); + return true; + }); + + cv.wait_for(lk, std::chrono::seconds(10), [&]{ return done; }); + done = false; + // Here values should contains 2 values + CPPUNIT_ASSERT_EQUAL(static_cast<int>(values.size()), 1); + CPPUNIT_ASSERT(values.front() == firstVal_data); + + // And the listen should retrieve futures values + // All values + dht::Value secondVal {"You're a monster"}; + auto secondVal_data = secondVal.data; + nodePeer.put(key, std::move(secondVal), [&](bool) { + done = true; + cv.notify_all(); + }); + cv.wait_for(lk, std::chrono::seconds(10), [&]{ return done; }); + // Here values should contains 3 values + CPPUNIT_ASSERT_EQUAL(static_cast<int>(values.size()), 2); + CPPUNIT_ASSERT(values.back() == secondVal_data); +} + } // namespace test diff --git a/tests/dhtproxytester.h b/tests/dhtproxytester.h index c086b787ae4f466bb0b0c2a2d4c88dca26e8e36b..fca4af523e7ae9be0f005c7d674073f418661c2c 100644 --- a/tests/dhtproxytester.h +++ b/tests/dhtproxytester.h @@ -31,6 +31,7 @@ namespace test { class DhtProxyTester : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(DhtProxyTester); CPPUNIT_TEST(testGetPut); + CPPUNIT_TEST(testListen); CPPUNIT_TEST_SUITE_END(); public: @@ -47,6 +48,10 @@ class DhtProxyTester : public CppUnit::TestFixture { * Test get and put methods */ void testGetPut(); + /** + * Test listen + */ + void testListen(); private: dht::DhtRunner nodePeer {};