async def test_receive_message_successfully(self, caplog): async with Context() as (client, server, subscriber): await server.send(MessageType.Envs, 'some-payload') await assert_received_eventually(subscriber, [ (MessageType.Envs.value, 'some-payload'), ]) assert warn_messages(caplog) == []
async def test_connection_when_server_shuts_down(self, caplog): async with Context() as (client, server, subscriber): await server.stop() assert client.connected is False await server.start() while client.connected is False: await asyncio.sleep(0.01) assert warn_messages(caplog) == [ 'server error [code: 503], will try to re-connect' ]
async def test_reconnect(self, caplog): async with Context() as (client, server, subscriber): await server.send(MessageType.Envs, 'some-payload') while len(server.connections) == 0: await asyncio.sleep(0.01) await server.restart() while len(server.connections) == 1: await asyncio.sleep(0.01) (path, headers) = server.connections[1] assert path == '/?fallback' # different URL assert headers['X-Connection-Attempt'] == '2' assert headers[ 'X-Message-Num-Client'] == '0' # 1 client message for ack'ing the server message assert headers[ 'X-Message-Num-Server'] == '0' # 1 server message was acked assert warn_messages(caplog) == []
async def test_connection(self, caplog): async with Context() as (client, server, subscriber): while len(server.connections) == 0: await asyncio.sleep(0.01) (path, headers) = server.connections[0] assert path == '/?primary' assert headers['X-API-Key'] == 'some-api-key' assert headers['X-Client-Name'] == 'pytest-conquer' assert headers['X-Client-Version'] == '1.0' assert headers['X-Connection-Attempt'] == '1' assert headers['X-Connection-ID'] == server.connections[0][1][ 'X-Connection-ID'] assert headers['X-Env'] == 'provider' assert headers['X-Message-Format'] == 'json' assert headers['X-Message-Num-Client'] == '-1' assert headers['X-Message-Num-Server'] == '-1' assert warn_messages(caplog) == []
def test_connection_when_server_not_reachable(self, datetime_mock, caplog, event_loop): settings = { 'api_retry_limit': '3', 'api_wait_limit': '0', 'api_domain': 'doesnot.exist:801', 'api_domain_fallback': 'doesnot.exist:802', } self.client = Client(MockSettings(settings)) with pytest.raises(SystemExit): datetime_mock.utcnow = mock.Mock(return_value=datetime(2000, 1, 1)) event_loop.run_until_complete(self.client.start()) event_loop.run_until_complete(asyncio.sleep(1)) assert warn_messages(caplog) == [ 'lost socket connection, will try to re-connect', 'lost socket connection, will try to re-connect', 'lost socket connection, will try to re-connect', 'lost socket connection, will try to re-connect', ] assert error_messages(caplog) == [ '\n' '\n' ' ' '================================================================================\n' '\n' ' [ERROR] [CONQUER] COULD NOT CONNECT:\n' '\n' ' Unable to connect to server, giving up.\n' ' Please try again and contact support if the error persists.\n' '\n' ' [Client-Name = pytest-conquer]\n' ' [Client-Version = 1.0]\n' ' [Connection-Attempt = 4]\n' ' [Connection-ID = ' + str(self.client.id) + ']\n' ' [Timestamp = 2000-01-01T00:00:00]\n' '\n' ' ' '================================================================================\n' '\n', ]