Ejemplo n.º 1
0
    async def test_request_raise_cancelled_error_if_hammertime_is_close(
            self, loop):
        hammertime = HammerTime(loop=loop)

        await hammertime.close()

        self.assertTrue(hammertime.is_closed)
        with self.assertRaises(asyncio.CancelledError):
            hammertime.request("http://example.com")
Ejemplo n.º 2
0
    async def test_loop_over_results(self, loop):
        h = HammerTime(loop=loop, request_engine=FakeEngine())
        h.request("http://example.com/1")
        h.request("http://example.com/2")

        out = set()

        async for entry in h.successful_requests():
            out.add(entry.response.content)

        self.assertEqual(out, {"http://example.com/1", "http://example.com/2"})
        self.assertEqual(h.completed_count, 2)
Ejemplo n.º 3
0
    async def test_skip_results_that_fail(self, loop):
        h = HammerTime(loop=loop, request_engine=FakeEngine())
        h.heuristics.add(BlockRequest("http://example.com/1"))
        h.request("http://example.com/1")
        h.request("http://example.com/2")

        out = set()

        async for entry in h.successful_requests():
            out.add(entry.response.content)

        self.assertEqual(out, {"http://example.com/2"})
        self.assertEqual(h.completed_count, 2)
Ejemplo n.º 4
0
    async def test_no_successful_request_returned_when_requests_are_cancelled(
            self, loop):
        engine = MagicMock()
        engine.perform = make_mocked_coro(
            raise_exception=asyncio.CancelledError)
        hammertime = HammerTime(loop=loop, request_engine=engine)

        for i in range(5):
            hammertime.request("http://example.com")

        successful_requests = []
        async for request in hammertime.successful_requests():
            successful_requests.append(request)

        self.assertEqual(successful_requests, [])
Ejemplo n.º 5
0
    async def test_explicit_abandon_obtained_when_requested(self, loop):
        h = HammerTime(loop=loop, request_engine=FakeEngine(), retry_count=2)
        h.heuristics.add(RejectStatusCode(range(0, 600)))  # Everything
        future = h.request("http://example.com/1")

        with self.assertRaises(RejectRequest):
            await future

        self.assertEqual(0, h.stats.retries)
Ejemplo n.º 6
0
    async def test_provide_exception_when_resolving_specific_promise(
            self, loop):
        h = HammerTime(loop=loop, request_engine=FakeEngine())
        h.heuristics.add(BlockRequest("http://example.com/1"))
        future = h.request("http://example.com/1")

        with self.assertRaises(StopRequest):
            await future

        with self.assertRaises(StopRequest):
            await future
Ejemplo n.º 7
0
    async def test_wait_for_multiple_requests(self, loop):
        h = HammerTime(loop=loop, request_engine=FakeEngine())
        promise_1 = h.request("http://example.com/1")
        promise_2 = h.request("http://example.com/2")

        entry = await promise_2
        self.assertEqual(entry.response.content, "http://example.com/2")

        entry = await promise_1
        self.assertEqual(entry.response.content, "http://example.com/1")
        await h.close()

        self.assertEqual(h.completed_count, 2)