예제 #1
0
파일: test_aio.py 프로젝트: beenje/sockio
async 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):
        await sock.open()
    assert not sock.connected()
    assert sock.connection_counter == 0
예제 #2
0
파일: test_aio.py 프로젝트: beenje/sockio
async 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):
        await sock.write_readline(IDN_REQ)
    assert not sock.connected()
    assert sock.in_waiting() == 0
    assert sock.connection_counter == 0
예제 #3
0
파일: test_aio.py 프로젝트: beenje/sockio
def test_socket_creation():
    sock = TCP("example.com", 34567)
    assert sock.host == "example.com"
    assert sock.port == 34567
    assert sock.connection_timeout is None
    assert sock.timeout is None
    assert sock.auto_reconnect
    assert not sock.connected()
    assert sock.in_waiting() == 0
    assert sock.connection_counter == 0
예제 #4
0
def test_socket_creation():
    sock = TCP('example.com', 34567)
    assert sock.host == 'example.com'
    assert sock.port == 34567
    assert sock.auto_reconnect == True
    assert not sock.connected
    assert sock.connection_counter == 0
예제 #5
0
async def test_write_fail(unused_tcp_port):
    sock = TCP('0', unused_tcp_port)
    assert not sock.connected
    assert sock.connection_counter == 0

    with pytest.raises(ConnectionRefusedError):
        await sock.write(IDN_REQ)
    assert not sock.connected
    assert sock.connection_counter == 0
예제 #6
0
파일: test_aio.py 프로젝트: beenje/sockio
async def test_error_callback(aio_server):
    host, port = aio_server.sockets[0].getsockname()

    state = dict(made=0)

    def error_callback():
        state["made"] += 1
        raise RuntimeError("cannot handle this")

    aio_tcp = TCP(host, port, on_connection_made=error_callback)

    assert not aio_tcp.connected()
    assert aio_tcp.connection_counter == 0
    assert state["made"] == 0

    await aio_tcp.open()
    assert aio_tcp.connected()
    assert aio_tcp.connection_counter == 1
    assert state["made"] == 1
예제 #7
0
파일: test_aio.py 프로젝트: beenje/sockio
async def test_open_timeout():
    timeout = 0.1
    # TODO: Not cool to use an external connection
    aio_tcp = TCP("www.google.com", 81, connection_timeout=timeout)
    with pytest.raises(ConnectionTimeoutError):
        start = time.time()
        try:
            await aio_tcp.open()
        finally:
            dt = time.time() - start
            assert dt > timeout and dt < (timeout + 0.05)

    # TODO: Not cool to use an external connection
    aio_tcp = TCP("www.google.com", 82)
    with pytest.raises(ConnectionTimeoutError):
        start = time.time()
        try:
            await aio_tcp.open(timeout=timeout)
        finally:
            dt = time.time() - start
            assert dt > timeout and dt < (timeout + 0.05)
예제 #8
0
파일: test_aio.py 프로젝트: beenje/sockio
async def test_eof_callback(aio_server):
    host, port = aio_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

    aio_tcp = TCP(
        host,
        port,
        on_connection_made=made,
        on_connection_lost=lost,
        on_eof_received=eof,
    )
    assert not aio_tcp.connected()
    assert aio_tcp.connection_counter == 0
    assert state["made"] == 0
    assert state["lost"] == 0
    assert state["eof"] == 0

    await aio_tcp.open()
    assert aio_tcp.connected()
    assert aio_tcp.connection_counter == 1
    assert state["made"] == 1
    assert state["lost"] == 0
    assert state["eof"] == 0

    await aio_server.stop()
    await asyncio.sleep(0.01)  # give time for connection to be closed

    assert state["made"] == 1
    assert state["lost"] == 0
    assert state["eof"] == 1
예제 #9
0
async def test_eof_callback(aio_server):
    host, port = aio_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

    aio_tcp = TCP(host,
                  port,
                  on_connection_made=made,
                  on_connection_lost=lost,
                  on_eof_received=eof)
    assert not aio_tcp.connected
    assert aio_tcp.connection_counter == 0
    assert state['made'] == 0
    assert state['lost'] == 0
    assert state['eof'] == 0

    await aio_tcp.open()
    assert aio_tcp.connected
    assert aio_tcp.connection_counter == 1
    assert state['made'] == 1
    assert state['lost'] == 0
    assert state['eof'] == 0

    aio_server.close()
    await aio_server.wait_closed()
    assert not aio_server.is_serving()

    assert state['made'] == 1
    assert state['lost'] == 0
    assert state['eof'] == 1
