예제 #1
0
    def test_timeout_proxy_repr(self):
        with timeout(1.0) as t:
            self.assertTrue(repr(t).startswith("TimeoutProxy(timeout(0.9"))
            self.assertTrue(
                repr(t).endswith(", exception={}))".format(
                    repr(CancelledError))))

        with timeout(1.0, exception=None) as t:
            self.assertTrue(repr(t).startswith("TimeoutProxy(timeout(0.9"))
            self.assertTrue(repr(t).endswith(", exception=None))"))
예제 #2
0
 def test_timeout_with_ignored_inner_exception(self):
     mock = Mock()
     with self.assertRaises(CancelledError):
         with timeout(0.05):
             with timeout(0.01):
                 with ignore_exception(CancelledError, 0.05):
                     mock(1)
                     sleep(0.02)
                     mock(2)
                 mock(3)
             mock(4)
     mock.assert_called_once_with(1)
 async def test_timeout_with_ignored_inner_exception(self):
     mock = Mock()
     with self.assertRaises(CancelledError):
         async with timeout(0.05):
             async with timeout(0.01):
                 async with ignore_exception(CancelledError, 0.05):
                     mock(1)
                     await sleep(0.02)
                     mock(2)
                 mock(3)
             mock(4)
     self.assertEqual(mock.mock_calls, [call(1)])
    async def test_nested_timeout_raises_with_same_timeout(self):
        mock = Mock()

        with self.assertRaises(CancelledError):
            async with timeout(0.01):
                mock(1)
                async with timeout(0.01):
                    mock(2)
                    await sleep(0.03)
                    mock(3)
                mock(4)

        self.assertEqual(mock.mock_calls, [call(1), call(2)])
예제 #5
0
    def test_nested_timeout_raises_with_same_timeout(self):
        mock = Mock()

        with self.assertRaises(CancelledError):
            with timeout(0.01):
                mock(1)
                with timeout(0.01):
                    mock(2)
                    sleep(0.03)
                    mock(3)
                mock(4)

        mock.assert_has_calls([call(1), call(2)])
        self.assertEqual(mock.call_count, 2)
    async def test_multiple_nested_silent_timeout_outer_propagation(self):
        mock = Mock()

        async with timeout(0.01, exception=None):
            mock(1)
            async with timeout(0.07):
                mock(2)
                async with timeout(0.05):
                    mock(3)
                    await sleep(0.03)
                    mock(4)
                mock(5)
            mock(6)

        self.assertEqual(mock.mock_calls, [call(1), call(2), call(3)])
    async def test_nested_timeout_outer_raises_inner_silent(self):
        mock = Mock()

        with self.assertRaises(CancelledError):
            async with timeout(0.05):
                mock(1)
                async with timeout(0.01, exception=None):
                    mock(2)
                    await sleep(0.03)
                    mock(3)
                mock(4)
                await sleep(0.05)
                mock(5)

        self.assertEqual(mock.mock_calls, [call(1), call(2), call(4)])
    async def test_nested_timeout_outer_raises(self):
        mock = Mock()

        with self.assertRaises(CancelledError):
            async with timeout(0.03):
                mock(1)
                async with timeout(0.07):
                    mock(2)
                    await sleep(0.01)
                    mock(3)
                mock(4)
                await sleep(0.04)
                mock(5)

        self.assertEqual(mock.mock_calls, [call(1), call(2), call(3), call(4)])
예제 #9
0
    def test_timeout_custom_exception_with_manual_raise(self):
        class CustomException(CancelledError):
            pass

        with self.assertRaises(CustomException):
            with timeout(0.01, exception=CustomException) as t:
                raise t.exception()
예제 #10
0
    def test_timeout_custom_exception_with_unexpected_delay(self):
        class CustomException(CancelledError):
            pass

        with self.assertRaises(CustomException):
            with timeout(0.01, exception=CustomException):
                sleep(0.02)
 async def test_silent_timeout_with_unexpected_delay(self):
     mock = Mock()
     async with timeout(0.01, exception=None):
         mock(1)
         await sleep(0.02)
         mock(2)
     self.assertEqual(mock.mock_calls, [call(1)])
 async def test_timeout_with_expected_delay(self):
     mock = Mock()
     async with timeout(0.02):
         mock(1)
         await sleep(0.01)
         mock(2)
     self.assertEqual(mock.mock_calls, [call(1), call(2)])
    async def test_multiple_nested_timeout_outer_propagation(self):
        mock = Mock()

        with self.assertRaises(CancelledError):
            async with timeout(0.01):
                mock(1)
                async with timeout(0.07):
                    mock(2)
                    async with timeout(0.05):
                        mock(3)
                        await sleep(0.03)
                        mock(4)
                    mock(5)
                mock(6)

        self.assertEqual(mock.mock_calls, [call(1), call(2), call(3)])
예제 #14
0
    def test_wraps(self):
        def fn():
            pass

        wrapped = timeout(0.01)(fn)
        self.assertEqual(wrapped.__name__, fn.__name__)
        self.assertEqual(wrapped.__wrapped__, fn)
