def test_spawn_ipv4(): cfile = create_config(("listen=127.0.0.1:9999",)) Config(cfile).load() loop = asyncio.new_event_loop() with mock.patch("socket.socket.bind"): supervisor = Supervisor(loop=loop) assert len(supervisor.socks) == 1 supervisor.close_socks() loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
def test_create(): cfile = create_config(('listen=:9999, :::9999', 'workers=2', )) Config(cfile).load() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) with mock.patch('socket.socket.bind'), \ mock.patch('blackhole.worker.Worker.start'): supervisor = Supervisor(loop=loop) supervisor.start_workers() assert len(supervisor.workers) == 2 loop.stop()
def test_run(): cfile = create_config(('listen=:9999, :::9999', 'workers=2', )) Config(cfile).load() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) with mock.patch('socket.socket.bind'), \ mock.patch('blackhole.worker.Worker.start'): supervisor = Supervisor(loop) with mock.patch('asyncio.unix_events._UnixSelectorEventLoop.' 'run_forever'): supervisor.run() assert len(supervisor.workers) == 2 supervisor.loop.stop()
def test_run(): cfile = create_config(("listen=:9999, :::9999", "workers=2")) Config(cfile).load() loop = asyncio.new_event_loop() with mock.patch("socket.socket.bind"), mock.patch( "blackhole.worker.Worker.start" ): supervisor = Supervisor(loop=loop) with mock.patch("{0}.run_forever".format(_LOOP)): supervisor.run() assert len(supervisor.workers) == 2 loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
def test_stop(): cfile = create_config(('listen=:9999, :::9999', 'workers=2', )) Config(cfile).load() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) with mock.patch('socket.socket.bind'), \ mock.patch('blackhole.worker.Worker.start'): supervisor = Supervisor(loop) supervisor.start_workers() assert len(supervisor.workers) == 2 with mock.patch('blackhole.worker.Worker.stop') as mock_stop, \ pytest.raises(SystemExit) as err: supervisor.stop() assert mock_stop.call_count == 2 assert str(err.value) == '0' supervisor.loop.stop()
def test_stop(): cfile = create_config(("listen=:9999, :::9999", "workers=2")) Config(cfile).load() loop = asyncio.new_event_loop() with mock.patch("socket.socket.bind"), mock.patch( "blackhole.worker.Worker.start" ): supervisor = Supervisor(loop=loop) supervisor.start_workers() assert len(supervisor.workers) == 2 with mock.patch( "blackhole.worker.Worker.stop" ) as mock_stop, pytest.raises(SystemExit) as exc: supervisor.stop() assert mock_stop.call_count == 2 assert exc.value.code == 0 loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
def test_spawn_ipv4_tls(): cert = create_file("cert.pem") key = create_file("key.key") cfile = create_config( ( "listen=127.0.0.1:9998", "tls_listen=127.0.0.1:9999", "tls_cert={}".format(cert), "tls_key={}".format(key), ) ) conf = Config(cfile).load() conf.args = Args((("less_secure", True),)) loop = asyncio.new_event_loop() with mock.patch("socket.socket.bind"), mock.patch( "ssl.create_default_context" ): supervisor = Supervisor(loop=loop) assert len(supervisor.socks) == 2 supervisor.close_socks() loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
def test_spawn_ipv4_tls_dhparams(): cert = create_file("test.pem", cert_data) key = create_file("test.key", key_data) dhparams = create_file("dhparams.pem", dhparams_data) cfile = create_config( ( "listen=127.0.0.1:9998", "tls_listen=127.0.0.1:9999", "tls_cert={}".format(cert), "tls_key={}".format(key), "tls_dhparams={}".format(dhparams), ) ) conf = Config(cfile).load() conf.args = Args((("less_secure", False),)) loop = asyncio.new_event_loop() with mock.patch("socket.socket.bind"): supervisor = Supervisor(loop=loop) assert len(supervisor.socks) == 2 supervisor.close_socks() loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()