def test_calculate_with_timeout_tries_to_interrupt_timed_out_thread( self, mock_sleep, mock_ithread_class): def check_interrupt_called_first(_): self.assertTrue(mock_ithread.interrupt.called) mock_sleep.side_effect = check_interrupt_called_first mock_ithread_class.return_value = mock_ithread = Mock() def set_is_alive(): mock_ithread.alive = not mock_ithread.alive return not mock_ithread.alive mock_ithread.isAlive.side_effect = set_is_alive calculate_with_timeout(sentinel.worksheet, sentinel.usercode, sentinel.timeout_seconds, sentinel.private_key) self.assertEquals(mock_ithread.method_calls, [ ('start', (), {}), ('join', (sentinel.timeout_seconds, ), {}), ('isAlive', (), {}), ('interrupt', (), {}), ('isAlive', (), {}), ]) self.assertEquals(mock_sleep.call_args, ((0.1, ), {}))
def test_calculate_with_timeout_tries_to_interrupt_timed_out_thread( self, mock_sleep, mock_ithread_class ): def check_interrupt_called_first(_): self.assertTrue(mock_ithread.interrupt.called) mock_sleep.side_effect = check_interrupt_called_first mock_ithread_class.return_value = mock_ithread = Mock() def set_is_alive(): mock_ithread.alive = not mock_ithread.alive return not mock_ithread.alive mock_ithread.isAlive.side_effect = set_is_alive calculate_with_timeout( sentinel.worksheet, sentinel.usercode, sentinel.timeout_seconds, sentinel.private_key ) self.assertEquals(mock_ithread.method_calls, [ ('start', (), {}), ('join', (sentinel.timeout_seconds,), {}), ('isAlive', (), {}), ('interrupt', (), {}), ('isAlive', (), {}), ] ) self.assertEquals(mock_sleep.call_args, ((0.1,), {}))
def test_calculate_with_timeout_calls_calculate_function_with_contents_and_usercode( self, mock_calculate): long_timeout_seconds = 100 calculate_with_timeout(sentinel.worksheet, sentinel.usercode, long_timeout_seconds, sentinel.private_key) self.assertEquals(mock_calculate.call_args, ((sentinel.worksheet, sentinel.usercode, sentinel.private_key), {}))
def calculate(self): private_key = self.create_private_key() worksheet = self.unjsonify_worksheet() transaction.commit() try: calculate_with_timeout(worksheet, self.usercode, self.timeout_seconds, private_key) finally: self._delete_private_key() self.jsonify_worksheet(worksheet)
def test_calculate_with_timeout_uses_interruptable_thread_with_correct_timeout( self, mock_ithread_class): mock_ithread_class.return_value = mock_ithread = Mock() mock_ithread.isAlive.return_value = False calculate_with_timeout(sentinel.worksheet, sentinel.usercode, sentinel.timeout_seconds, sentinel.private_key) self.assertEquals(mock_ithread.method_calls, [ ('start', (), {}), ('join', (sentinel.timeout_seconds, ), {}), ('isAlive', (), {}), ])
def test_calculate_with_timeout_calls_calculate_function_with_contents_and_usercode( self, mock_calculate ): long_timeout_seconds = 100 calculate_with_timeout( sentinel.worksheet, sentinel.usercode, long_timeout_seconds, sentinel.private_key ) self.assertEquals( mock_calculate.call_args, ((sentinel.worksheet, sentinel.usercode, sentinel.private_key), {}) )
def test_calculate_with_timeout_uses_interruptable_thread_with_correct_timeout( self, mock_ithread_class ): mock_ithread_class.return_value = mock_ithread = Mock() mock_ithread.isAlive.return_value = False calculate_with_timeout( sentinel.worksheet, sentinel.usercode, sentinel.timeout_seconds, sentinel.private_key ) self.assertEquals(mock_ithread.method_calls, [ ('start', (), {}), ('join', (sentinel.timeout_seconds,), {}), ('isAlive', (), {}), ] )