Skip to content
Snippets Groups Projects
Unverified Commit c583b728 authored by Sébastien Blin's avatar Sébastien Blin
Browse files

tests: add testListen for DhtProxyTester

parent 7f943e07
No related branches found
No related tags found
No related merge requests found
...@@ -51,27 +51,79 @@ DhtProxyTester::tearDown() { ...@@ -51,27 +51,79 @@ DhtProxyTester::tearDown() {
nodePeer.join(); nodePeer.join();
nodeClient->join(); nodeClient->join();
server->stop(); server->stop();
server = nullptr;
nodeProxy->join(); nodeProxy->join();
} }
void void
DhtProxyTester::testGetPut() { DhtProxyTester::testGetPut() {
auto cv = std::make_shared<std::condition_variable>(); bool done = false;
std::condition_variable cv;
std::mutex cv_m; std::mutex cv_m;
auto key = dht::InfoHash::get("GLaDOs"); 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; auto val_data = val.data;
nodePeer.put(key, std::move(val), [cv](bool) { nodePeer.put(key, std::move(val), [&](bool) {
cv->notify_all(); done = true;
cv.notify_all();
}); });
std::unique_lock<std::mutex> lk(cv_m); 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(); auto vals = nodeClient->get(key).get();
CPPUNIT_ASSERT(not vals.empty()); CPPUNIT_ASSERT(not vals.empty());
CPPUNIT_ASSERT(vals.front()->data == val_data); 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 } // namespace test
...@@ -31,6 +31,7 @@ namespace test { ...@@ -31,6 +31,7 @@ namespace test {
class DhtProxyTester : public CppUnit::TestFixture { class DhtProxyTester : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DhtProxyTester); CPPUNIT_TEST_SUITE(DhtProxyTester);
CPPUNIT_TEST(testGetPut); CPPUNIT_TEST(testGetPut);
CPPUNIT_TEST(testListen);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
public: public:
...@@ -47,6 +48,10 @@ class DhtProxyTester : public CppUnit::TestFixture { ...@@ -47,6 +48,10 @@ class DhtProxyTester : public CppUnit::TestFixture {
* Test get and put methods * Test get and put methods
*/ */
void testGetPut(); void testGetPut();
/**
* Test listen
*/
void testListen();
private: private:
dht::DhtRunner nodePeer {}; dht::DhtRunner nodePeer {};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment