File size: 4,474 Bytes
0936134 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | # -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division, unicode_literals
##
## This file is part of DaBroker, a distributed data access manager.
##
## DaBroker is Copyright © 2014 by Matthias Urlichs <matthias@urlichs.de>,
## it is licensed under the GPLv3. See the file `README.rst` for details,
## including optimistic statements by the author.
##
## This paragraph is auto-generated and may self-destruct at any time,
## courtesy of "make update". The original is in ‘utils/_boilerplate.py’.
## Thus, please do not remove the next line, or insert any blank lines.
##BP
import pytest
import pytest_asyncio.plugin
import os
import asyncio
from dabroker.proto import ProtocolClient,ProtocolInteraction
from dabroker.proto.lines import LineProtocol
from dabroker.util.tests import load_cfg
import unittest
from unittest.mock import Mock
D=0.2
class EchoServerClientProtocol(asyncio.Protocol):
def __init__(self,loop):
self._loop = loop
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
print('Connection from:',peername)
self.transport = transport
def data_received(self, data):
message = data.decode()
print("Message:",message)
self._loop.call_later(3*D, self._echo,data)
def _echo(self,data):
self.transport.write(data)
def connection_lost(self, exc):
print("Conn closed:",exc)
class EchoClientProtocol(asyncio.Protocol):
def __init__(self, future):
self.future = future
self.done = False
def connection_made(self, transport):
self.transport = transport
transport.write(b"Hello!\n");
def data_received(self, data):
assert data == b"Hello!\n"
self.done = True
self.transport.close()
def connection_lost(self, exc):
self.future.set_result(self.done)
@pytest.yield_fixture
def echoserver(loop, unused_tcp_port):
# Each client connection will create a new protocol instance
coro = loop.create_server(lambda: EchoServerClientProtocol(loop), '127.0.0.1', unused_tcp_port)
server = loop.run_until_complete(coro)
server.port = unused_tcp_port
yield server
server.close()
loop.run_until_complete(server.wait_closed())
@pytest.mark.run_loop
async def test_echo(loop, echoserver):
f = asyncio.Future(loop=loop)
coro = loop.create_connection(lambda: EchoClientProtocol(f),
'127.0.0.1', echoserver.port)
await coro
await f
assert f.result() is True
class LinesTester(ProtocolInteraction):
def __init__(self,conn_store=None,**k):
self.conn_store = conn_store
super().__init__(**k)
async def interact(self):
if self.conn_store is not None:
self.conn_store.append(self._protocol)
self.send("whatever\nwherever")
await asyncio.sleep(D/2, loop=self._loop)
self.send("whenever")
m = await self.recv()
n = await self.recv()
o = await self.recv()
assert m == "whatever"
assert n == "wherever"
assert o == "whenever"
@pytest.mark.run_loop
async def test_lines(echoserver, loop):
"""Use the "lines" protocol to go through the basic protocol features"""
c = ProtocolClient(LineProtocol, "127.0.0.1",echoserver.port, loop=loop)
await c.run(LinesTester(loop=loop))
assert len(c.conns) == 1
fff=[]
tp = LinesTester(fff,loop=loop,conn=c)
e = asyncio.ensure_future(tp.run(), loop=loop)
# give 'e' time to start up
await asyncio.sleep(D/3, loop=loop)
# make sure close() waits for 'e' and doesn't break it
await c.close()
assert e.done()
e.result()
assert len(c.conns) == 0
# create an idle connection which the next step can re-use
eee=[]
await c.run(LinesTester(eee, loop=loop))
assert len(c.conns) == 1
# now do two at the same time, and abort
fff.pop()
ff = c.run(tp)
f = asyncio.ensure_future(ff, loop=loop)
await asyncio.sleep(D, loop=loop)
with pytest.raises(RuntimeError):
await c.run(tp)
g = asyncio.ensure_future(c.run(LinesTester(loop=loop)), loop=loop)
await asyncio.sleep(D, loop=loop)
await f
# check that the connection is reused
assert len(eee) == 1
assert len(fff) == 1
assert eee[0] == fff[0]
c.MAX_IDLE = 0
hhh = []
hh = c.run(LinesTester(hhh, loop=loop))
h = asyncio.ensure_future(hh, loop=loop)
await asyncio.sleep(D/3, loop=loop)
assert len(hhh) == 1
assert eee[0] != hhh[0]
c.abort()
assert len(c.conns) == 0
with pytest.raises(asyncio.CancelledError):
await g
with pytest.raises(asyncio.CancelledError):
g.result()
with pytest.raises(asyncio.CancelledError):
await h
# let the mainloop process things
await asyncio.sleep(D/2, loop=loop)
|