| 1 |
import pytest |
| 2 |
import socket |
| 3 |
import asyncio |
| 4 |
import contextlib |
| 5 |
from panoramisk import Manager |
| 6 |
from panoramisk import utils |
| 7 |
from panoramisk.actions import Action |
| 8 |
|
| 9 |
|
| 10 |
def unused_tcp_port_factory(): |
| 11 |
"""Find an unused localhost TCP port from 1024-65535 and return it.""" |
| 12 |
with contextlib.closing(socket.socket()) as sock: |
| 13 |
sock.bind(('127.0.0.1', 0)) |
| 14 |
return sock.getsockname()[1] |
| 15 |
|
| 16 |
|
| 17 |
def message(msg): |
| 18 |
EOL = utils.EOL.encode('utf8') |
| 19 |
return EOL.join([l.strip() for l in msg.strip().split(b'\n')]) + EOL + EOL |
| 20 |
|
| 21 |
|
| 22 |
def login(action_id): |
| 23 |
return message(b''' |
| 24 |
Response: Success |
| 25 |
ActionID: ''' + action_id.encode('utf8') + b''' |
| 26 |
Message: Authentication accepted |
| 27 |
''') |
| 28 |
|
| 29 |
|
| 30 |
def resp(action_id): |
| 31 |
return message(b''' |
| 32 |
Response: Success |
| 33 |
ActionID: ''' + action_id.encode('utf8') + b''' |
| 34 |
Message: Resp |
| 35 |
''') |
| 36 |
|
| 37 |
|
| 38 |
def pong(action_id): |
| 39 |
return message(b''' |
| 40 |
Response: Success |
| 41 |
ActionID: ''' + action_id.encode('utf8') + b''' |
| 42 |
Ping: Pong |
| 43 |
Timestamp: 1409169929.412068 |
| 44 |
''') |
| 45 |
|
| 46 |
|
| 47 |
class Event: |
| 48 |
|
| 49 |
def __init__(self, event): |
| 50 |
self.event = event |
| 51 |
|
| 52 |
|
| 53 |
class _Asterisk(asyncio.Protocol): |
| 54 |
|
| 55 |
def connection_made(self, transport): |
| 56 |
self.uuid = transport.get_extra_info('peername') |
| 57 |
self.transport = transport |
| 58 |
self.factory.clients[self.uuid] = self |
| 59 |
if not self.factory.started.done(): |
| 60 |
self.factory.started.set_result(True) |
| 61 |
|
| 62 |
def data_received(self, data): |
| 63 |
data = data.decode('utf8') |
| 64 |
data = data.split(utils.EOL + utils.EOL) |
| 65 |
if data: |
| 66 |
if not self.factory._data_received.done(): |
| 67 |
self.factory._data_received.set_result(True) |
| 68 |
for line in data: |
| 69 |
if not line.strip(): |
| 70 |
continue |
| 71 |
a = Action() |
| 72 |
for kv in line.split('\n'): |
| 73 |
if ':' in kv: |
| 74 |
k, v = kv.split(':', 1) |
| 75 |
a[k] = v.strip() |
| 76 |
self.factory.actions.append(a) |
| 77 |
print(repr(a)) |
| 78 |
if 'action' in a: |
| 79 |
if a['action'] == 'Login': |
| 80 |
message = login(a['actionid']) |
| 81 |
elif a['action'] == 'Ping': |
| 82 |
message = pong(a['actionid']) |
| 83 |
else: |
| 84 |
message = resp(a['actionid']) |
| 85 |
print(repr(message)) |
| 86 |
self.transport.write(message) |
| 87 |
|
| 88 |
|
| 89 |
class Asterisk(object): |
| 90 |
|
| 91 |
clients = {} |
| 92 |
|
| 93 |
def __init__(self, loop, port): |
| 94 |
self.loop = loop |
| 95 |
self.port = port |
| 96 |
self.protocol = None |
| 97 |
|
| 98 |
@property |
| 99 |
def client(self): |
| 100 |
return list(self.clients.values())[0] |
| 101 |
|
| 102 |
async def start(self): |
| 103 |
self.actions = [] |
| 104 |
self.clients = {} |
| 105 |
self.started = asyncio.Future() |
| 106 |
self._data_received = asyncio.Future() |
| 107 |
self.protocol = _Asterisk() |
| 108 |
self.protocol.factory = self |
| 109 |
await self.loop.create_server( |
| 110 |
lambda: self.protocol, '127.0.0.1', self.port) |
| 111 |
return self.started |
| 112 |
|
| 113 |
async def data_received(self): |
| 114 |
self._data_received = asyncio.Future() |
| 115 |
await self._data_received |
| 116 |
return self._data_received |
| 117 |
|
| 118 |
async def stop(self): |
| 119 |
for client in self.clients.values(): |
| 120 |
client.transport.close() |
| 121 |
|
| 122 |
|
| 123 |
@pytest.mark.asyncio |
| 124 |
async def test_reconnection_without_lost(event_loop): |
| 125 |
unused_tcp_port = unused_tcp_port_factory() |
| 126 |
|
| 127 |
print('Start server on %s' % unused_tcp_port) |
| 128 |
manager = Manager(loop=event_loop, |
| 129 |
host='127.0.0.1', |
| 130 |
port=unused_tcp_port, |
| 131 |
username='user', |
| 132 |
secret='secret') |
| 133 |
server = Asterisk(event_loop, unused_tcp_port) |
| 134 |
|
| 135 |
await server.start() |
| 136 |
await manager.connect() |
| 137 |
await server.data_received() |
| 138 |
print(server.actions) |
| 139 |
login = server.actions[0] |
| 140 |
assert login['action'] == 'Login' |
| 141 |
unused_tcp_port = unused_tcp_port_factory() |
| 142 |
|
| 143 |
print('Restart server on %s' % unused_tcp_port) |
| 144 |
server.port = unused_tcp_port |
| 145 |
manager.config['port'] = unused_tcp_port |
| 146 |
await server.stop() |
| 147 |
|
| 148 |
manager.send_action({'Action': 'Ping'}) |
| 149 |
f = manager.send_action({'Action': 'Test', 'Command': 'test'}) |
| 150 |
fully_booted = Event('FullyBooted') |
| 151 |
|
| 152 |
await asyncio.sleep(.1) |
| 153 |
assert manager.awaiting_actions |
| 154 |
await server.start() |
| 155 |
await asyncio.sleep(2) |
| 156 |
assert manager.awaiting_actions |
| 157 |
manager.dispatch(fully_booted) |
| 158 |
await asyncio.sleep(.5) |
| 159 |
assert not manager.awaiting_actions |
| 160 |
resp = await f |
| 161 |
await server.stop() |
| 162 |
|
| 163 |
login2 = server.actions[0] |
| 164 |
assert login2['action'] == 'Login' |
| 165 |
assert login['actionid'] != login2['actionid'] |
| 166 |
|
| 167 |
test_action = server.actions[-1] |
| 168 |
assert test_action.id == resp.id |
| 169 |
assert test_action['action'] == 'Test' |