Skip to content

Commit 9994d01

Browse files
committed
Add Unit Test for SingleThreadedSchedulerClient
Ensures ordering of callbacks within a SingleThreadedSchedulerClient with respect to each other
1 parent 34dd1a6 commit 9994d01

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/test/scheduler_tests.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE(manythreads)
6565
size_t nTasks = microTasks.getQueueInfo(first, last);
6666
BOOST_CHECK(nTasks == 0);
6767

68-
for (int i = 0; i < 100; i++) {
68+
for (int i = 0; i < 100; ++i) {
6969
boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng));
7070
boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng));
7171
int whichCounter = zeroToNine(rng);
@@ -112,4 +112,46 @@ BOOST_AUTO_TEST_CASE(manythreads)
112112
BOOST_CHECK_EQUAL(counterSum, 200);
113113
}
114114

115+
BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
116+
{
117+
CScheduler scheduler;
118+
119+
// each queue should be well ordered with respect to itself but not other queues
120+
SingleThreadedSchedulerClient queue1(&scheduler);
121+
SingleThreadedSchedulerClient queue2(&scheduler);
122+
123+
// create more threads than queues
124+
// if the queues only permit execution of one task at once then
125+
// the extra threads should effectively be doing nothing
126+
// if they don't we'll get out of order behaviour
127+
boost::thread_group threads;
128+
for (int i = 0; i < 5; ++i) {
129+
threads.create_thread(boost::bind(&CScheduler::serviceQueue, &scheduler));
130+
}
131+
132+
// these are not atomic, if SinglethreadedSchedulerClient prevents
133+
// parallel execution at the queue level no synchronization should be required here
134+
int counter1 = 0;
135+
int counter2 = 0;
136+
137+
// just simply count up on each queue - if execution is properly ordered then
138+
// the callbacks should run in exactly the order in which they were enqueued
139+
for (int i = 0; i < 100; ++i) {
140+
queue1.AddToProcessQueue([i, &counter1]() {
141+
BOOST_CHECK_EQUAL(i, counter1++);
142+
});
143+
144+
queue2.AddToProcessQueue([i, &counter2]() {
145+
BOOST_CHECK_EQUAL(i, counter2++);
146+
});
147+
}
148+
149+
// finish up
150+
scheduler.stop(true);
151+
threads.join_all();
152+
153+
BOOST_CHECK_EQUAL(counter1, 100);
154+
BOOST_CHECK_EQUAL(counter2, 100);
155+
}
156+
115157
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)