예제 #15
0
    def test_nested_timeout_outer_raises_inner_silent(self):
        mock = Mock()

        with self.assertRaises(CancelledError):
            with timeout(0.05):
                mock(1)
                with timeout(0.01, exception=None):
                    mock(2)
                    sleep(0.03)
                    mock(3)
                mock(4)
                sleep(0.05)
                mock(5)

        mock.assert_has_calls([call(1), call(2), call(4)])
        self.assertEqual(mock.call_count, 3)
예제 #16
0
    def test_nested_timeout_outer_raises(self):
        mock = Mock()

        with self.assertRaises(CancelledError):
            with timeout(0.03):
                mock(1)
                with timeout(0.07):
                    mock(2)
                    sleep(0.01)
                    mock(3)
                mock(4)
                sleep(0.04)
                mock(5)

        mock.assert_has_calls([call(1), call(2), call(3), call(4)])
        self.assertEqual(mock.call_count, 4)
예제 #17
0
    def test_timeout_repr_with_custom_exception(self):
        class CustomException(CancelledError):
            pass

        self.assertEqual(
            repr(timeout(1.0, exception=CustomException)),
            "timeout(1.0, exception={})".format(repr(CustomException)))
예제 #18
0
    def test_multiple_nested_silent_timeout_outer_propagation(self):
        mock = Mock()

        with timeout(0.01, exception=None):
            mock(1)
            with timeout(0.07):
                mock(2)
                with timeout(0.05):
                    mock(3)
                    sleep(0.03)
                    mock(4)
                mock(5)
            mock(6)

        mock.assert_has_calls([call(1), call(2), call(3)])
        self.assertEqual(mock.call_count, 3)
예제 #19
0
 def test_silent_timeout_with_unexpected_delay(self):
     mock = Mock()
     with timeout(0.01, exception=None):
         mock(1)
         sleep(0.02)
         mock(2)
     mock.assert_called_once_with(1)
 async def test_timeout_with_unexpected_delay(self):
     mock = Mock()
     with self.assertRaises(CancelledError):
         async with timeout(0.01):
             mock(1)
             await sleep(0.03)
             mock(2)
     self.assertEqual(mock.mock_calls, [call(1)])
예제 #21
0
 def test_timeout_with_unexpected_delay(self):
     mock = Mock()
     with self.assertRaises(CancelledError):
         with timeout(0.01):
             mock(1)
             sleep(0.02)
             mock(2)
     mock.assert_called_once_with(1)
예제 #22
0
    def test_multiple_nested_timeout_outer_propagation(self):
        mock = Mock()

        with self.assertRaises(CancelledError):
            with timeout(0.01):
                mock(1)
                with timeout(0.07):
                    mock(2)
                    with timeout(0.05):
                        mock(3)
                        sleep(0.03)
                        mock(4)
                    mock(5)
                mock(6)

        mock.assert_has_calls([call(1), call(2), call(3)])
        self.assertEqual(mock.call_count, 3)
예제 #23
0
 def test_timeout_with_expected_delay(self):
     mock = Mock()
     with timeout(0.02):
         mock(1)
         sleep(0.01)
         mock(2)
     mock.assert_has_calls([call(1), call(2)])
     self.assertEqual(mock.call_count, 2)
예제 #24
0
    def test_restores_prev_signal_handler_with_expected_delay(self):
        def handler():
            pass

        signal.signal(signal.SIGALRM, handler)

        with timeout(0.02):
            sleep(0.01)

        handler_after = signal.getsignal(signal.SIGALRM)
        self.assertEqual(handler_after, handler)
예제 #25
0
 def test_forwards_args_and_result(self):
     mock = Mock(return_value=sentinel.res)
     res = timeout(0.01)(mock)(sentinel.a,
                               sentinel.b,
                               key1=sentinel.val,
                               key2=None)
     mock.assert_called_once_with(sentinel.a,
                                  sentinel.b,
                                  key1=sentinel.val,
                                  key2=None)
     self.assertEqual(res, sentinel.res)
예제 #26
0
    def test_restores_prev_signal_handler_with_unexpected_delay(self):
        def handler():
            pass

        signal.signal(signal.SIGALRM, handler)

        with self.assertRaises(CancelledError):
            with timeout(0.01):
                sleep(0.02)

        handler_after = signal.getsignal(signal.SIGALRM)
        self.assertEqual(handler_after, handler)
예제 #27
0
 def test_nested_silent_timeout_with_exception(self):
     with self.assertRaises(CancelledError):
         with timeout(0.05, exception=None):
             with timeout(0.01):
                 sleep(0.03)
예제 #28
0
 def test_timeout_remaining_property_after(self):
     t = timeout(0.01)
     with t:
         pass
     sleep(0.02)
     self.assertEqual(t.remaining, 0)
예제 #29
0
 def test_timeout_remaining_property_berfore(self):
     seconds = 1.0
     t = timeout(seconds)
     with t:
         self.assertLess(t.remaining, seconds)
예제 #30
0
 def test_timeout_remaining_property(self):
     with timeout(1.0) as t:
         self.assertLess(t.remaining, 1.0)