checkout working scheduler
diff --git a/include/event-scheduler.h b/include/event-scheduler.h
index a99dbae..2055598 100644
--- a/include/event-scheduler.h
+++ b/include/event-scheduler.h
@@ -122,7 +122,7 @@
// generator is needed only when this is a periodic task
// two simple generators implementation (SimpleIntervalGenerator and RandomIntervalGenerator) are provided;
// if user needs more complex pattern in the intervals between calls, extend class IntervalGenerator
- PeriodicTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator, int repeat = -1);
+ PeriodicTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator);
virtual ~PeriodicTask(){}
// invoke callback, reset self and ask scheduler to schedule self with the next delay interval
@@ -133,18 +133,8 @@
virtual void
reset() _OVERRIDE;
- void
- setRepeatTimes(int repeat) { m_repeat = repeat; m_indefinite = (m_repeat > 0); }
-
-private:
- // this is to deregister itself from scheduler automatically if not indefinite
- void
- deregisterSelf();
-
private:
IntervalGeneratorPtr m_generator;
- int m_repeat;
- bool m_indefinite;
};
struct SchedulerException : virtual boost::exception, virtual exception { };
diff --git a/src/event-scheduler.cpp b/src/event-scheduler.cpp
index 1c892d6..be67aad 100644
--- a/src/event-scheduler.cpp
+++ b/src/event-scheduler.cpp
@@ -79,11 +79,9 @@
m_invoked = false;
}
-PeriodicTask::PeriodicTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator, int repeat)
+PeriodicTask::PeriodicTask(const Callback &callback, const Tag &tag, const SchedulerPtr &scheduler, const IntervalGeneratorPtr &generator)
: Task(callback, tag, scheduler)
, m_generator(generator)
- , m_repeat(repeat)
- , m_indefinite(m_repeat > 0)
{
}
@@ -94,22 +92,7 @@
{
m_callback();
m_invoked = true;
- if (m_indefinite)
- {
- m_scheduler->rescheduleTask(m_tag);
- }
- else
- {
- m_repeat--;
- if (m_repeat > 0)
- {
- m_scheduler->rescheduleTask(m_tag);
- }
- else
- {
- deregisterSelf();
- }
- }
+ m_scheduler->rescheduleTask(m_tag);
}
}
@@ -121,12 +104,6 @@
setTv(interval);
}
-void
-PeriodicTask::deregisterSelf()
-{
- m_scheduler->deleteTask(m_tag);
-}
-
RandomIntervalGenerator::RandomIntervalGenerator(double interval, double percent, Direction direction)
: m_interval(interval)
, m_rng(time(NULL))
diff --git a/test/test-event-scheduler.cc b/test/test-event-scheduler.cc
index b827296..4642d95 100644
--- a/test/test-event-scheduler.cc
+++ b/test/test-event-scheduler.cc
@@ -37,17 +37,14 @@
{
SchedulerPtr scheduler(new Scheduler());
IntervalGeneratorPtr generator(new SimpleIntervalGenerator(0.2));
- IntervalGeneratorPtr randomGen(new RandomIntervalGenerator(0.05, 0.5));
string tag1 = "hello";
string tag2 = "world";
string tag3 = "period";
- string tag4 = "haha";
TaskPtr task1(new OneTimeTask(boost::bind(func, tag1), tag1, scheduler, 0.5));
TaskPtr task2(new OneTimeTask(boost::bind(func, tag2), tag2, scheduler, 0.5));
TaskPtr task3(new PeriodicTask(boost::bind(func, tag3), tag3, scheduler, generator));
- TaskPtr task4(new PeriodicTask(boost::bind(func, tag4), tag4, scheduler, randomGen, 5));
scheduler->start();
scheduler->addTask(task1);
@@ -72,14 +69,11 @@
usleep(100000);
scheduler->deleteTask(bind(matcher, _1));
BOOST_CHECK_EQUAL(scheduler->size(), 1);
- usleep(500000);
-
- scheduler->addTask(task4);
- usleep(500000);
+ usleep(1000000);
scheduler->shutdown();
- int hello = 0, world = 0, period = 0, haha = 0;
+ int hello = 0, world = 0, period = 0;
map<string, int>::iterator it;
it = table.find(tag1);
@@ -98,20 +92,12 @@
period = it->second;
}
- it = table.find(tag4);
- if (it != table.end())
- {
- haha = it->second;
- }
-
// added four times, canceled once before invoking callback
BOOST_CHECK_EQUAL(hello, 3);
// added two times, canceled once by matcher before invoking callback
BOOST_CHECK_EQUAL(world, 1);
// invoked every 0.2 seconds before deleted by matcher
BOOST_CHECK_EQUAL(period, static_cast<int>((0.6 + 0.6 + 0.4 + 0.2 + 0.1) / 0.2));
- // should be invoked 5 times exactly
- BOOST_CHECK_EQUAL(haha, 5);
}