Beispiel #1
0
def test_echo_server_client_doers():
    """
    Test EchoServerDoer ClientDoer classes
    """
    tock = 0.03125
    ticks = 16
    limit = ticks * tock
    doist = doing.Doist(tock=tock, real=True, limit=limit)
    assert doist.tyme == 0.0  # on next cycle
    assert doist.tock == tock == 0.03125
    assert doist.real == True
    assert doist.limit == limit == 0.5
    assert doist.doers == []

    port = 6120
    server = tcp.Server(host="", port=port)
    client = tcp.Client(tymth=doist.tymen(), host="localhost", port=port)

    serdoer = tcp.EchoServerDoer(tymth=doist.tymen(), server=server)
    assert serdoer.server == server
    assert serdoer.tyme == serdoer.server.tyme == doist.tyme
    clidoer = tcp.ClientDoer(tymth=doist.tymen(), client=client)
    assert clidoer.client == client
    assert clidoer.tyme == clidoer.client.tyme == doist.tyme

    assert serdoer.tock == 0.0  # ASAP
    assert clidoer.tock == 0.0  # ASAP

    doers = [serdoer, clidoer]

    msgTx = b"Hello me maties!"
    clidoer.client.tx(msgTx)

    doist.do(doers=doers)
    assert doist.tyme == limit
    assert server.opened == False
    assert client.opened == False

    assert not client.txbs
    msgEx = bytes(client.rxbs)  # echoed back message
    assert msgEx == msgTx

    ca, ix = list(server.ixes.items())[0]
    assert bytes(ix.rxbs) == b""  # empty server rxbs becaue echoed
    """End Test """
Beispiel #2
0
def test_server_client_doers():
    """
    Test ServerDoer ClientDoer classes
    """
    tock = 0.03125
    ticks = 16
    limit = ticks * tock
    doist = doing.Doist(tock=tock, real=True, limit=limit)
    assert doist.tyme == 0.0  # on next cycle
    assert doist.tock == tock == 0.03125
    assert doist.real == True
    assert doist.limit == limit == 0.5
    assert doist.doers == []

    port = 6120
    server = tcp.Server(host="", port=port)
    # client needs tymth in order to init its .tymer
    client = tcp.Client(tymth=doist.tymen(), host="localhost", port=port)
    assert client.tyme == doist.tyme

    serdoer = tcp.ServerDoer(tymth=doist.tymen(), server=server)
    assert serdoer.server == server
    assert serdoer.tyme == serdoer.server.tyme == doist.tyme
    clidoer = tcp.ClientDoer(tymth=doist.tymen(), client=client)
    assert clidoer.client == client
    assert clidoer.tyme == clidoer.client.tyme == doist.tyme

    assert serdoer.tock == 0.0  # ASAP
    assert clidoer.tock == 0.0  # ASAP

    doers = [serdoer, clidoer]

    msgTx = b"Hello me maties!"
    clidoer.client.tx(msgTx)

    doist.do(doers=doers)
    assert doist.tyme == limit
    assert server.opened == False
    assert client.opened == False

    assert not client.txbs
    ca, ix = list(server.ixes.items())[0]
    msgRx = bytes(ix.rxbs)
    assert msgRx == msgTx
    """End Test """
Beispiel #3
0
def test_tcp_basic():
    """
    Test the tcp connection between client and server

    client send from and receive to port is ephemeral
    server receive to and send from port is well known

    Server listens on ist well know  receive to and send from port

    So incoming to server.
        Source address is client host and client ephemeral port
        Destination address is server host and server well known port

    Each accept socket on server is a different duple of client source, server dest
        all the dest are the same but each source is differenct so can route
        based on the source.

    Server routes incoming packets to accept socket port. The routing uses
           the clients send from ephemeral port to do the routing to the
           correct accept socket. All the accept sockets have the same local
           port but a different remote IP host .
    The servers accept socket port is the well known port so still receives to
           and sends from its well know port.
    The server sends to and receives from the clients ephemeral port number.


    """
    tymist = tyming.Tymist()
    client = tcp.Client(tymth=tymist.tymen())
    assert client.tymeout == 0.0
    assert isinstance(client.tymer, tyming.Tymer)
    assert client.tymer.duration == client.tymeout

    assert client.ha == ('127.0.0.1', 56000)
    assert (client.host, client.port) == client.ha
    assert client.hostname == client.host
    assert client.cs == None
    assert client.ca == (None, None)
    assert client.accepted == False
    assert client.cutoff == False
    assert client.reconnectable == False
    assert client.opened == False

    assert client.bs == 8096
    assert isinstance(client.txbs, bytearray)
    assert isinstance(client.rxbs, bytearray)
    assert client.wl == None

    tymist = tyming.Tymist()
    with tcp.openClient(tymth=tymist.tymen(), tymeout=0.5) as client:
        assert client.tymeout == 0.5
        assert client.ha == ('127.0.0.1', 56000)
        assert client.opened == True
        assert client.accepted == False
        assert client.cutoff == False
        assert client.reconnectable == False

    assert client.opened == False
    assert client.accepted == False
    assert client.cutoff == False

    server = tcp.Server()
    assert server.tymeout == 1.0

    assert server.ha == ('', 56000)
    assert server.eha == ('127.0.0.1', 56000)
    assert server.opened == False

    assert server.bs == 8096
    assert isinstance(server.axes, deque)
    assert isinstance(server.ixes, dict)
    assert server.wl == None

    with tcp.openServer(tymth=tymist.tymen(), tymeout=1.5) as server:
        assert server.ha == ('0.0.0.0', 56000)
        assert server.eha == ('127.0.0.1', 56000)
        assert server.opened == True

    assert server.opened == False

    tymist = tyming.Tymist()
    with tcp.openServer(tymth=tymist.tymen(), ha=("", 6101)) as server, \
         tcp.openClient(tymth=tymist.tymen(), ha=("127.0.0.1", 6101)) as beta, \
         tcp.openClient(tymth=tymist.tymen(), ha=("127.0.0.1", 6101)) as gamma:

        assert server.opened == True
        assert beta.opened == True
        assert gamma.opened == True

        assert server.ha == ('0.0.0.0', 6101)  # listen interface
        assert server.eha == ('127.0.0.1', 6101
                              )  # normalized listen/accept external interface
        assert beta.ha == (
            '127.0.0.1', 6101
        )  # server listen/accept maybe sha  (server host address)

        assert beta.accepted == False
        assert beta.connected == False
        assert beta.cutoff == False

        assert gamma.accepted == False
        assert gamma.connected == False
        assert gamma.cutoff == False

        #  connect beta to server
        while not (beta.connected and beta.ca in server.ixes):
            beta.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert beta.accepted == True
        assert beta.connected == True
        assert beta.cutoff == False
        assert beta.ca == beta.cs.getsockname()  # local connection address
        assert beta.ha == beta.cs.getpeername()  # remote connection address
        assert server.eha == beta.ha  # server external, beta external for server

        ixBeta = server.ixes[beta.ca]
        assert ixBeta.cs.getsockname() == beta.cs.getpeername(
        )  # ixBeta local beta remote
        assert ixBeta.cs.getpeername() == beta.cs.getsockname(
        )  # ixBeta remote beta local
        assert ixBeta.ca == beta.ca == ixBeta.cs.getpeername()
        assert ixBeta.ha == beta.ha == ixBeta.cs.getsockname()

        msgOut = b"Beta sends to Server"
        count = beta.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgOut == msgIn

        # receive without sending
        msgIn = ixBeta.receive()
        assert msgIn is None

        # send multiple
        msgOut1 = b"First Message"
        count = beta.send(msgOut1)
        assert count == len(msgOut1)
        msgOut2 = b"Second Message"
        count = beta.send(msgOut2)
        assert count == len(msgOut2)
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgIn == msgOut1 + msgOut2

        # send from server to beta
        msgOut = b"Server sends to Beta"
        count = ixBeta.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = beta.receive()
        assert msgOut == msgIn

        # receive without sending
        msgIn = beta.receive()
        assert msgIn is None

        # build message too big to fit in buffer
        size = beta.actualBufSizes()[0]
        msgOut = bytearray()
        count = 0
        while (len(msgOut) <= size * 4):
            msgOut.extend(b"%032x_" % (count))  #  need to fix this
            count += 1
        assert len(msgOut) >= size * 4

        msgIn = bytearray()
        txbs = bytearray(msgOut)  # make copy
        size = 0
        while len(msgIn) < len(msgOut):
            #if size < len(msgOut):
            #size += beta.send(msgOut[size:])
            count = beta.send(txbs)
            del txbs[:count]
            size += count
            time.sleep(0.05)
            msgIn.extend(ixBeta.receive())
        assert size == len(msgOut)
        assert msgOut == msgIn

        #  gamma to server
        while not (gamma.connected and gamma.ca in server.ixes):
            gamma.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert gamma.accepted == True
        assert gamma.connected == True
        assert gamma.cutoff == False
        assert gamma.ca == gamma.cs.getsockname()
        assert gamma.ha == gamma.cs.getpeername()
        assert server.eha, gamma.ha
        ixGamma = server.ixes[gamma.ca]
        assert ixGamma.cs.getsockname() == gamma.cs.getpeername()
        assert ixGamma.cs.getpeername() == gamma.cs.getsockname()
        assert ixGamma.ca == gamma.ca
        assert ixGamma.ha == gamma.ha

        msgOut = b"Gamma sends to Server"
        count = gamma.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = ixGamma.receive()
        assert msgOut == msgIn

        # receive without sending
        msgIn = ixGamma.receive()
        assert msgIn is None

        # send from server to gamma
        msgOut = b"Server sends to Gamma"
        count = ixGamma.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgOut == msgIn

        # recieve without sending
        msgIn = gamma.receive()
        assert msgIn is None

        # close beta and then attempt to send
        beta.close()
        msgOut = b"Beta send on closed socket"
        with pytest.raises(AttributeError):
            count = beta.send(msgOut)

        # attempt to receive on closed socket
        with pytest.raises(AttributeError):
            msgIn = beta.receive()

        # read on server after closed beta
        msgIn = ixBeta.receive()
        assert msgIn == b''

        # send on server after closed beta
        msgOut = b"Servers sends to Beta after close"
        count = ixBeta.send(msgOut)
        assert count == len(msgOut)  #apparently works

        # close ixBeta manually
        ixBeta.close()
        del server.ixes[ixBeta.ca]
        time.sleep(0.05)
        #after close no socket .cs so can't receive
        with pytest.raises(AttributeError):
            msgIn = ixBeta.receive()
        assert ixBeta.cutoff == True

        # send on gamma to servver first then shutdown gamma sends
        msgOut = b"Gamma sends to server"
        count = gamma.send(msgOut)
        assert count == len(msgOut)
        gamma.shutdownSend()
        time.sleep(0.05)
        msgIn = ixGamma.receive()
        assert msgOut == msgIn  # send before shutdown worked
        msgIn = ixGamma.receive()
        assert msgIn == b''  # gamma shutdown detected, not None
        assert ixGamma.cutoff == True

        # send from server to gamma first  then shutdown server send
        msgOut = b"Server sends to Gamma"
        count = ixGamma.send(msgOut)
        assert count == len(msgOut)
        ixGamma.shutdown()  # shutdown server connection to gamma
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgOut == msgIn
        msgIn = gamma.receive()
        if 'linux' in sys.platform:
            assert msgIn == b''  # server shutdown detected not None
            assert gamma.cutoff == True
        else:
            assert msgIn == None  # server shutdown not detected
            assert gamma.cutoff == False
        time.sleep(0.05)
        msgIn = gamma.receive()
        if 'linux' in sys.platform:
            assert msgIn == b''  # server shutdown detected not None
            assert gamma.cutoff == True
        else:
            assert msgIn == None  # server shutdown not detected
            assert gamma.cutoff == False

        ixGamma.close()  # close server connection to gamma
        del server.ixes[ixGamma.ca]
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgIn == b''  # server close is detected
        assert gamma.cutoff == True

        # reopen beta
        assert beta.reopen() == True
        assert beta.accepted == False
        assert beta.connected == False
        assert beta.cutoff == False

        # reconnect beta to server
        while not (beta.connected and beta.ca in server.ixes):
            beta.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert beta.accepted == True
        assert beta.connected == True
        assert beta.cutoff == False
        assert beta.ca == beta.cs.getsockname()
        assert beta.ha == beta.cs.getpeername()
        assert server.eha == beta.ha

        ixBeta = server.ixes[beta.ca]
        assert ixBeta.cs.getsockname() == beta.cs.getpeername()
        assert ixBeta.cs.getpeername() == beta.cs.getsockname()
        assert ixBeta.ca == beta.ca
        assert ixBeta.ha == beta.ha

        msgOut = b"Beta sends to server"
        count = beta.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgOut == msgIn

        # send from server to beta
        msgOut = b"Server sends to Beta"
        count = ixBeta.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = beta.receive()
        assert msgOut == msgIn

        # send from server to beta then shutdown sever and attempt to send again
        msgOut1 = b"Server sends to Beta"
        count = ixBeta.send(msgOut)
        assert count == len(msgOut1)
        ixBeta.shutdownSend()
        msgOut2 = b"Server send again after server shutdowns socket"
        with pytest.raises(OSError) as ex:
            count = ixBeta.send(msgOut)
        assert ex.typename == 'BrokenPipeError'
        time.sleep(0.05)
        msgIn = beta.receive()
        assert msgOut1 == msgIn
        msgIn = beta.receive()
        assert msgIn == b''  # beta detects shutdown socket
        assert beta.cutoff == True

        # send from beta to server then shutdown beta
        msgOut = b"Beta sends to server"
        count = beta.send(msgOut)
        assert count == len(msgOut)
        beta.shutdown()
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgOut == msgIn
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        if 'linux' in sys.platform:
            assert ixBeta.cutoff == True
            assert msgIn == b''  # server does detect shutdown
        else:
            assert ixBeta.cutoff == False
            assert msgIn == None  # server does not detect shutdown
        beta.close()
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgIn == b''  # server detects closed socket
        ixBeta.close()
        del server.ixes[ixBeta.ca]

        # reopen gamma
        assert gamma.reopen() == True
        assert gamma.accepted == False
        assert gamma.connected == False
        assert gamma.cutoff == False
        # reconnect gamma to server
        while not (gamma.connected and gamma.ca in server.ixes):
            gamma.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert gamma.accepted == True
        assert gamma.connected == True
        assert gamma.cutoff == False
        assert gamma.ca == gamma.cs.getsockname()
        assert gamma.ha == gamma.cs.getpeername()
        assert server.eha == gamma.ha

        ixGamma = server.ixes[gamma.ca]
        assert ixGamma.cs.getsockname() == gamma.cs.getpeername()
        assert ixGamma.cs.getpeername() == gamma.cs.getsockname()
        assert ixGamma.ca == gamma.ca
        assert ixGamma.ha == gamma.ha

        msgOut = b"Gamma sends to server"
        count = gamma.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = ixGamma.receive()
        assert msgOut == msgIn

        # close both sides and reopen Gamma
        gamma.close()
        time.sleep(0.05)
        msgIn = ixGamma.receive()
        assert ixGamma.cutoff == True  # closed on other end
        assert msgIn == b''  # server detects close
        ixGamma.close()
        del server.ixes[ixGamma.ca]

        # reopen gamma
        assert gamma.reopen() == True
        assert gamma.accepted == False
        assert gamma.connected == False
        assert gamma.cutoff == False

        # reconnect gamma to server
        while not (gamma.connected and gamma.ca in server.ixes):
            gamma.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert gamma.accepted == True
        assert gamma.connected == True
        assert gamma.cutoff == False
        assert gamma.ca == gamma.cs.getsockname()
        assert gamma.ha == gamma.cs.getpeername()
        assert server.eha == gamma.ha

        ixGamma = server.ixes[gamma.ca]
        assert ixGamma.cs.getsockname() == gamma.cs.getpeername()
        assert ixGamma.cs.getpeername() == gamma.cs.getsockname()
        assert ixGamma.ca == gamma.ca
        assert ixGamma.ha == gamma.ha

        # send from server to gamma
        msgOut = b"Server sends to Gamma"
        count = ixGamma.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgOut == msgIn

        ixGamma.close()
        del server.ixes[ixGamma.ca]
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgIn == b''  # gamma detects close
        assert gamma.cutoff == True

    assert beta.opened == False
    assert gamma.opened == False
    assert server.opened == False
    """Done Test"""
Beispiel #4
0
def test_requester_respondent_sse_stream_fancy_json():
    """
    Test NonBlocking Http client to server Fancy SSE JSON with chunked transfer
    encoding
    """
    alpha = tcp.Server(port=6101, bufsize=131072)
    assert alpha.reopen()
    assert alpha.ha == ('0.0.0.0', 6101)
    assert alpha.eha == ('127.0.0.1', 6101)

    beta = tcp.Client(ha=alpha.eha, bufsize=131072)
    assert beta.reopen()
    assert not beta.accepted
    assert not beta.connected
    assert not beta.cutoff

    # connect beta to alpha
    while True:
        beta.serviceConnect()
        alpha.serviceConnects()
        if beta.connected and beta.ca in alpha.ixes:
            break
        time.sleep(0.05)

    assert beta.accepted
    assert beta.connected
    assert not beta.cutoff
    assert beta.ca == beta.cs.getsockname()
    assert beta.ha == beta.cs.getpeername()
    assert alpha.eha == beta.ha

    ixBeta = alpha.ixes[beta.ca]
    assert ixBeta.ca is not None
    assert ixBeta.cs is not None
    assert ixBeta.cs.getsockname() == beta.cs.getpeername()
    assert ixBeta.cs.getpeername() == beta.cs.getsockname()
    assert ixBeta.ca == beta.ca
    assert ixBeta.ha == beta.ha

    # Build Request
    host = u'127.0.0.1'
    port = 6061
    method = u'GET'
    path = u'/fancy?idify=true&jsonify=true'
    headers = dict([('Accept', 'application/json')])
    request = clienting.Requester(hostname=host,
                                  port=port,
                                  method=method,
                                  path=path,
                                  headers=headers)

    msgOut = request.rebuild()
    assert request.lines == [
        b'GET /fancy?idify=true&jsonify=true HTTP/1.1',
        b'Host: 127.0.0.1:6061', b'Accept-Encoding: identity',
        b'Accept: application/json', b'', b''
    ]

    assert msgOut == request.head
    assert request.head == (b'GET /fancy?idify=true&jsonify=true HTTP/1.1\r\n'
                            b'Host: 127.0.0.1:6061\r\nAccept-Encoding: '
                            b'identity\r\nAccept: application/json\r\n\r\n')

    beta.tx(msgOut)
    while beta.txbs and not ixBeta.rxbs:
        beta.serviceSends()
        time.sleep(0.05)
        alpha.serviceReceivesAllIx()
        time.sleep(0.05)
    msgIn = bytes(ixBeta.rxbs)
    assert msgIn == msgOut
    ixBeta.clearRxbs()

    # Build Response
    lines = [
        b'HTTP/1.0 200 OK\r\n',
        b'Content-Type: text/event-stream\r\n',
        b'Cache-Control: no-cache\r\n',
        b'Date: Thu, 30 Apr 2015 22:11:53 GMT\r\n',
        b'Server: IoBook.local\r\n\r\n',
    ]

    msgOut = b''.join(lines)
    ixBeta.tx(msgOut)
    while ixBeta.txbs or not beta.rxbs:
        alpha.serviceSendsAllIx()
        time.sleep(0.05)
        beta.serviceReceives()
        time.sleep(0.05)
    msgIn = bytes(beta.rxbs)
    assert msgIn == msgOut

    lines = [
        b'retry: 1000\n\n',
        b'id: 0\ndata: START\n\n',
        b'id: 1\ndata: {"count":1}\n\n',
        b'id: 2\n',
        b'data: {"count":2}\n\n',
        b'id: 3\ndata: {"count":3}\n\n',
        b'id: 4\ndata: {"count":4}\n\n',
    ]
    msgOut = b''.join(lines)
    response = clienting.Respondent(
        msg=beta.rxbs,
        method=method,
        dictable=True,  # convert event data to dict from json
    )
    ixBeta.tx(msgOut)
    timer = timing.Timer(duration=1.0)
    while response.parser and not timer.expired:
        alpha.serviceSendsAllIx()
        response.parse()
        beta.serviceReceives()
        time.sleep(0.01)

    assert not beta.rxbs
    assert not response.body
    assert not response.eventSource.raw

    if response.parser:
        response.parser.close()
        response.parser = None

    assert response.data is None

    assert response.eventSource.retry == 1000
    assert response.retry == response.eventSource.retry
    assert int(response.eventSource.leid) == 4
    assert response.leid == response.eventSource.leid
    # event data is dict not string since dictable
    assert len(response.events) == 5
    event = response.events.popleft()
    assert event == {'id': '0', 'name': '', 'data': 'START'}
    event = response.events.popleft()
    assert event == {'id': '1', 'name': '', 'data': {'count': 1}}
    event = response.events.popleft()
    assert event == {'id': '2', 'name': '', 'data': {'count': 2}}
    event = response.events.popleft()
    assert event == {'id': '3', 'name': '', 'data': {'count': 3}}
    event = response.events.popleft()
    assert event == {'id': '4', 'name': '', 'data': {'count': 4}}
    assert not response.events

    alpha.close()
    beta.close()
Beispiel #5
0
def test_requester_respondent_sse_stream_fancy():
    """
    Test NonBlocking Http client to SSE server with non trivial path and
    multiline data in reponse events
    """
    alpha = tcp.Server(port=6101, bufsize=131072)
    assert alpha.reopen()
    assert alpha.ha == ('0.0.0.0', 6101)
    assert alpha.eha == ('127.0.0.1', 6101)

    beta = tcp.Client(ha=alpha.eha, bufsize=131072)
    assert beta.reopen()
    assert not beta.accepted
    assert not beta.connected
    assert not beta.cutoff

    # connect beta to alpha
    while True:
        beta.serviceConnect()
        alpha.serviceConnects()
        if beta.connected and beta.ca in alpha.ixes:
            break
        time.sleep(0.05)

    assert beta.accepted
    assert beta.connected
    assert not beta.cutoff
    assert beta.ca == beta.cs.getsockname()
    assert beta.ha == beta.cs.getpeername()
    assert alpha.eha == beta.ha

    ixBeta = alpha.ixes[beta.ca]
    assert ixBeta.ca is not None
    assert ixBeta.cs is not None
    assert ixBeta.cs.getsockname() == beta.cs.getpeername()
    assert ixBeta.cs.getpeername() == beta.cs.getsockname()
    assert ixBeta.ca == beta.ca
    assert ixBeta.ha == beta.ha

    #  build request with fancy path
    host = u'127.0.0.1'
    port = 6061
    method = u'GET'
    path = u'/fancy?idify=true&multiply=true'

    headers = dict([('Accept', 'application/json')])
    request = clienting.Requester(hostname=host,
                                  port=port,
                                  method=method,
                                  path=path,
                                  headers=headers)
    msgOut = request.rebuild()
    assert request.lines == [
        b'GET /fancy?idify=true&multiply=true HTTP/1.1',
        b'Host: 127.0.0.1:6061', b'Accept-Encoding: identity',
        b'Accept: application/json', b'', b''
    ]

    assert request.head == (
        b'GET /fancy?idify=true&multiply=true HTTP/1.1\r\n'
        b'Host: 127.0.0.1:6061\r\nAccept-Encoding: identity\r\n'
        b'Accept: application/json\r\n\r\n')
    assert msgOut == request.head

    # send request
    beta.tx(msgOut)
    while beta.txbs and not ixBeta.rxbs:
        beta.serviceSends()
        time.sleep(0.05)
        alpha.serviceReceivesAllIx()
        time.sleep(0.05)
    msgIn = bytes(ixBeta.rxbs)
    assert msgIn == msgOut
    ixBeta.clearRxbs()

    # Build response
    lines = [
        b'HTTP/1.0 200 OK\r\n',
        b'Server: PasteWSGIServer/0.5 Python/2.7.9\r\n',
        b'Date: Thu, 30 Apr 2015 21:35:25 GMT\r\n'
        b'Content-Type: text/event-stream\r\n',
        b'Cache-Control: no-cache\r\n',
        b'Connection: close\r\n\r\n',
    ]

    msgOut = b''.join(lines)
    ixBeta.tx(msgOut)
    while ixBeta.txbs or not beta.rxbs:
        alpha.serviceSendsAllIx()
        time.sleep(0.05)
        beta.serviceReceives()
        time.sleep(0.05)
    msgIn = bytes(beta.rxbs)
    assert msgIn == msgOut

    # build sse response with multiline data in events
    lines = [
        b'retry: 1000\n\n',
        b'id: 0\ndata: START\n\n',
        b'id: 1\ndata: 1\ndata: 2\n\n',
        b'id: 2\ndata: 3\ndata: 4\n\n',
        b'id: 3\ndata: 5\ndata: 6\n\n',
        b'id: 4\ndata: 7\ndata: 8\n\n',
    ]
    msgOut = b''.join(lines)

    response = clienting.Respondent(msg=beta.rxbs, method=method)

    ixBeta.tx(msgOut)
    timer = timing.Timer(duration=0.5)
    while response.parser and not timer.expired:
        alpha.serviceSendsAllIx()
        response.parse()
        beta.serviceReceives()
        time.sleep(0.01)

    assert not beta.rxbs
    assert not response.body
    assert not response.eventSource.raw

    if response.parser:
        response.parser.close()
        response.parser = None

    response.dictify()
    assert response.data is None

    assert response.eventSource.retry == 1000
    assert response.retry == response.eventSource.retry
    assert int(response.eventSource.leid) == 4
    assert response.leid == response.eventSource.leid
    assert len(response.events) == 5
    event = response.events.popleft()
    assert event == {'id': '0', 'name': '', 'data': 'START'}
    event = response.events.popleft()
    assert event == {'id': '1', 'name': '', 'data': '1\n2'}
    event = response.events.popleft()
    assert event == {'id': '2', 'name': '', 'data': '3\n4'}
    event = response.events.popleft()
    assert event == {'id': '3', 'name': '', 'data': '5\n6'}
    event = response.events.popleft()
    assert event == {'id': '4', 'name': '', 'data': '7\n8'}
    assert not response.events

    alpha.close()
    beta.close()
