def test_write_readline_fail(unused_tcp_port): sock = TCP("0", unused_tcp_port) assert not sock.connected() assert sock.connection_counter == 0 with pytest.raises(ConnectionRefusedError): sock.write_readline(IDN_REQ) assert not sock.connected() assert sock.connection_counter == 0
def test_open_fail(unused_tcp_port): sock = TCP("0", unused_tcp_port) assert not sock.connected() assert sock.connection_counter == 0 with pytest.raises(ConnectionRefusedError): sock.open() assert not sock.connected() assert sock.connection_counter == 0
def test_socket_creation(): sock = TCP("example.com", 34567) assert sock.host == "example.com" assert sock.port == 34567 assert sock.auto_reconnect is True assert not sock.connected() assert sock.connection_counter == 0
def test_callbacks(sio_server): host, port = sio_server.sockets[0].getsockname() state = dict(made=0, lost=0, eof=0) def made(): state["made"] += 1 def lost(exc): state["lost"] += 1 def eof(): state["eof"] += 1 sio_tcp = TCP( host, port, on_connection_made=made, on_connection_lost=lost, on_eof_received=eof, ) assert not sio_tcp.connected() assert sio_tcp.connection_counter == 0 assert state["made"] == 0 assert state["lost"] == 0 assert state["eof"] == 0 sio_tcp.open() assert sio_tcp.connected() assert sio_tcp.connection_counter == 1 assert state["made"] == 1 assert state["lost"] == 0 assert state["eof"] == 0 with pytest.raises(ConnectionError): sio_tcp.open() assert sio_tcp.connected() assert sio_tcp.connection_counter == 1 assert state["made"] == 1 assert state["lost"] == 0 assert state["eof"] == 0 sio_tcp.close() assert not sio_tcp.connected() assert sio_tcp.connection_counter == 1 assert state["made"] == 1 assert state["lost"] == 1 assert state["eof"] == 0 sio_tcp.open() assert sio_tcp.connected() assert sio_tcp.connection_counter == 2 assert state["made"] == 2 assert state["lost"] == 1 assert state["eof"] == 0 sio_tcp.close() assert not sio_tcp.connected() assert sio_tcp.connection_counter == 2 assert state["made"] == 2 assert state["lost"] == 2 assert state["eof"] == 0 sio_tcp.close() assert not sio_tcp.connected() assert sio_tcp.connection_counter == 2 assert state["made"] == 2 assert state["lost"] == 2 assert state["eof"] == 0