async def asyncSetUp(self): await super().asyncSetUp() self.processor = statsd.Processor(host=self.statsd_server.host, port=self.statsd_server.port, reconnect_sleep=0.25) asyncio.create_task(self.processor.run()) await self.wait_for(self.statsd_server.client_connected.acquire())
async def test_that_processor_can_be_cancelled(self): processor = statsd.Processor(host=self.statsd_server.host, port=self.statsd_server.port) task = asyncio.create_task(processor.run()) await self.wait_for(self.statsd_server.client_connected.acquire()) task.cancel() await self.wait_for(processor.stopped.wait())
async def test_starting_and_stopping_without_connecting(self): host, port = self.statsd_server.host, self.statsd_server.port self.statsd_server.close() await self.wait_for(self.statsd_server.wait_closed()) processor = statsd.Processor(host=host, port=port) asyncio.create_task(processor.run()) await self.wait_for(processor.running.wait()) await processor.stop()
async def test_socket_resets(self): processor = statsd.Processor(host=self.statsd_server.host, port=self.statsd_server.port) asyncio.create_task(processor.run()) await self.wait_for(self.statsd_server.client_connected.acquire()) self.statsd_server.transports[0].close() await self.wait_for(self.statsd_server.client_connected.acquire()) await self.wait_for(processor.stop())
async def test_that_processor_connects_and_disconnects(self): processor = statsd.Processor(host=self.statsd_server.host, port=self.statsd_server.port) asyncio.create_task(processor.run()) await self.wait_for(self.statsd_server.client_connected.acquire()) await self.wait_for(processor.stop()) self.assertEqual(1, self.statsd_server.connections_made) self.assertEqual(1, self.statsd_server.connections_lost)
async def test_shutdown_when_disconnected(self): processor = statsd.Processor(host=self.statsd_server.host, port=self.statsd_server.port) asyncio.create_task(processor.run()) await self.wait_for(self.statsd_server.client_connected.acquire()) self.statsd_server.close() await self.statsd_server.wait_closed() await self.wait_for(processor.stop())
async def test_that_protocol_exceptions_are_logged(self): processor = statsd.Processor(host=self.statsd_server.host, port=self.statsd_server.port) asyncio.create_task(processor.run()) await self.wait_for(processor.running.wait()) with self.assertLogs(processor.logger, level=logging.ERROR) as cm: processor.queue.put_nowait('not-bytes') # type: ignore[arg-type] while processor.queue.qsize() > 0: await asyncio.sleep(0.1) for record in cm.records: if record.exc_info is not None and record.funcName == 'run': break else: self.fail('Expected run to log exception') await processor.stop()
async def test_that_processor_reconnects(self): processor = statsd.Processor(host=self.statsd_server.host, port=self.statsd_server.port) asyncio.create_task(processor.run()) await self.wait_for(self.statsd_server.client_connected.acquire()) # Now that the server is running and the client has connected, # cancel the server and let it die off. self.statsd_server.close() await self.statsd_server.wait_closed() until = time.time() + self.test_timeout while processor.connected: await asyncio.sleep(0.1) if time.time() >= until: self.fail('processor never disconnected') # Start the server on the same port and let the client reconnect. self.statsd_task = asyncio.create_task(self.statsd_server.run()) await self.wait_for(self.statsd_server.client_connected.acquire()) self.assertTrue(processor.connected) await self.wait_for(processor.stop())
def test_that_processor_fails_when_host_is_none(self): with self.assertRaises(RuntimeError) as context: statsd.Processor(host=None, port=12345) # type: ignore[arg-type] self.assertIn('host', str(context.exception))
async def test_that_stopping_when_not_running_is_safe(self): processor = statsd.Processor(host=self.statsd_server.host, port=self.statsd_server.port) await self.wait_for(processor.stop())