Beispiel #6
0
def test_requester_respondent_sse_stream_chunked():
    """
    Test NonBlocking Http client with SSE streaming server with transfer encoding
    (chunked)
    """
    alpha = tcp.Server(port=6101, bufsize=131072)
    assert alpha.reopen()
    assert alpha.ha == ('0.0.0.0', 6101)
    assert alpha.eha == ('127.0.0.1', 6101)

    beta = tcp.Client(ha=alpha.eha, bufsize=131072)
    assert beta.reopen()
    assert not beta.accepted
    assert not beta.connected
    assert not beta.cutoff

    # connect beta to alpha
    while True:
        beta.serviceConnect()
        alpha.serviceConnects()
        if beta.connected and beta.ca in alpha.ixes:
            break
        time.sleep(0.05)

    assert beta.accepted
    assert beta.connected
    assert not beta.cutoff
    assert beta.ca == beta.cs.getsockname()
    assert beta.ha == beta.cs.getpeername()
    assert alpha.eha == beta.ha

    ixBeta = alpha.ixes[beta.ca]
    assert ixBeta.ca is not None
    assert ixBeta.cs is not None
    assert ixBeta.cs.getsockname() == beta.cs.getpeername()
    assert ixBeta.cs.getpeername() == beta.cs.getsockname()
    assert ixBeta.ca == beta.ca
    assert ixBeta.ha == beta.ha

    # build request
    host = u'127.0.0.1'
    port = 6061
    method = u'GET'
    path = u'/stream'
    headers = dict([('Accept', 'application/json')])
    request = clienting.Requester(hostname=host,
                                  port=port,
                                  method=method,
                                  path=path,
                                  headers=headers)
    msgOut = request.rebuild()
    assert request.lines == [
        b'GET /stream HTTP/1.1', b'Host: 127.0.0.1:6061',
        b'Accept-Encoding: identity', b'Accept: application/json', b'', b''
    ]

    assert request.head == b'GET /stream HTTP/1.1\r\nHost: 127.0.0.1:6061\r\nAccept-Encoding: identity\r\nAccept: application/json\r\n\r\n'
    assert msgOut == request.head

    #  send Beta request to Alpha
    beta.tx(msgOut)
    while beta.txbs and not ixBeta.rxbs:
        beta.serviceSends()
        time.sleep(0.05)
        alpha.serviceReceivesAllIx()
        time.sleep(0.05)
    msgIn = bytes(ixBeta.rxbs)
    assert msgIn == msgOut
    ixBeta.clearRxbs()

    # build response header Alpha to Beta with chunk header
    lines = [
        b'HTTP/1.1 200 OK\r\n',
        b'Content-Type: text/event-stream\r\n',
        b'Cache-Control: no-cache\r\n',
        b'Transfer-Encoding: chunked\r\n',
        b'Date: Thu, 30 Apr 2015 20:11:35 GMT\r\n',
        b'Server: IoBook.local\r\n\r\n',
    ]

    msgOut = b''.join(lines)
    ixBeta.tx(msgOut)
    while ixBeta.txbs or not beta.rxbs:
        alpha.serviceSendsAllIx()
        time.sleep(0.05)
        beta.serviceReceives()
        time.sleep(0.05)
    msgIn = bytes(beta.rxbs)
    assert msgIn == msgOut

    # build response body
    lines = [
        b'd\r\nretry: 1000\n\n\r\n',
        b'd\r\ndata: START\n\n\r\n',
        b'9\r\ndata: 1\n\n\r\n',
        b'9\r\ndata: 2\n\n\r\n',
        b'9\r\ndata: 3\n\n\r\n',
        b'9\r\ndata: 4\n\n\r\n',
    ]
    msgOut = b''.join(lines)
    ixBeta.tx(msgOut)

    #  create Respondent to process response
    response = clienting.Respondent(msg=beta.rxbs, method=method)

    timer = timing.Timer(duration=0.5)
    while response.parser and not timer.expired:
        alpha.serviceSendsAllIx()
        response.parse()
        beta.serviceReceives()
        time.sleep(0.01)

    assert not beta.rxbs  #  empty
    assert not response.body  # empty
    assert not response.eventSource.raw  # empty

    if response.parser:
        response.parser.close()
        response.parser = None

    response.dictify()  # .data is None
    assert response.data is None

    assert response.eventSource.retry == 1000
    assert response.retry == response.eventSource.retry
    assert response.eventSource.leid is None
    assert response.leid == response.eventSource.leid
    assert len(response.events) == 5
    event = response.events.popleft()
    assert event == {'id': None, 'name': '', 'data': 'START'}
    event = response.events.popleft()
    assert event == {'id': None, 'name': '', 'data': '1'}
    event = response.events.popleft()
    assert event == {'id': None, 'name': '', 'data': '2'}
    event = response.events.popleft()
    assert event == {'id': None, 'name': '', 'data': '3'}
    event = response.events.popleft()
    assert event == {'id': None, 'name': '', 'data': '4'}
    assert not response.events

    alpha.close()
    beta.close()