test server-reconnection: it worked already

Cool, it actually worked without changes. Closes #42
This commit is contained in:
Brian Warner 2017-04-07 09:48:48 -07:00
parent 3d89d78ea5
commit 781d791e03
2 changed files with 52 additions and 0 deletions

View File

@ -77,6 +77,7 @@ class RendezvousConnector(object):
ep = self._make_endpoint(p.hostname, p.port or 80)
# TODO: change/wrap ClientService to fail if the first attempt fails
self._connector = internet.ClientService(ep, f)
self._debug_record_inbound_f = None
def set_trace(self, f):
self._trace = f
@ -120,6 +121,8 @@ class RendezvousConnector(object):
self._tx("close", mailbox=mailbox, mood=mood)
def stop(self):
# ClientService.stopService is defined to "Stop attempting to
# reconnect and close any existing connections"
d = defer.maybeDeferred(self._connector.stopService)
d.addErrback(log.err) # TODO: deliver error upstairs?
d.addBoth(self._stopped)
@ -157,6 +160,8 @@ class RendezvousConnector(object):
))
self._timing.add("ws_receive", _side=self._side, message=msg)
if self._debug_record_inbound_f:
self._debug_record_inbound_f(msg)
mtype = msg["type"]
meth = getattr(self, "_response_handle_"+mtype, None)
if not meth:

View File

@ -493,3 +493,50 @@ class Errors(ServerBase, unittest.TestCase):
yield w.when_code()
self.assertRaises(OnlyOneCodeError, w.set_code, "123-nope")
yield self.assertFailure(w.close(), LonelyError)
class Reconnection(ServerBase, unittest.TestCase):
@inlineCallbacks
def test_basic(self):
w1 = wormhole.create(APPID, self.relayurl, reactor)
w1_in = []
w1._boss._RC._debug_record_inbound_f = w1_in.append
#w1.debug_set_trace("W1")
w1.allocate_code()
code = yield w1.when_code()
w1.send(b"data1") # will be queued until wormhole is established
# now wait until we've deposited all our messages on the server
def seen_our_pake():
for m in w1_in:
if m["type"] == "message" and m["phase"] == "pake":
return True
return False
yield poll_until(seen_our_pake)
w1_in[:] = []
# drop the connection
w1._boss._RC._ws.transport.loseConnection()
# wait for it to reconnect and redeliver all the messages. The server
# sends mtype=message messages in random order, but we've only sent
# one of them, so it's safe to wait for just the PAKE phase.
yield poll_until(seen_our_pake)
# now let the second side proceed. this simulates the most common
# case: the server is bounced while the sender is waiting, before the
# receiver has started
w2 = wormhole.create(APPID, self.relayurl, reactor)
#w2.debug_set_trace(" W2")
w2.set_code(code)
dataY = yield w2.when_received()
self.assertEqual(dataY, b"data1")
w2.send(b"data2")
dataX = yield w1.when_received()
self.assertEqual(dataX, b"data2")
c1 = yield w1.close()
self.assertEqual(c1, "happy")
c2 = yield w2.close()
self.assertEqual(c2, "happy")