def test_batched_cancel_too_late(framework_aio):
    '''
    nothing bad happens if we cancel() after the callbacks
    '''
    # Trollius doesn't come with this, so won't work on py2
    pytest.importorskip('asyncio.test_utils')
    from asyncio.test_utils import TestLoop

    def time_gen():
        yield
        yield
    new_loop = TestLoop(time_gen)
    calls = []

    def foo(*args, **kw):
        calls.append((args, kw))

    with replace_loop(new_loop):
        batched = txaio.make_batched_timer(1)
        call = batched.call_later(2, foo, "a call")

        new_loop.advance_time(2.1)
        new_loop._run_once()
        assert len(calls) == 1
        call.cancel()
        assert len(calls) == 1
        new_loop.advance_time(1)
        new_loop._run_once()
        assert len(calls) == 1
def test_batched_cancel(framework_aio):
    '''
    we can cancel uncalled call_laters
    '''
    # Trollius doesn't come with this, so won't work on py2
    pytest.importorskip('asyncio.test_utils')
    from asyncio.test_utils import TestLoop

    def time_gen():
        yield
        yield
    new_loop = TestLoop(time_gen)
    calls = []

    def foo(*args, **kw):
        calls.append((args, kw))

    with replace_loop(new_loop):
        batched = txaio.make_batched_timer(1)
        call = batched.call_later(2, foo, "a call")

        # advance clock a bit; shouldn't have fired anything yet
        new_loop.advance_time(1.2)
        new_loop._run_once()

        call.cancel()

        # advancing clock past where we "should" get the call, if it
        # were still active.
        new_loop.advance_time(4.0)
        new_loop._run_once()
        assert len(calls) == 0
Beispiel #3
0
def test_sleep(event_loop: EventLoop):
    def near(x, y, relative=5):
        return y - relative < x < y + relative

    start = event_loop.time()
    yield from asyncio.sleep(1000000)
    assert near(event_loop.time(), 1000000)
    yield from asyncio.sleep(100)
    assert near(event_loop.time() - start, 1000000 + 100)
Beispiel #4
0
def test_call_later_aio(framework_aio):
    '''
    Wait for two Futures.
    '''

    # Trollius doesn't come with this, so won't work on py2
    pytest.importorskip('asyncio.test_utils')

    def time_gen():
        when = yield
        assert when == 1
        # even though we only do one call, I guess TestLoop needs
        # a "trailing" yield? "or something"
        when = yield 0

    from asyncio.test_utils import TestLoop
    new_loop = TestLoop(time_gen)

    calls = []
    with replace_loop(new_loop) as fake_loop:

        def foo(*args, **kw):
            calls.append((args, kw))

        delay = txaio.call_later(1, foo, 5, 6, 7, foo="bar")
        assert len(calls) == 0
        assert hasattr(delay, 'cancel')
        fake_loop.advance_time(2)
        fake_loop._run_once()

        assert len(calls) == 1
        assert calls[0][0] == (5, 6, 7)
        assert calls[0][1] == dict(foo="bar")
Beispiel #5
0
def event_loop():
    def gen():
        """The ``gen`` argument to :class: TestLoop's constructor.

        Note: the class isn't documented.  From code reading, it appears that
        the ``when`` values are sent to the generator, which yields advance
        increments, stopping the generation when ``when == 0``.
        """
        advance = 0
        while True:
            when = yield advance
            if not when: break
            advance = when - loop.time()

    loop = TestLoop(gen)
    yield loop
    loop.close()
def test_batched_successful_call_explicit_loop(framework_aio):
    '''
    batched calls really happen in batches
    '''
    # Trollius doesn't come with this, so won't work on py2
    pytest.importorskip('asyncio.test_utils')
    from asyncio.test_utils import TestLoop

    def time_gen():
        yield
        yield
    new_loop = TestLoop(time_gen)
    calls = []

    def foo(*args, **kw):
        calls.append((args, kw))

    txa = txaio.with_config(loop=new_loop)

    batched = txa.make_batched_timer(5)

    batched.call_later(1, foo, "first call")
    new_loop.advance_time(2.0)
    new_loop._run_once()
    assert len(calls) == 1
