ndn-handle: Implement TCP backdoor to inject data packets into repo
Change-Id: I74d0698f914a2e68d47ede427c7f36b3ba3b4f47
Refs: #1485
diff --git a/tests/tcp-bulk-insert-handle.cpp b/tests/tcp-bulk-insert-handle.cpp
new file mode 100644
index 0000000..ffe002b
--- /dev/null
+++ b/tests/tcp-bulk-insert-handle.cpp
@@ -0,0 +1,172 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "../ndn-handle/tcp-bulk-insert-handle.hpp"
+
+#include "sqlite-fixture.hpp"
+#include "dataset-fixtures.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace repo {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(TcpBulkInsertHandle)
+
+class TcpClient
+{
+public:
+ TcpClient()
+ : socket(ioService)
+ {
+ }
+
+ void
+ start(const std::string& host, const std::string& port)
+ {
+ using namespace boost::asio;
+
+ ip::tcp::resolver resolver(ioService);
+ ip::tcp::resolver::query query(host, port);
+
+ ip::tcp::resolver::iterator endpoint = resolver.resolve(query);
+ ip::tcp::resolver::iterator end;
+
+ if (endpoint == end)
+ BOOST_FAIL("Cannot resolve [" + host + ":" + port + "]");
+
+ ip::tcp::endpoint serverEndpoint = *endpoint;
+
+ socket.async_connect(serverEndpoint,
+ bind(&TcpClient::onSuccessfullConnect, this, _1));
+ }
+
+ virtual void
+ onSuccessfullConnect(const boost::system::error_code& error)
+ {
+ if (error)
+ {
+ BOOST_FAIL("TCP connection aborted");
+ return;
+ }
+ }
+
+public:
+ boost::asio::io_service ioService;
+ boost::asio::ip::tcp::socket socket;
+};
+
+template<class Dataset>
+class TcpBulkInsertFixture : public TcpClient,
+ public SqliteFixture,
+ public Dataset
+{
+public:
+ TcpBulkInsertFixture()
+ : scheduler(ioService)
+ , bulkInserter(ioService, *handle)
+ {
+ guardEvent = scheduler.scheduleEvent(ndn::time::seconds(2),
+ bind(&TcpBulkInsertFixture::fail, this, "Test timed out"));
+ }
+
+ virtual void
+ onSuccessfullConnect(const boost::system::error_code& error)
+ {
+ TcpClient::onSuccessfullConnect(error);
+
+ // This value may need to be adjusted if some dataset exceeds 100k
+ socket.set_option(boost::asio::socket_base::send_buffer_size(100000));
+
+ // Initially I wrote the following to use scatter-gather approach (using
+ // std::vector<const_buffer> and a single socket.async_send operation). Unfortunately, as
+ // described in http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/overview/implementation.html,
+ // scatter-gather is limited to at most `min(64,IOV_MAX)` buffers to be transmitted
+ // in a single operation
+ for (typename Dataset::DataContainer::iterator i = this->data.begin();
+ i != this->data.end(); ++i) {
+
+ socket.async_send(boost::asio::buffer((*i)->wireEncode().wire(), (*i)->wireEncode().size()),
+ bind(&TcpBulkInsertFixture::onSendFinished, this, _1, false));
+ }
+
+ socket.async_send(boost::asio::buffer(static_cast<const uint8_t*>(0), 0),
+ bind(&TcpBulkInsertFixture::onSendFinished, this, _1, true));
+ }
+
+ void
+ onSendFinished(const boost::system::error_code& error, bool isFinal)
+ {
+ if (error) {
+ BOOST_FAIL("TCP connection aborted");
+ return;
+ }
+
+ if (isFinal) {
+ scheduler.cancelEvent(guardEvent);
+
+ // In case there are some outstanding handlers
+ // ioService.post(bind(&TcpBulkInsertFixture::stop, this));
+ scheduler.scheduleEvent(ndn::time::seconds(1),
+ bind(&TcpBulkInsertFixture::stop, this));
+ }
+ }
+
+ void
+ fail(const std::string& info)
+ {
+ ioService.stop();
+ BOOST_FAIL(info);
+ }
+
+ void
+ stop()
+ {
+ // Terminate test
+ socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
+ socket.close();
+
+ bulkInserter.stop();
+ // may be ioService.stop() as well
+ }
+
+public:
+ Scheduler scheduler;
+ ndn::EventId guardEvent;
+ repo::TcpBulkInsertHandle bulkInserter;
+};
+
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(BulkInsertAndRead, T, DatasetFixtures, TcpBulkInsertFixture<T>)
+{
+ BOOST_TEST_MESSAGE(T::getName());
+ // BOOST_CHECK_EQUAL(this->handle->size(), 1);
+
+ // start bulk inserter
+ this->bulkInserter.listen("localhost", "17376");
+
+ // start test
+ this->start("localhost", "17376");
+
+ // actually run the test
+ this->ioService.run();
+
+ BOOST_CHECK_EQUAL(this->handle->size(), this->data.size());
+
+ // Read (all items should exist)
+ for (typename T::InterestContainer::iterator i = this->interests.begin();
+ i != this->interests.end(); ++i) {
+ ndn::Data retrievedData;
+ BOOST_REQUIRE_EQUAL(this->handle->readData(i->first, retrievedData), true);
+ BOOST_CHECK_EQUAL(retrievedData, *i->second);
+ }
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace repo
+} // namespace tests