Ejemplo n.º 1
0
 def setup(self):
     global time
     time = minitwisted.time = MockTime()
     # Order in which callbacks have been fired
     self.callback_order = []
     self.task_m = TaskManager()
Ejemplo n.º 2
0
class TestTaskManager:
    def callback_f(self, callback_id):
        self.callback_order.append(callback_id)

    def setup(self):
        global time
        time = minitwisted.time = MockTime()
        # Order in which callbacks have been fired
        self.callback_order = []
        self.task_m = TaskManager()

    def test_simple(self):
        for i in xrange(5):
            self.task_m.add(Task(.01, self.callback_f, i))
        while True:
            task = self.task_m.consume_task()
            if task is None:
                break
            task.fire_callback()
        logger.debug('%s' % self.callback_order)
        assert self.callback_order == []
        time.sleep(.01)
        while True:
            task = self.task_m.consume_task()
            if task is None:
                break
            task.fire_callbacks()
        assert self.callback_order == range(5)

    def test_cancel(self):
        for i in xrange(5):
            self.task_m.add(Task(.1, self.callback_f, i))
        c_task = Task(.1, self.callback_f, 5)
        self.task_m.add(c_task)
        for i in xrange(6, 10):
            self.task_m.add(Task(.1, self.callback_f, i))
        while True:
            task = self.task_m.consume_task()
            if task is None:
                break
            task.fire_callback()
        logger.debug('%s' % self.callback_order)
        assert self.callback_order == []
        ok_(not c_task.cancelled)
        c_task.cancel()
        ok_(c_task.cancelled)

        time.sleep(.1)
        while True:
            task = self.task_m.consume_task()
            if task is None:
                break
            task.fire_callbacks()
        logger.debug('%s' % self.callback_order)
        assert self.callback_order == [0, 1, 2, 3, 4, 6, 7, 8, 9]
        # task 5 was cancelled

    def test_different_delay(self):
        #         NOTICE: this test might fail if your configuration
        #         (interpreter/processor) is too slow

        task_delays = (1, 1, 1, .5, 1, 1, 2, 1, 1, 1, 1, 1.5, 1, 1, 1, 1, .3)

        expected_list = (
            [],
            ['a', 16, 3, 'b'],  #9 is cancelled
            ['a', 0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 15, 'c', 'b'],
            ['a', 11, 'c', 'b'],
            ['a', 6, 'c', 'b'],
        )
        tasks = [Task(delay, self.callback_f, i) \
                 for i, delay in enumerate(task_delays)]
        for task in tasks:
            self.task_m.add(task)

        for i, expected in enumerate(expected_list):
            while True:
                task = self.task_m.consume_task()
                if task is None:
                    break
                task.fire_callbacks()
            logger.debug('#: %d, result: %s, expected: %s' %
                         (i, self.callback_order, expected))
            assert self.callback_order == expected
            self.callback_order = []
            self.task_m.add(Task(0, self.callback_f, 'a'))
            self.task_m.add(Task(.5, self.callback_f, 'b'))
            self.task_m.add(Task(1, self.callback_f, 'c'))
            time.sleep(.5)
            tasks[9].cancel()  # too late (already fired)
            tasks[14].cancel()  # should be cancelled

    def _callback1(self, arg1, arg2):
        if arg1 == 1 and arg2 == 2:
            self.callback_order.append(1)

    def _callback2(self, arg1, arg2):
        if arg1 == 1 and arg2 == 2:
            self.callback_order.append(2)

    def test_callback_list(self):
        self.task_m.add(
            Task(tc.TASK_INTERVAL / 2, [self._callback1, self._callback2], 1,
                 2))
        ok_(self.task_m.consume_task() is None)
        eq_(self.callback_order, [])
        time.sleep(tc.TASK_INTERVAL)
        self.task_m.consume_task().fire_callbacks()
        eq_(self.callback_order, [1, 2])

    def teardown(self):
        global time
        time = minitwisted.time = time.actual_time
Ejemplo n.º 3
0
 def setup(self):
     global time
     time = minitwisted.time = MockTime()
     # Order in which callbacks have been fired
     self.callback_order = []
     self.task_m = TaskManager()