def test_batched_successful_call(framework_aio):
    '''
    batched calls really happen in batches
    '''
    # Trollius doesn't come with this, so won't work on py2
    pytest.importorskip('asyncio.test_utils')
    from asyncio.test_utils import TestLoop

    # XXX I *really* don't get the point of these generators...
    def time_gen():
        yield
        yield
        yield
    new_loop = TestLoop(time_gen)
    calls = []
    with replace_loop(new_loop):
        def foo(*args, **kw):
            calls.append((args, kw))

        batched = txaio.make_batched_timer(5)

        # add 3 calls: first 2 should be in the same bucket, 3rd in
        # another bucket
        batched.call_later(5.1, foo, "first call")
        batched.call_later(9.9, foo, "second call")
        batched.call_later(10.1, foo, "third call")

        # advancing 4.9 seconds: shouldn't have expired from a bucket
        new_loop.advance_time(4.9)
        new_loop._run_once()
        assert len(calls) == 0

        # tick over past first bucket; first two calls should happen
        # (the "5s -> 10s" bucket)
        new_loop.advance_time(0.2)
        new_loop._run_once()
        assert len(calls) == 2
        assert calls[0] == (("first call", ), dict())
        assert calls[1] == (("second call", ), dict())

        # tick into next bucket
        new_loop.advance_time(5)
        new_loop._run_once()
        assert len(calls) == 3
        assert calls[2] == (("third call", ), dict())
Beispiel #8
0
 def setUp(self):
     self.loop = TestLoop()
Beispiel #9
0
class TestConnection(unittest.TestCase):
    def setUp(self):
        self.loop = TestLoop()

    def test_bad_json(self):
        @asyncio.coroutine
        def go():
            conn = Connection('host', 9999, loop=self.loop)
            resp = mock.Mock()
            resp.status = 401
            r2 = asyncio.Future(loop=self.loop)
            r2.set_result('error text')
            resp.text.return_value = r2
            fut = asyncio.Future(loop=self.loop)
            fut.set_result(resp)
            conn._request = mock.Mock(return_value=fut)

            with self.assertRaises(TransportError) as ctx:
                yield from conn.perform_request('GET', '/data', None, None)
            self.assertEqual(401, ctx.exception.status_code)
            self.assertEqual('error text', ctx.exception.error)
            self.assertEqual(None, ctx.exception.info)

        self.loop.run_until_complete(go())

    def test_bad_status_400(self):
        @asyncio.coroutine
        def go():
            conn = Connection('host', 9999, loop=self.loop)
            resp = mock.Mock()
            resp.status = 400
            r2 = asyncio.Future(loop=self.loop)
            r2.set_result('{"a": 1}')
            resp.text.return_value = r2
            fut = asyncio.Future(loop=self.loop)
            fut.set_result(resp)
            conn._request = mock.Mock(return_value=fut)

            with self.assertRaises(RequestError) as ctx:
                yield from conn.perform_request('GET', '/data', None, None)
            self.assertEqual(400, ctx.exception.status_code)
            self.assertEqual('{"a": 1}', ctx.exception.error)
            self.assertEqual({"a": 1}, ctx.exception.info)

        self.loop.run_until_complete(go())

    def test_bad_status_404(self):
        @asyncio.coroutine
        def go():
            conn = Connection('host', 9999, loop=self.loop)
            resp = mock.Mock()
            resp.status = 404
            r2 = asyncio.Future(loop=self.loop)
            r2.set_result('{"a": 1}')
            resp.text.return_value = r2
            fut = asyncio.Future(loop=self.loop)
            fut.set_result(resp)
            conn._request = mock.Mock(return_value=fut)

            with self.assertRaises(NotFoundError) as ctx:
                yield from conn.perform_request('GET', '/data', None, None)
            self.assertEqual(404, ctx.exception.status_code)
            self.assertEqual('{"a": 1}', ctx.exception.error)
            self.assertEqual({"a": 1}, ctx.exception.info)

        self.loop.run_until_complete(go())

    def test_bad_status_409(self):
        @asyncio.coroutine
        def go():
            conn = Connection('host', 9999, loop=self.loop)
            resp = mock.Mock()
            resp.status = 409
            r2 = asyncio.Future(loop=self.loop)
            r2.set_result('{"a": 1}')
            resp.text.return_value = r2
            fut = asyncio.Future(loop=self.loop)
            fut.set_result(resp)
            conn._request = mock.Mock(return_value=fut)

            with self.assertRaises(ConflictError) as ctx:
                yield from conn.perform_request('GET', '/data', None, None)
            self.assertEqual(409, ctx.exception.status_code)
            self.assertEqual('{"a": 1}', ctx.exception.error)
            self.assertEqual({"a": 1}, ctx.exception.info)

        self.loop.run_until_complete(go())