blob: e5c99e573a551bb45f2b283a5c95e434bc1d9f71 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
#include "ndn-cxx/util/scheduler.hpp"
#include "tests/boost-test.hpp"
#include "tests/unit/io-fixture.hpp"
#include <boost/lexical_cast.hpp>
namespace ndn::tests {
using namespace ndn::scheduler;
class SchedulerFixture : public IoFixture
{
protected:
Scheduler scheduler{m_io};
};
BOOST_AUTO_TEST_SUITE(Util)
BOOST_FIXTURE_TEST_SUITE(TestScheduler, SchedulerFixture)
BOOST_AUTO_TEST_SUITE(General)
BOOST_AUTO_TEST_CASE(Events)
{
size_t count1 = 0;
size_t count2 = 0;
scheduler.schedule(500_ms, [&] {
++count1;
BOOST_CHECK_EQUAL(count2, 1);
});
EventId i = scheduler.schedule(1_s, [] { BOOST_ERROR("This event should not have been fired"); });
i.cancel();
scheduler.schedule(250_ms, [&] {
BOOST_CHECK_EQUAL(count1, 0);
++count2;
});
i = scheduler.schedule(50_ms, [&] { BOOST_ERROR("This event should not have been fired"); });
i.cancel();
advanceClocks(25_ms, 1000_ms);
BOOST_CHECK_EQUAL(count1, 1);
BOOST_CHECK_EQUAL(count2, 1);
}
BOOST_AUTO_TEST_CASE(CallbackException)
{
class MyException : public std::exception
{
};
scheduler.schedule(10_ms, [] {
// use plain 'throw' to ensure that Scheduler does not depend on the
// internal machinery of NDN_THROW and that it can catch all exceptions
// regardless of how they are thrown by the application
throw MyException{};
});
bool isCallbackInvoked = false;
scheduler.schedule(20_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
BOOST_CHECK_THROW(this->advanceClocks(6_ms, 2), MyException);
this->advanceClocks(6_ms, 2);
BOOST_CHECK(isCallbackInvoked);
}
BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
{
EventId i;
i.cancel();
// avoid "test case [...] did not check any assertions" message from Boost.Test
BOOST_CHECK(true);
}
BOOST_AUTO_TEST_CASE(SelfCancel)
{
EventId selfEventId;
selfEventId = scheduler.schedule(100_ms, [&] { selfEventId.cancel(); });
BOOST_REQUIRE_NO_THROW(advanceClocks(100_ms, 10));
}
class SelfRescheduleFixture : public SchedulerFixture
{
public:
void
reschedule()
{
EventId eventId = scheduler.schedule(100_ms, [this] { reschedule(); });
selfEventId.cancel();
selfEventId = eventId;
if (count < 5)
count++;
else
selfEventId.cancel();
}
void
reschedule2()
{
selfEventId.cancel();
if (count < 5) {
selfEventId = scheduler.schedule(100_ms, [this] { reschedule2(); });
count++;
}
}
void
reschedule3()
{
selfEventId.cancel();
scheduler.schedule(100_ms, [&] { ++count; });
scheduler.schedule(100_ms, [&] { ++count; });
scheduler.schedule(100_ms, [&] { ++count; });
scheduler.schedule(100_ms, [&] { ++count; });
scheduler.schedule(100_ms, [&] { ++count; });
scheduler.schedule(100_ms, [&] { ++count; });
}
public:
EventId selfEventId;
size_t count = 0;
};
BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
{
selfEventId = scheduler.schedule(0_s, [this] { reschedule(); });
BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
BOOST_CHECK_EQUAL(count, 5);
}
BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
{
selfEventId = scheduler.schedule(0_s, [this] { reschedule2(); });
BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
BOOST_CHECK_EQUAL(count, 5);
}
BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
{
selfEventId = scheduler.schedule(0_s, [this] { reschedule3(); });
BOOST_REQUIRE_NO_THROW(advanceClocks(50_ms, 1000_ms));
BOOST_CHECK_EQUAL(count, 6);
}
class CancelAllFixture : public SchedulerFixture
{
public:
void
event()
{
++count;
scheduler.schedule(1_s, [&] { event(); });
}
public:
uint32_t count = 0;
};
BOOST_FIXTURE_TEST_CASE(CancelAll, CancelAllFixture)
{
scheduler.schedule(500_ms, [&] { scheduler.cancelAllEvents(); });
scheduler.schedule(1_s, [&] { event(); });
scheduler.schedule(3_s, [] { BOOST_ERROR("This event should have been cancelled" ); });
advanceClocks(100_ms, 100);
BOOST_CHECK_EQUAL(count, 0);
}
BOOST_AUTO_TEST_CASE(CancelAllWithScopedEventId) // Bug 3691
{
ScopedEventId eid = scheduler.schedule(10_ms, []{});
scheduler.cancelAllEvents();
eid.cancel(); // should not crash
// avoid "test case [...] did not check any assertions" message from Boost.Test
BOOST_CHECK(true);
}
BOOST_AUTO_TEST_SUITE_END() // General
BOOST_AUTO_TEST_SUITE(EventId)
using scheduler::EventId;
BOOST_AUTO_TEST_CASE(ConstructEmpty)
{
EventId eid;
BOOST_CHECK(!eid);
}
BOOST_AUTO_TEST_CASE(Equality)
{
EventId eid, eid2;
BOOST_CHECK(eid == eid2);
eid = scheduler.schedule(10_ms, []{});
BOOST_CHECK(eid != eid2);
eid2 = scheduler.schedule(10_ms, []{});
BOOST_CHECK(eid != eid2);
eid.cancel();
BOOST_CHECK(eid != eid2);
BOOST_CHECK(eid == EventId{});
eid2.cancel();
BOOST_CHECK(eid == eid2);
eid = eid2 = scheduler.schedule(20_ms, []{});
BOOST_CHECK(eid == eid2);
BOOST_CHECK(eid != EventId{});
eid.cancel();
BOOST_CHECK(eid == eid2);
BOOST_CHECK(eid == EventId{});
}
BOOST_AUTO_TEST_CASE(OperatorBool)
{
EventId eid;
BOOST_CHECK_EQUAL(static_cast<bool>(eid), false);
BOOST_CHECK_EQUAL(!eid, true);
eid = scheduler.schedule(10_ms, []{});
BOOST_CHECK_EQUAL(static_cast<bool>(eid), true);
BOOST_CHECK_EQUAL(!eid, false);
EventId eid2 = eid;
eid2.cancel();
BOOST_CHECK(!eid);
BOOST_CHECK(!eid2);
}
BOOST_AUTO_TEST_CASE(DuringCallback)
{
EventId eid;
EventId eid2 = scheduler.schedule(20_ms, []{});
bool isCallbackInvoked = false;
eid = scheduler.schedule(10_ms, [&eid, &eid2, &isCallbackInvoked] {
isCallbackInvoked = true;
// eid is "expired" during callback execution
BOOST_CHECK(!eid);
BOOST_CHECK_NE(eid, eid2);
eid2.cancel();
BOOST_CHECK_EQUAL(eid, eid2);
});
this->advanceClocks(6_ms, 2);
BOOST_CHECK(isCallbackInvoked);
}
BOOST_AUTO_TEST_CASE(Reset)
{
bool isCallbackInvoked = false;
EventId eid = scheduler.schedule(10_ms, [&isCallbackInvoked] { isCallbackInvoked = true; });
eid.reset();
BOOST_CHECK(!eid);
this->advanceClocks(6_ms, 2);
BOOST_CHECK(isCallbackInvoked);
}
BOOST_AUTO_TEST_CASE(ToString)
{
std::string nullString = boost::lexical_cast<std::string>(shared_ptr<int>());
EventId eid;
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString);
eid = scheduler.schedule(10_ms, []{});
BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString);
}
BOOST_AUTO_TEST_SUITE_END() // EventId
BOOST_AUTO_TEST_SUITE(ScopedEventId)
using scheduler::ScopedEventId;
BOOST_AUTO_TEST_CASE(Destruct)
{
int hit = 0;
{
ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
} // se goes out of scope
this->advanceClocks(1_ms, 15);
BOOST_CHECK_EQUAL(hit, 0);
}
BOOST_AUTO_TEST_CASE(Assign)
{
int hit1 = 0, hit2 = 0;
ScopedEventId se1 = scheduler.schedule(10_ms, [&] { ++hit1; });
se1 = scheduler.schedule(10_ms, [&] { ++hit2; });
this->advanceClocks(1_ms, 15);
BOOST_CHECK_EQUAL(hit1, 0);
BOOST_CHECK_EQUAL(hit2, 1);
}
BOOST_AUTO_TEST_CASE(Release)
{
int hit = 0;
{
ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
se.release();
} // se goes out of scope
this->advanceClocks(1_ms, 15);
BOOST_CHECK_EQUAL(hit, 1);
}
BOOST_AUTO_TEST_CASE(Move)
{
int hit = 0;
unique_ptr<ScopedEventId> se2;
{
ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
se2 = make_unique<ScopedEventId>(std::move(se)); // move constructor
} // se goes out of scope
this->advanceClocks(1_ms, 15);
BOOST_CHECK_EQUAL(hit, 1);
ScopedEventId se3;
{
ScopedEventId se = scheduler.schedule(10_ms, [&] { ++hit; });
se3 = std::move(se); // move assignment
} // se goes out of scope
this->advanceClocks(1_ms, 15);
BOOST_CHECK_EQUAL(hit, 2);
}
BOOST_AUTO_TEST_CASE(OperatorBool)
{
ScopedEventId se;
BOOST_CHECK_EQUAL(static_cast<bool>(se), false);
BOOST_CHECK_EQUAL(!se, true);
se = scheduler.schedule(10_ms, []{});
BOOST_CHECK_EQUAL(static_cast<bool>(se), true);
BOOST_CHECK_EQUAL(!se, false);
se.cancel();
BOOST_CHECK(!se);
se = scheduler.schedule(10_ms, []{});
BOOST_CHECK(se);
se.release();
BOOST_CHECK(!se);
}
BOOST_AUTO_TEST_SUITE_END() // ScopedEventId
BOOST_AUTO_TEST_SUITE_END() // TestScheduler
BOOST_AUTO_TEST_SUITE_END() // Util
} // namespace ndn::tests