Ejemplo n.º 4
0
class TestTaskManager:
    
    def callback_f(self, callback_id):
        self.callback_order.append(callback_id)
        
    def setup(self):
        global time
        time = minitwisted.time = MockTime()
        # Order in which callbacks have been fired
        self.callback_order = []
        self.task_m = TaskManager()

    def test_simple(self):
        for i in xrange(5):
            self.task_m.add(Task(.01, self.callback_f, i))
        while True:
            task = self.task_m.consume_task()
            if task is None:
                break
            task.fire_callback()
        logger.debug('%s' % self.callback_order)
        assert self.callback_order == []
        time.sleep(.01)
        while True:
            task = self.task_m.consume_task()
            if task is None:
                break
            task.fire_callbacks() 
        assert self.callback_order == range(5)

    def test_cancel(self):
        for i in xrange(5):
            self.task_m.add(Task(.1, self.callback_f, i))
        c_task = Task(.1, self.callback_f, 5)
        self.task_m.add(c_task)
        for i in xrange(6,10):
            self.task_m.add(Task(.1, self.callback_f, i))
        while True:
            task = self.task_m.consume_task()
            if task is None:
                break
            task.fire_callback()
        logger.debug('%s' % self.callback_order)
        assert self.callback_order == []
        ok_(not c_task.cancelled)
        c_task.cancel()
        ok_(c_task.cancelled)
        
        time.sleep(.1)
        while True:
            task = self.task_m.consume_task()
            if task is None:
                break
            task.fire_callbacks()
        logger.debug('%s' % self.callback_order)
        assert self.callback_order == [0,1,2,3,4,  6,7,8,9]
        # task 5 was cancelled        

    def test_different_delay(self):
#         NOTICE: this test might fail if your configuration
#         (interpreter/processor) is too slow
        
        task_delays = (1, 1, 1, .5, 1, 1, 2, 1, 1, 1,
                       1, 1.5, 1, 1, 1, 1, .3)
                       
        expected_list = ([],
                         ['a', 16, 3, 'b'], #9 is cancelled
                         ['a', 0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 15, 'c', 'b'],
                         ['a', 11, 'c', 'b'],
                         ['a', 6, 'c', 'b'],
            )
        tasks = [Task(delay, self.callback_f, i) \
                 for i, delay in enumerate(task_delays)]
        for task in tasks:
            self.task_m.add(task)

        for i, expected in enumerate(expected_list):
            while True:
                task = self.task_m.consume_task()
                if task is None:
                    break
                task.fire_callbacks()
            logger.debug('#: %d, result: %s, expected: %s' % (i,
                                              self.callback_order, expected))
            assert self.callback_order == expected
            self.callback_order = []
            self.task_m.add(Task(0, self.callback_f, 'a'))
            self.task_m.add(Task(.5, self.callback_f, 'b'))
            self.task_m.add(Task(1, self.callback_f, 'c'))
            time.sleep(.5)
            tasks[9].cancel() # too late (already fired) 
            tasks[14].cancel() # should be cancelled

    def _callback1(self, arg1, arg2):
        if arg1 == 1 and arg2 == 2:
            self.callback_order.append(1)
    def _callback2(self, arg1, arg2):
        if arg1 == 1 and arg2 == 2:
            self.callback_order.append(2)
    
    def test_callback_list(self):
        self.task_m.add(Task(tc.TASK_INTERVAL/2,
                              [self._callback1, self._callback2],
                              1, 2))
        ok_(self.task_m.consume_task() is None)
        eq_(self.callback_order, [])
        time.sleep(tc.TASK_INTERVAL)
        self.task_m.consume_task().fire_callbacks()
        eq_(self.callback_order, [1,2])

    def teardown(self):
        global time
        time = minitwisted.time = time.actual_time
Ejemplo n.º 5
0
 def setup(self):
     # Order in which callbacks have been fired
     self.callback_order = []
     self.task_m = TaskManager()
Ejemplo n.º 6
0
 def setup(self):
     # Order in which callbacks have been fired
     self.callback_order = []
     self.task_m = TaskManager()