예제 #10
0
async def test_coroutine_callbacks(aio_server):
    host, port = aio_server.sockets[0].getsockname()
    RESP_TIME = 0.02
    state = dict(made=0, lost=0, eof=0)

    async def made():
        await asyncio.sleep(RESP_TIME)
        state['made'] += 1

    async def lost(exc):
        await asyncio.sleep(RESP_TIME)
        state['lost'] += 1

    async def eof():
        await asyncio.sleep(RESP_TIME)
        state['eof'] += 1

    aio_tcp = TCP(host,
                  port,
                  on_connection_made=made,
                  on_connection_lost=lost,
                  on_eof_received=eof)

    assert not aio_tcp.connected
    assert aio_tcp.connection_counter == 0
    assert state['made'] == 0
    assert state['lost'] == 0
    assert state['eof'] == 0

    await aio_tcp.open()
    assert aio_tcp.connected
    assert aio_tcp.connection_counter == 1
    assert state['made'] == 1
    assert state['lost'] == 0
    assert state['eof'] == 0

    with pytest.raises(ConnectionError):
        await aio_tcp.open()
    assert aio_tcp.connected
    assert aio_tcp.connection_counter == 1
    assert state['made'] == 1
    assert state['lost'] == 0
    assert state['eof'] == 0

    await aio_tcp.close()
    assert not aio_tcp.connected
    assert aio_tcp.connection_counter == 1
    assert state['made'] == 1
    assert state['lost'] == 0
    assert state['eof'] == 0
    await asyncio.sleep(RESP_TIME + 0.01)
    assert state['made'] == 1
    assert state['lost'] == 1
    assert state['eof'] == 0

    await aio_tcp.open()
    assert aio_tcp.connected
    assert aio_tcp.connection_counter == 2
    assert state['made'] == 2
    assert state['lost'] == 1
    assert state['eof'] == 0

    await aio_tcp.close()
    assert not aio_tcp.connected
    assert aio_tcp.connection_counter == 2
    assert state['made'] == 2
    assert state['lost'] == 1
    assert state['eof'] == 0
    await asyncio.sleep(RESP_TIME + 0.01)
    assert state['made'] == 2
    assert state['lost'] == 2
    assert state['eof'] == 0

    await aio_tcp.close()
    assert not aio_tcp.connected
    assert aio_tcp.connection_counter == 2
    assert state['made'] == 2
    assert state['lost'] == 2
    assert state['eof'] == 0
    await asyncio.sleep(RESP_TIME + 0.01)
    assert state['made'] == 2
    assert state['lost'] == 2
    assert state['eof'] == 0
예제 #11
0
파일: test_aio.py 프로젝트: beenje/sockio
async def test_callbacks(aio_server):
    host, port = aio_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

    aio_tcp = TCP(
        host,
        port,
        on_connection_made=made,
        on_connection_lost=lost,
        on_eof_received=eof,
    )
    assert not aio_tcp.connected()
    assert aio_tcp.connection_counter == 0
    assert state["made"] == 0
    assert state["lost"] == 0
    assert state["eof"] == 0

    await aio_tcp.open()
    assert aio_tcp.connected()
    assert aio_tcp.connection_counter == 1
    assert state["made"] == 1
    assert state["lost"] == 0
    assert state["eof"] == 0

    with pytest.raises(ConnectionError):
        await aio_tcp.open()
    assert aio_tcp.connected()
    assert aio_tcp.connection_counter == 1
    assert state["made"] == 1
    assert state["lost"] == 0
    assert state["eof"] == 0

    await aio_tcp.close()
    assert not aio_tcp.connected()
    assert aio_tcp.connection_counter == 1
    assert state["made"] == 1
    assert state["lost"] == 1
    assert state["eof"] == 0

    await aio_tcp.open()
    assert aio_tcp.connected()
    assert aio_tcp.connection_counter == 2
    assert state["made"] == 2
    assert state["lost"] == 1
    assert state["eof"] == 0

    await aio_tcp.close()
    assert not aio_tcp.connected()
    assert aio_tcp.connection_counter == 2
    assert state["made"] == 2
    assert state["lost"] == 2
    assert state["eof"] == 0

    await aio_tcp.close()
    assert not aio_tcp.connected()
    assert aio_tcp.connection_counter == 2
    assert state["made"] == 2
    assert state["lost"] == 2
    assert state["eof"] == 0