Permalink
Browse files

tests: test retrieve values when resubscribe

  • Loading branch information...
AmarOk1412 authored and aberaud committed Jan 9, 2019
1 parent 3a3933e commit d523e9f259aa2e90e0c56b7f66324422422c9e2b
Showing with 69 additions and 7 deletions.
  1. +2 −0 src/dht_proxy_server.cpp
  2. +56 −3 tests/dhtproxytester.cpp
  3. +11 −4 tests/dhtproxytester.h
@@ -558,6 +558,8 @@ DhtProxyServer::cancelPushListen(const std::string& pushToken, const dht::InfoHa
void
DhtProxyServer::sendPushNotification(const std::string& token, const Json::Value& json, bool isAndroid) const
{
if (pushServer_.empty())
return;
restbed::Uri uri(proxy::HTTP_PROTO + pushServer_ + "/api/push");
auto req = std::make_shared<restbed::Request>(uri);
req->set_method("POST");
@@ -77,7 +77,6 @@ DhtProxyTester::testGetPut() {
CPPUNIT_ASSERT(vals.front()->data == val_data);
}


void
DhtProxyTester::testListen() {
bool done = false;
@@ -108,7 +107,7 @@ DhtProxyTester::testListen() {

cv.wait_for(lk, std::chrono::seconds(10), [&]{ return done; });
done = false;
// Here values should contains 2 values
// Here values should contains 1 values
CPPUNIT_ASSERT_EQUAL(static_cast<int>(values.size()), 1);
CPPUNIT_ASSERT(values.front() == firstVal_data);

@@ -118,9 +117,63 @@ DhtProxyTester::testListen() {
auto secondVal_data = secondVal.data;
nodePeer.put(key, std::move(secondVal));
cv.wait_for(lk, std::chrono::seconds(10), [&]{ return done; });
// Here values should contains 3 values
// Here values should contains 2 values
CPPUNIT_ASSERT_EQUAL(static_cast<int>(values.size()), 2);
CPPUNIT_ASSERT(values.back() == secondVal_data);
}

void
DhtProxyTester::testResubscribeGetValues() {
nodeClient->setPushNotificationToken("atlas");

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;

// Send a first subscribe, the value is sent via a push notification
// So ignore values here.
nodeClient->listen(key, [&](const std::vector<std::shared_ptr<dht::Value>>&, bool) {
return true;
});
cv.wait_for(lk, std::chrono::seconds(1));

// Reboot node (to avoid cache)
nodeClient->join();
nodeClient->run(42242, {}, true);
nodeClient->bootstrap(nodePeer.getBound());
nodeClient->setProxyServer("127.0.0.1:8080");
nodeClient->enableProxy(true);
nodeClient->setPushNotificationToken("atlas");

// For the second subscribe, the proxy will return the value in the body
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 still contains 1 values
CPPUNIT_ASSERT_EQUAL(static_cast<int>(values.size()), 1);
CPPUNIT_ASSERT(values.front() == firstVal_data);

}

} // namespace test
@@ -32,26 +32,33 @@ class DhtProxyTester : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DhtProxyTester);
CPPUNIT_TEST(testGetPut);
CPPUNIT_TEST(testListen);
CPPUNIT_TEST(testResubscribeGetValues);
CPPUNIT_TEST_SUITE_END();

public:
/**
* Method automatically called before each test by CppUnit
* Init nodes
*/
void setUp();
void setUp();
/**
* Method automatically called after each test CppUnit
*/
void tearDown();
void tearDown();
/**
* Test get and put methods
*/
void testGetPut();
void testGetPut();
/**
* Test listen
*/
void testListen();
void testListen();

/**
* When a proxy redo a subscribe on the proxy
* it should retrieve existant values
*/
void testResubscribeGetValues();

private:
dht::DhtRunner nodePeer {};

0 comments on commit d523e9f

Please sign in to comment.