tests: Converting test cases to use UnitTestClock
Change-Id: I3ceaf048cc60f9cdbf98ace43f96d5c81047efd0
diff --git a/tests/unit-tests/util/notification-stream.cpp b/tests/unit-tests/util/notification-stream.cpp
index adba5c7..4b6d43d 100644
--- a/tests/unit-tests/util/notification-stream.cpp
+++ b/tests/unit-tests/util/notification-stream.cpp
@@ -47,26 +47,29 @@
#include "util/notification-stream.hpp"
#include "simple-notification.hpp"
+#include "util/dummy-client-face.hpp"
#include "boost-test.hpp"
-#include "util/dummy-client-face.hpp"
+#include "../unit-test-time-fixture.hpp"
namespace ndn {
namespace util {
namespace tests {
-BOOST_AUTO_TEST_SUITE(UtilNotificationStream)
+BOOST_FIXTURE_TEST_SUITE(UtilNotificationStream, ndn::tests::UnitTestTimeFixture)
BOOST_AUTO_TEST_CASE(Post)
{
- shared_ptr<DummyClientFace> face = makeDummyClientFace();
+ shared_ptr<DummyClientFace> face = makeDummyClientFace(io);
ndn::KeyChain keyChain;
util::NotificationStream<SimpleNotification> notificationStream(*face,
"/localhost/nfd/NotificationStreamTest", keyChain);
SimpleNotification event1("msg1");
notificationStream.postNotification(event1);
- face->processEvents();
+
+ advanceClocks(time::milliseconds(1));
+
BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
BOOST_CHECK_EQUAL(face->sentDatas[0].getName(),
"/localhost/nfd/NotificationStreamTest/%FE%00");
@@ -76,7 +79,9 @@
SimpleNotification event2("msg2");
notificationStream.postNotification(event2);
- face->processEvents();
+
+ advanceClocks(time::milliseconds(1));
+
BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 2);
BOOST_CHECK_EQUAL(face->sentDatas[1].getName(),
"/localhost/nfd/NotificationStreamTest/%FE%01");
diff --git a/tests/unit-tests/util/notification-subscriber.cpp b/tests/unit-tests/util/notification-subscriber.cpp
index 9d7ab65..96f5bf4 100644
--- a/tests/unit-tests/util/notification-subscriber.cpp
+++ b/tests/unit-tests/util/notification-subscriber.cpp
@@ -48,10 +48,10 @@
#include "util/notification-subscriber.hpp"
#include "util/notification-stream.hpp"
#include "simple-notification.hpp"
+#include "util/dummy-client-face.hpp"
#include "boost-test.hpp"
-#include <boost/asio.hpp>
-#include "util/dummy-client-face.hpp"
+#include "../unit-test-time-fixture.hpp"
namespace ndn {
namespace util {
@@ -59,14 +59,14 @@
BOOST_AUTO_TEST_SUITE(UtilNotificationSubscriber)
-class EndToEndFixture
+class EndToEndFixture : public ndn::tests::UnitTestTimeFixture
{
public:
EndToEndFixture()
: streamPrefix("ndn:/NotificationSubscriberTest")
- , publisherFace(makeDummyClientFace(ioService))
+ , publisherFace(makeDummyClientFace(io))
, notificationStream(*publisherFace, streamPrefix, publisherKeyChain)
- , subscriberFace(makeDummyClientFace(ioService))
+ , subscriberFace(makeDummyClientFace(io))
, subscriber(*subscriberFace, streamPrefix, time::seconds(1))
{
}
@@ -80,7 +80,7 @@
SimpleNotification notification(msg);
notificationStream.postNotification(notification);
- publisherFace->processEvents(time::milliseconds(100));
+ advanceClocks(time::milliseconds(1));
BOOST_REQUIRE_EQUAL(publisherFace->sentDatas.size(), 1);
@@ -149,7 +149,6 @@
}
protected:
- boost::asio::io_service ioService;
Name streamPrefix;
shared_ptr<DummyClientFace> publisherFace;
ndn::KeyChain publisherKeyChain;
@@ -178,34 +177,34 @@
// not received when subscriber is not running
this->deliverNotification("n1");
- subscriberFace->processEvents(time::milliseconds(100));
+ advanceClocks(time::milliseconds(1));
BOOST_CHECK(lastNotification.getMessage().empty());
BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
subscriberFace->sentInterests.clear();
subscriber.start();
- subscriberFace->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1));
BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true);
BOOST_CHECK(this->hasInitialRequest());
// respond to initial request
subscriberFace->sentInterests.clear();
this->deliverNotification("n2");
- subscriberFace->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1));
BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2");
BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
// respond to continuation request
subscriberFace->sentInterests.clear();
this->deliverNotification("n3");
- subscriberFace->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1));
BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3");
BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1);
// timeout
subscriberFace->sentInterests.clear();
lastNotification.setMessage("");
- subscriberFace->processEvents(2 * subscriber.getInterestLifetime());
+ advanceClocks(subscriber.getInterestLifetime(), 2);
BOOST_CHECK(lastNotification.getMessage().empty());
BOOST_CHECK_EQUAL(hasTimeout, true);
BOOST_CHECK(this->hasInitialRequest());
@@ -218,7 +217,7 @@
subscriberFace->receive(wrongData);
subscriberFace->sentInterests.clear();
lastNotification.setMessage("");
- subscriberFace->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1));
BOOST_CHECK(lastNotification.getMessage().empty());
BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName);
BOOST_CHECK(this->hasInitialRequest());
@@ -227,7 +226,7 @@
subscriberFace->sentInterests.clear();
lastNotification.setMessage("");
this->deliverNotification("\x07n4");
- subscriberFace->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1));
BOOST_CHECK(lastNotification.getMessage().empty());
BOOST_CHECK(this->hasInitialRequest());
@@ -235,7 +234,7 @@
subscriber.onNotification += bind(&EndToEndFixture::clearNotificationHandlers, this);
subscriberFace->sentInterests.clear();
this->deliverNotification("n5");
- subscriberFace->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1));
BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0);
}
diff --git a/tests/unit-tests/util/segment-fetcher.cpp b/tests/unit-tests/util/segment-fetcher.cpp
index 3a9fc5f..15d7831 100644
--- a/tests/unit-tests/util/segment-fetcher.cpp
+++ b/tests/unit-tests/util/segment-fetcher.cpp
@@ -24,6 +24,7 @@
#include "boost-test.hpp"
#include "util/dummy-client-face.hpp"
#include "security/key-chain.hpp"
+#include "../unit-test-time-fixture.hpp"
namespace ndn {
namespace util {
@@ -31,11 +32,11 @@
BOOST_AUTO_TEST_SUITE(UtilSegmentFetcher)
-class Fixture
+class Fixture : public ndn::tests::UnitTestTimeFixture
{
public:
Fixture()
- : face(makeDummyClientFace())
+ : face(makeDummyClientFace(io))
, nErrors(0)
, nDatas(0)
, dataSize(0)
@@ -89,10 +90,9 @@
bind(&Fixture::onData, this, _1),
bind(&Fixture::onError, this, _1));
- face->processEvents(time::seconds(1));
+ advanceClocks(time::milliseconds(1), 99);
- BOOST_CHECK_EQUAL(nErrors, 1);
- BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
+ BOOST_CHECK_EQUAL(nErrors, 0);
BOOST_CHECK_EQUAL(nDatas, 0);
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
@@ -101,21 +101,27 @@
BOOST_CHECK_EQUAL(interest.getName(), "/hello/world");
BOOST_CHECK_EQUAL(interest.getMustBeFresh(), true);
BOOST_CHECK_EQUAL(interest.getChildSelector(), 1);
+
+ advanceClocks(time::milliseconds(1), 2);
+
+ BOOST_CHECK_EQUAL(nErrors, 1);
+ BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::INTEREST_TIMEOUT));
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
+ BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
}
BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
{
-
SegmentFetcher::fetch(*face, Interest("/hello/world", time::seconds(1000)),
DontVerifySegment(),
bind(&Fixture::onData, this, _1),
bind(&Fixture::onError, this, _1));
+ advanceClocks(time::milliseconds(1), 10);
- face->processEvents(time::milliseconds(-100));
face->receive(*makeData("/hello/world/version0", 0, true));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
BOOST_CHECK_EQUAL(nErrors, 0);
BOOST_CHECK_EQUAL(nDatas, 1);
@@ -139,7 +145,7 @@
bind(&Fixture::onData, this, _1),
bind(&Fixture::onError, this, _1));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
const uint8_t buffer[] = "Hello, world!";
@@ -148,7 +154,7 @@
keyChain.sign(*data);
face->receive(*data);
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
BOOST_CHECK_EQUAL(nErrors, 1);
BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::DATA_HAS_NO_SEGMENT));
@@ -169,9 +175,9 @@
bind(&Fixture::onData, this, _1),
bind(&Fixture::onError, this, _1));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
face->receive(*makeData("/hello/world/version0", 0, true));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
BOOST_CHECK_EQUAL(nErrors, 1);
BOOST_CHECK_EQUAL(lastError, static_cast<uint32_t>(SegmentFetcher::SEGMENT_VERIFICATION_FAIL));
@@ -188,16 +194,16 @@
bind(&Fixture::onData, this, _1),
bind(&Fixture::onError, this, _1));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
face->receive(*makeData("/hello/world/version0", 0, false));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
face->receive(*makeData("/hello/world/version0", 1, false));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
face->receive(*makeData("/hello/world/version0", 2, true));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
BOOST_CHECK_EQUAL(nErrors, 0);
BOOST_CHECK_EQUAL(nDatas, 1);
@@ -238,19 +244,19 @@
bind(&Fixture::onData, this, _1),
bind(&Fixture::onError, this, _1));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
face->receive(*makeData("/hello/world/version0", 1, false));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
face->receive(*makeData("/hello/world/version0", 0, false));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
face->receive(*makeData("/hello/world/version0", 1, false));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
face->receive(*makeData("/hello/world/version0", 2, true));
- face->processEvents(time::milliseconds(-100));
+ advanceClocks(time::milliseconds(1), 10);
BOOST_CHECK_EQUAL(nErrors, 0);
BOOST_CHECK_EQUAL(nDatas, 1);
diff --git a/tests/unit-tests/util/test-scheduler.cpp b/tests/unit-tests/util/test-scheduler.cpp
index 04d4f99..c8c8179 100644
--- a/tests/unit-tests/util/test-scheduler.cpp
+++ b/tests/unit-tests/util/test-scheduler.cpp
@@ -22,253 +22,186 @@
#include "util/scheduler.hpp"
#include "boost-test.hpp"
+#include "../unit-test-time-fixture.hpp"
namespace ndn {
+namespace tests {
-BOOST_AUTO_TEST_SUITE(UtilTestScheduler)
+BOOST_FIXTURE_TEST_SUITE(UtilTestScheduler, UnitTestTimeFixture)
-struct SchedulerFixture
+BOOST_AUTO_TEST_CASE(Events)
{
- SchedulerFixture()
- : count1(0)
- , count2(0)
- , count3(0)
- {
- }
-
- void
- event1()
- {
- BOOST_CHECK_EQUAL(count3, 1);
- ++count1;
- }
-
- void
- event2()
- {
- ++count2;
- }
-
- void
- event3()
- {
- BOOST_CHECK_EQUAL(count1, 0);
- ++count3;
- }
-
- int count1;
- int count2;
- int count3;
-};
-
-BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
-{
- boost::asio::io_service io;
+ size_t count1 = 0;
+ size_t count2 = 0;
Scheduler scheduler(io);
- scheduler.scheduleEvent(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
+ scheduler.scheduleEvent(time::milliseconds(500), [&] {
+ ++count1;
+ BOOST_CHECK_EQUAL(count2, 1);
+ });
- EventId i = scheduler.scheduleEvent(time::seconds(1), bind(&SchedulerFixture::event2, this));
+ EventId i = scheduler.scheduleEvent(time::seconds(1), [&] {
+ BOOST_ERROR("This event should not have been fired");
+ });
scheduler.cancelEvent(i);
- scheduler.scheduleEvent(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
+ scheduler.scheduleEvent(time::milliseconds(250), [&] {
+ BOOST_CHECK_EQUAL(count1, 0);
+ ++count2;
+ });
- i = scheduler.scheduleEvent(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
+ i = scheduler.scheduleEvent(time::milliseconds(50), [&] {
+ BOOST_ERROR("This event should not have been fired");
+ });
scheduler.cancelEvent(i);
- io.run();
-
+ advanceClocks(time::milliseconds(1), 1000);
BOOST_CHECK_EQUAL(count1, 1);
- BOOST_CHECK_EQUAL(count2, 0);
- BOOST_CHECK_EQUAL(count3, 1);
+ BOOST_CHECK_EQUAL(count2, 1);
}
BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
{
- boost::asio::io_service io;
Scheduler scheduler(io);
EventId i;
scheduler.cancelEvent(i);
}
-struct SelfCancelFixture
+BOOST_AUTO_TEST_CASE(SelfCancel)
{
- SelfCancelFixture()
- : m_scheduler(m_io)
- {
- }
+ Scheduler scheduler(io);
- void
- cancelSelf()
- {
- m_scheduler.cancelEvent(m_selfEventId);
- }
+ EventId selfEventId;
+ selfEventId = scheduler.scheduleEvent(time::milliseconds(100), [&] {
+ scheduler.cancelEvent(selfEventId);
+ });
- boost::asio::io_service m_io;
- Scheduler m_scheduler;
- EventId m_selfEventId;
-};
-
-BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
-{
- m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
- bind(&SelfCancelFixture::cancelSelf, this));
-
- BOOST_REQUIRE_NO_THROW(m_io.run());
+ BOOST_REQUIRE_NO_THROW(advanceClocks(time::milliseconds(100), 10));
}
-struct SelfRescheduleFixture
+class SelfRescheduleFixture : public UnitTestTimeFixture
{
+public:
SelfRescheduleFixture()
- : m_scheduler(m_io)
- , m_count(0)
+ : scheduler(io)
+ , count(0)
{
}
void
reschedule()
{
- EventId eventId = m_scheduler.scheduleEvent(time::milliseconds(100),
- bind(&SelfRescheduleFixture::reschedule, this));
- m_scheduler.cancelEvent(m_selfEventId);
- m_selfEventId = eventId;
+ EventId eventId = scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::reschedule, this));
+ scheduler.cancelEvent(selfEventId);
+ selfEventId = eventId;
- if(m_count < 5)
- m_count++;
+ if (count < 5)
+ count++;
else
- m_scheduler.cancelEvent(m_selfEventId);
+ scheduler.cancelEvent(selfEventId);
}
void
reschedule2()
{
- m_scheduler.cancelEvent(m_selfEventId);
+ scheduler.cancelEvent(selfEventId);
-
- if(m_count < 5)
- {
- m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
- bind(&SelfRescheduleFixture::reschedule2, this));
- m_count++;
- }
- }
-
- void
- doNothing()
- {
- m_count++;
+ if (count < 5) {
+ selfEventId = scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::reschedule2, this));
+ count++;
+ }
}
void
reschedule3()
{
- m_scheduler.cancelEvent(m_selfEventId);
+ scheduler.cancelEvent(selfEventId);
- m_scheduler.scheduleEvent(time::milliseconds(100),
- bind(&SelfRescheduleFixture::doNothing, this));
- m_scheduler.scheduleEvent(time::milliseconds(100),
- bind(&SelfRescheduleFixture::doNothing, this));
- m_scheduler.scheduleEvent(time::milliseconds(100),
- bind(&SelfRescheduleFixture::doNothing, this));
- m_scheduler.scheduleEvent(time::milliseconds(100),
- bind(&SelfRescheduleFixture::doNothing, this));
- m_scheduler.scheduleEvent(time::milliseconds(100),
- bind(&SelfRescheduleFixture::doNothing, this));
- m_scheduler.scheduleEvent(time::milliseconds(100),
- bind(&SelfRescheduleFixture::doNothing, this));
+ scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
+ scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
+ scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
+ scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
+ scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
+ scheduler.scheduleEvent(time::milliseconds(100), [&] { ++count; });
}
- boost::asio::io_service m_io;
- Scheduler m_scheduler;
- EventId m_selfEventId;
- int m_count;
-
+ Scheduler scheduler;
+ EventId selfEventId;
+ size_t count;
};
BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
{
- m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
- bind(&SelfRescheduleFixture::reschedule, this));
+ selfEventId = scheduler.scheduleEvent(time::seconds(0),
+ bind(&SelfRescheduleFixture::reschedule, this));
- BOOST_REQUIRE_NO_THROW(m_io.run());
+ BOOST_REQUIRE_NO_THROW(advanceClocks(time::milliseconds(10), 1000));
- BOOST_CHECK_EQUAL(m_count, 5);
+ BOOST_CHECK_EQUAL(count, 5);
}
BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
{
- m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
- bind(&SelfRescheduleFixture::reschedule2, this));
+ selfEventId = scheduler.scheduleEvent(time::seconds(0),
+ bind(&SelfRescheduleFixture::reschedule2, this));
- BOOST_REQUIRE_NO_THROW(m_io.run());
+ BOOST_REQUIRE_NO_THROW(advanceClocks(time::milliseconds(10), 1000));
- BOOST_CHECK_EQUAL(m_count, 5);
+ BOOST_CHECK_EQUAL(count, 5);
}
BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
{
- m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
- bind(&SelfRescheduleFixture::reschedule3, this));
+ selfEventId = scheduler.scheduleEvent(time::seconds(0),
+ bind(&SelfRescheduleFixture::reschedule3, this));
- BOOST_REQUIRE_NO_THROW(m_io.run());
+ BOOST_REQUIRE_NO_THROW(advanceClocks(time::milliseconds(10), 1000));
- BOOST_CHECK_EQUAL(m_count, 6);
+ BOOST_CHECK_EQUAL(count, 6);
}
-struct CancelAllFixture
+struct CancelAllFixture : public UnitTestTimeFixture
{
CancelAllFixture()
- : m_scheduler(m_io)
- , m_count(0)
+ : scheduler(io)
+ , count(0)
{
}
void
- cancelAll()
- {
- m_scheduler.cancelAllEvents();
- }
-
- void
event()
{
- ++m_count;
+ ++count;
- m_scheduler.scheduleEvent(time::seconds(1),
- bind(&CancelAllFixture::event, this));
+ scheduler.scheduleEvent(time::seconds(1), [&] { event(); });
}
- void
- abort()
- {
- m_io.stop();
- }
-
- boost::asio::io_service m_io;
- Scheduler m_scheduler;
- uint32_t m_count;
+ Scheduler scheduler;
+ uint32_t count;
};
BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
{
- m_scheduler.scheduleEvent(time::milliseconds(500),
- bind(&CancelAllFixture::cancelAll, this));
+ scheduler.scheduleEvent(time::milliseconds(500), [&] { scheduler.cancelAllEvents(); });
- m_scheduler.scheduleEvent(time::seconds(1),
- bind(&CancelAllFixture::event, this));
+ scheduler.scheduleEvent(time::seconds(1), [&] { event(); });
- m_scheduler.scheduleEvent(time::seconds(3),
- bind(&CancelAllFixture::abort, this));
+ scheduler.scheduleEvent(time::seconds(3), [] {
+ BOOST_ERROR("This event should have been cancelled" );
+ });
- BOOST_REQUIRE_NO_THROW(m_io.run());
+ advanceClocks(time::milliseconds(100), 100);
- BOOST_CHECK_EQUAL(m_count, 0);
+ BOOST_CHECK_EQUAL(count, 0);
}
BOOST_AUTO_TEST_SUITE_END()
+} // namespace tests
} // namespace ndn