| 1 |
from io import ( |
| 2 |
StringIO, |
| 3 |
) |
| 4 |
import sys |
| 5 |
import shutil |
| 6 |
|
| 7 |
from twisted.trial import unittest |
| 8 |
from twisted.internet.interfaces import ( |
| 9 |
IPullProducer, |
| 10 |
) |
| 11 |
from twisted.internet.protocol import ( |
| 12 |
ProcessProtocol, |
| 13 |
) |
| 14 |
from twisted.internet.defer import ( |
| 15 |
inlineCallbacks, |
| 16 |
Deferred, |
| 17 |
) |
| 18 |
from autobahn.twisted.websocket import ( |
| 19 |
WebSocketClientProtocol, |
| 20 |
create_client_agent, |
| 21 |
) |
| 22 |
from zope.interface import implementer |
| 23 |
|
| 24 |
|
| 25 |
class _CollectOutputProtocol(ProcessProtocol): |
| 26 |
""" |
| 27 |
Internal helper. Collects all output (stdout + stderr) into |
| 28 |
self.output, and callback's on done with all of it after the |
| 29 |
process exits (for any reason). |
| 30 |
""" |
| 31 |
def __init__(self): |
| 32 |
self.done = Deferred() |
| 33 |
self.running = Deferred() |
| 34 |
self.output = StringIO() |
| 35 |
|
| 36 |
def processEnded(self, reason): |
| 37 |
if not self.done.called: |
| 38 |
self.done.callback(self.output.getvalue()) |
| 39 |
|
| 40 |
def outReceived(self, data): |
| 41 |
print(data.decode(), end="", flush=True) |
| 42 |
self.output.write(data.decode(sys.getfilesystemencoding())) |
| 43 |
if not self.running.called: |
| 44 |
if "on 8088" in self.output.getvalue(): |
| 45 |
self.running.callback(None) |
| 46 |
|
| 47 |
def errReceived(self, data): |
| 48 |
print("ERR: {}".format(data.decode(sys.getfilesystemencoding()))) |
| 49 |
self.output.write(data.decode(sys.getfilesystemencoding())) |
| 50 |
|
| 51 |
|
| 52 |
def run_transit(reactor, proto, tcp_port=None, websocket_port=None): |
| 53 |
exe = shutil.which("twistd") |
| 54 |
args = [ |
| 55 |
exe, "-n", "transitrelay", |
| 56 |
] |
| 57 |
if tcp_port is not None: |
| 58 |
args.append("--port") |
| 59 |
args.append(tcp_port) |
| 60 |
if websocket_port is not None: |
| 61 |
args.append("--websocket") |
| 62 |
args.append(websocket_port) |
| 63 |
proc = reactor.spawnProcess(proto, exe, args) |
| 64 |
return proc |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
class Sender(WebSocketClientProtocol): |
| 69 |
""" |
| 70 |
""" |
| 71 |
|
| 72 |
def __init__(self, *args, **kw): |
| 73 |
WebSocketClientProtocol.__init__(self, *args, **kw) |
| 74 |
self.done = Deferred() |
| 75 |
self.got_ok = Deferred() |
| 76 |
|
| 77 |
def onMessage(self, payload, is_binary): |
| 78 |
print("onMessage") |
| 79 |
if not self.got_ok.called: |
| 80 |
if payload == b"ok\n": |
| 81 |
self.got_ok.callback(None) |
| 82 |
print("send: {}".format(payload.decode("utf8"))) |
| 83 |
|
| 84 |
def onClose(self, clean, code, reason): |
| 85 |
print(f"close: {clean} {code} {reason}") |
| 86 |
self.done.callback(None) |
| 87 |
|
| 88 |
|
| 89 |
class Receiver(WebSocketClientProtocol): |
| 90 |
""" |
| 91 |
""" |
| 92 |
|
| 93 |
def __init__(self, *args, **kw): |
| 94 |
WebSocketClientProtocol.__init__(self, *args, **kw) |
| 95 |
self.done = Deferred() |
| 96 |
self.first_message = Deferred() |
| 97 |
self.received = 0 |
| 98 |
|
| 99 |
def onMessage(self, payload, is_binary): |
| 100 |
print("recv: {}".format(len(payload))) |
| 101 |
self.received += len(payload) |
| 102 |
if not self.first_message.called: |
| 103 |
self.first_message.callback(None) |
| 104 |
|
| 105 |
def onClose(self, clean, code, reason): |
| 106 |
print(f"close: {clean} {code} {reason}") |
| 107 |
self.done.callback(None) |
| 108 |
|
| 109 |
|
| 110 |
class TransitWebSockets(unittest.TestCase): |
| 111 |
""" |
| 112 |
Integration-style tests of the transit WebSocket relay, using the |
| 113 |
real reactor (and running transit as a subprocess). |
| 114 |
""" |
| 115 |
|
| 116 |
@inlineCallbacks |
| 117 |
def test_buffer_fills(self): |
| •118 |
return |
| 119 |
""" |
| 120 |
A running transit relay stops accepting incoming data at a |
| 121 |
reasonable amount if the peer isn't reading. This test defines |
| 122 |
that as 'less than 100MiB' although in practice Twisted seems |
| 123 |
to stop before 10MiB. |
| 124 |
""" |
| 125 |
from twisted.internet import reactor |
| 126 |
transit_proto = _CollectOutputProtocol() |
| 127 |
transit_proc = run_transit(reactor, transit_proto, websocket_port="tcp:8088") |
| 128 |
|
| 129 |
def cleanup_process(): |
| 130 |
transit_proc.signalProcess("HUP") |
| 131 |
return transit_proto.done |
| 132 |
self.addCleanup(cleanup_process) |
| 133 |
|
| 134 |
yield transit_proto.running |
| 135 |
print("Transit running") |
| 136 |
|
| 137 |
agent = create_client_agent(reactor) |
| 138 |
side_a = yield agent.open("ws://localhost:8088", {}, lambda: Sender()) |
| 139 |
side_b = yield agent.open("ws://localhost:8088", {}, lambda: Receiver()) |
| 140 |
|
| 141 |
side_a.sendMessage(b"please relay aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa for side aaaaaaaaaaaaaaaa", True) |
| 142 |
side_b.sendMessage(b"please relay aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa for side bbbbbbbbbbbbbbbb", True) |
| 143 |
|
| 144 |
yield side_a.got_ok |
| 145 |
yield side_b.first_message |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
reactor.removeReader(side_b.transport) |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
max_data = 1024*1024*100 |
| 155 |
|
| 156 |
@implementer(IPullProducer) |
| 157 |
class ProduceMessages: |
| 158 |
def __init__(self, ws, on_produce): |
| 159 |
self._ws = ws |
| 160 |
self._sent = 0 |
| 161 |
self._max = max_data |
| 162 |
self._on_produce = on_produce |
| 163 |
|
| 164 |
def resumeProducing(self): |
| 165 |
self._on_produce() |
| 166 |
if self._sent >= self._max: |
| 167 |
self._ws.sendClose() |
| 168 |
return |
| 169 |
data = b"a" * 1024*1024 |
| 170 |
self._ws.sendMessage(data, True) |
| 171 |
self._sent += len(data) |
| 172 |
print("sent {}, total {}".format(len(data), self._sent)) |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
done = Deferred() |
| 180 |
last_produce = None |
| 181 |
timeout = 2 |
| 182 |
|
| 183 |
def asked_for_data(): |
| 184 |
nonlocal last_produce |
| 185 |
last_produce = reactor.seconds() |
| 186 |
|
| 187 |
data = ProduceMessages(side_a, asked_for_data) |
| 188 |
side_a.transport.registerProducer(data, False) |
| 189 |
data.resumeProducing() |
| 190 |
|
| 191 |
def check_if_done(): |
| 192 |
if last_produce is not None: |
| 193 |
if reactor.seconds() - last_produce > timeout: |
| 194 |
done.callback(None) |
| 195 |
return |
| 196 |
|
| 197 |
reactor.callLater(.1, check_if_done) |
| 198 |
check_if_done() |
| 199 |
|
| 200 |
yield done |
| 201 |
|
| 202 |
mib = 1024*1024.0 |
| 203 |
print("Sent {}MiB of {}MiB before backpressure".format(data._sent / mib, max_data / mib)) |
| 204 |
self.assertTrue(data._sent < max_data, "Too much data sent") |
| 205 |
|
| 206 |
side_a.sendClose() |
| 207 |
side_b.sendClose() |
| 208 |
yield side_a.done |
| 209 |
yield side_b.done |