コード例 #1
0
ファイル: test_checker.py プロジェクト: dlecocq/nsq-py
    def test_survives_standard_error(self):
        '''The thread survives exceptions'''
        def callback():
            '''Raise an exception'''
            raise StandardError('foo')

        with mock.patch('nsq.checker.logger') as mock_logger:
            thread = PeriodicThread(0.01, callback)
            thread.start()
            thread.join(0.1)
            self.assertTrue(thread.is_alive())
            thread.stop()
            thread.join()
            mock_logger.exception.assert_called_with('Callback failed')
            self.assertGreater(mock_logger.exception.call_count, 1)
コード例 #2
0
ファイル: test_checker.py プロジェクト: lambdaq/nsq-py
    def test_stop(self):
        '''Stop is effective for stopping a stoppable thread'''
        def callback(event):
            '''Trigger an event'''
            event.set()

        event = threading.Event()
        thread = PeriodicThread(0.01, callback, event)
        thread.start()
        event.wait()
        thread.stop()
        thread.join()
        self.assertFalse(thread.is_alive())
コード例 #3
0
ファイル: test_checker.py プロジェクト: dlecocq/nsq-py
    def test_stop(self):
        '''Stop is effective for stopping a stoppable thread'''
        def callback(event):
            '''Trigger an event'''
            event.set()

        event = threading.Event()
        thread = PeriodicThread(0.01, callback, event)
        thread.start()
        event.wait()
        thread.stop()
        thread.join()
        self.assertFalse(thread.is_alive())
コード例 #4
0
ファイル: test_checker.py プロジェクト: lambdaq/nsq-py
    def test_repeats(self):
        '''Repeats the same callback several times'''
        def callback(event, counter):
            '''Trigger an event after accumulating enough'''
            counter['count'] += 1
            if counter['count'] > 10:
                event.set()

        event = threading.Event()
        counter = {'count': 0}
        thread = PeriodicThread(0.01, callback, event, counter)
        thread.start()
        event.wait()
        thread.stop()
        thread.join()
        self.assertGreaterEqual(counter['count'], 10)
コード例 #5
0
ファイル: test_checker.py プロジェクト: dlecocq/nsq-py
    def test_repeats(self):
        '''Repeats the same callback several times'''
        def callback(event, counter):
            '''Trigger an event after accumulating enough'''
            counter['count'] += 1
            if counter['count'] > 10:
                event.set()

        event = threading.Event()
        counter = {'count': 0}
        thread = PeriodicThread(0.01, callback, event, counter)
        thread.start()
        event.wait()
        thread.stop()
        thread.join()
        self.assertGreaterEqual(counter['count'], 10)
コード例 #6
0
ファイル: test_checker.py プロジェクト: lambdaq/nsq-py
    def test_survives_standard_error(self):
        '''The thread survives exceptions'''
        def callback():
            '''Raise an exception'''
            raise Exception('foo')

        with mock.patch('nsq.checker.logger') as mock_logger:
            thread = PeriodicThread(0.01, callback)
            thread.start()
            thread.join(0.1)
            self.assertTrue(thread.is_alive())
            thread.stop()
            thread.join()
            mock_logger.exception.assert_called_with('Callback failed')
            self.assertGreater(mock_logger.exception.call_count, 1)