def test_immediate(self): capture = [] @periodic.periodic(120, run_immediately=True) def a(): capture.append('a') w = periodic.PeriodicWorker([a]) t = tu.daemon_thread(target=w.start) t.start() time.sleep(0.1) w.stop() t.join() a_calls = [c for c in capture if c == 'a'] self.assertGreater(0, len(a_calls))
def test_periodic_single(self): barrier = latch.Latch(5) capture = [] @periodic.periodic(0.01) def callee(): barrier.countdown() if barrier.needed == 0: w.stop() capture.append(1) w = periodic.PeriodicWorker([callee]) t = tu.daemon_thread(target=w.start) t.start() t.join() self.assertEqual(0, barrier.needed) self.assertEqual(5, sum(capture))
def test_periodic_single(self): barrier = latch.Latch(5) capture = [] tombstone = tu.Event() @periodic.periodic(0.01) def callee(): barrier.countdown() if barrier.needed == 0: tombstone.set() capture.append(1) w = periodic.PeriodicWorker([callee], tombstone=tombstone) t = tu.daemon_thread(target=w.start) t.start() t.join() self.assertEqual(0, barrier.needed) self.assertEqual(5, sum(capture)) self.assertTrue(tombstone.is_set())
def test_period_double_no_immediate(self): capture = [] @periodic.periodic(0.01, run_immediately=False) def a(): capture.append('a') @periodic.periodic(0.02, run_immediately=False) def b(): capture.append('b') w = periodic.PeriodicWorker([a, b]) t = tu.daemon_thread(target=w.start) t.start() time.sleep(0.1) w.stop() t.join() b_calls = [c for c in capture if c == 'b'] self.assertGreater(0, len(b_calls)) a_calls = [c for c in capture if c == 'a'] self.assertGreater(0, len(a_calls))
def test_start_nothing_error(self): w = periodic.PeriodicWorker([]) self.assertRaises(RuntimeError, w.start)