Repository: magic-wormhole/magic-wormhole (language: python)

Issue:
A bunch of tests fail with magic-wormhole-transit-relay 0.3.1
while bumping magic-wormhole-transit-relay for chimera linux, i got a bunch of tests failing:
https://gist.github.com/triallax/efa7da4248ab6017dedbb7a56d2282ba

would be nice if we can get those fixed :)


Discussion / hints:
Could you please file this against https://github.com/magic-wormhole/magic-wormhole-transit-relay/ instead, and say some more about the setup if it seems relevant? (I can move if you want, but then I'll "own" the ticket).

At a first glance, it seems like these could be related to python 3.12 ...?
i don't mind refiling it there, but is that really correct? these test failures are from magic-wormhole's test suite, i mentioned the transit relay thing specifically because these failures only occur after the bump, everything passes fine with magic-wormhole-test-relay 0.2.1 (in both cases running on python 3.12)
Ahh, sorry I guess my glance was _too_ brief, I didn't grok that it's _this_ test-suite failing. thanks
no worries, i did make the issue body unnecessarily terse and unclear (i didn't explicitly state it was magic-wormhole's test suite even)
Cannot repeat with Python 3.11 and magic-wormhole-transit-relay 0.3.1

(Will try with Python 3.12)
Hmm, works for me with 3.12.1 as well:

```
PASSED (successes=456)
  py312: OK (67.27=setup[21.19]+cmd[0.88,0.65,44.55] seconds)
  congratulations :) (68.63 seconds)
```
Arg, my bad. Tox had pinned an older version of magic-wormhole-transit relay.

I have repeated with 3.12.7 and 0.3.1. Thanks for the report!

Fix patch:
diff --git a/NEWS.md b/NEWS.md
index 31c87f94..87019419 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -2,6 +2,7 @@ User-visible changes in "magic-wormhole":
 
 ## Upcoming Release
 
+* Bump magic-wormhole-transit-relay to 0.3.1 for testing, and fix (#543)
 * (add release-notes here when making PRs)
 
 
diff --git a/setup.py b/setup.py
index 3361ae5c..38d67414 100644
--- a/setup.py
+++ b/setup.py
@@ -60,7 +60,7 @@
       extras_require={
           ':sys_platform=="win32"': ["pywin32"],
           "dev": ["tox", "pyflakes",
-                  "magic-wormhole-transit-relay==0.1.2",
+                  "magic-wormhole-transit-relay==0.3.1",
                   "magic-wormhole-mailbox-server==0.3.1"],
           "dilate": ["noiseprotocol"],
           "build": ["twine", "dulwich", "readme_renderer", "gpg", "wheel"],
diff --git a/tox.ini b/tox.ini
index 7a6a0579..ea27f3ea 100644
--- a/tox.ini
+++ b/tox.ini
@@ -4,8 +4,8 @@
 # and then run "tox" from this directory.
 
 [tox]
-# useful envs: py37, py38, pypy, flake8
-envlist = {py37,py38}
+# useful envs: py311, py312, pypy, flake8
+envlist = {py38,py39,py310,py311,py312}
 skip_missing_interpreters = True
 minversion = 2.4.0
 


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 4e7e5e58..820e57e1 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -11,7 +11,7 @@ jobs:
     runs-on: ubuntu-latest
     strategy:
       matrix:
-        python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
+        python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
 
     steps:
     - uses: actions/checkout@v2
diff --git a/src/wormhole/test/common.py b/src/wormhole/test/common.py
index c578da16..77899f12 100644
--- a/src/wormhole/test/common.py
+++ b/src/wormhole/test/common.py
@@ -1,14 +1,15 @@
 # no unicode_literals until twisted update
 from click.testing import CliRunner
 from twisted.application import internet, service
-from twisted.internet import defer, endpoints, reactor, task
+from twisted.internet import defer, endpoints, reactor, task, protocol
 from twisted.python import log
 
 from unittest import mock
 from wormhole_mailbox_server.database import create_channel_db, create_usage_db
 from wormhole_mailbox_server.server import make_server
 from wormhole_mailbox_server.web import make_web_server
-from wormhole_transit_relay.transit_server import Transit
+from wormhole_transit_relay.transit_server import Transit, TransitConnection
+from wormhole_transit_relay.usage import create_usage_tracker
 
 from ..cli import cli
 from ..transit import allocate_tcp_port
@@ -77,9 +78,15 @@ def _setup_relay(self, error, advertise_version=None):
         self.transitport = allocate_tcp_port()
         ep = endpoints.serverFromString(
             reactor, "tcp:%d:interface=127.0.0.1" % self.transitport)
-        self._transit_server = f = Transit(
-            blur_usage=None, log_file=None, usage_db=None)
-        internet.StreamServerEndpointService(ep, f).setServiceParent(self.sp)
+
+        usage = create_usage_tracker(blur_usage=None, log_file=None, usage_db=None)
+        self._transit_server = protocol.ServerFactory()
+        self._transit_server.protocol = TransitConnection
+        self._transit_server.log_requests = False
+        self._transit_server.transit = Transit(usage, reactor.seconds)
+
+
+        internet.StreamServerEndpointService(ep, self._transit_server).setServiceParent(self.sp)
         self.transit = u"tcp:127.0.0.1:%d" % self.transitport
 
     @defer.inlineCallbacks
@@ -102,6 +109,7 @@ def tearDown(self):
             defer.returnValue(None)
         # disconnect all callers
         d = defer.maybeDeferred(self.sp.stopService)
+        d.addBoth(lambda _: self._transit_server.stopFactory())
         # wait a second, then check to see if it worked
         yield task.deferLater(reactor, 1.0, lambda: None)
         if len(tp.working):
diff --git a/src/wormhole/test/test_transit.py b/src/wormhole/test/test_transit.py
index b4331bdd..0ec391b5 100644
--- a/src/wormhole/test/test_transit.py
+++ b/src/wormhole/test/test_transit.py
@@ -1526,11 +1526,16 @@ def test_old(self):
         tc = transit_server.TransitConnection()
         tc.factory = mock.Mock()
         tc.factory.connection_got_token = mock.Mock()
+        tc.transport = mock.Mock()
+        tc.connectionMade()
+        tc._state.please_relay = mock.Mock()
+        tc._state.please_relay_for_side = mock.Mock()
         tc.dataReceived(old_handshake[:-1])
-        self.assertEqual(tc.factory.connection_got_token.mock_calls, [])
+        self.assertEqual(tc._state.please_relay.mock_calls, [])
         tc.dataReceived(old_handshake[-1:])
-        self.assertEqual(tc.factory.connection_got_token.mock_calls,
-                         [mock.call(hexlify(token), None, tc)])
+        self.assertEqual(tc._state.please_relay.mock_calls,
+                         [mock.call(hexlify(token))])
+        self.assertEqual(tc._state.please_relay_for_side.mock_calls, [])
 
     def test_new(self):
         c = transit.Common(None)
@@ -1540,13 +1545,15 @@ def test_new(self):
 
         tc = transit_server.TransitConnection()
         tc.factory = mock.Mock()
-        tc.factory.connection_got_token = mock.Mock()
+        tc.transport = mock.Mock()
+        tc.connectionMade()
+        tc._state._real_register_token_for_side = m = mock.Mock()
         tc.dataReceived(new_handshake[:-1])
-        self.assertEqual(tc.factory.connection_got_token.mock_calls, [])
+        self.assertEqual(m.mock_calls, [])
         tc.dataReceived(new_handshake[-1:])
         self.assertEqual(
-            tc.factory.connection_got_token.mock_calls,
-            [mock.call(hexlify(token), c._side.encode("ascii"), tc)])
+            m.mock_calls,
+            [mock.call(hexlify(token), c._side.encode("ascii"))])
 
 
 class Full(ServerBase, unittest.TestCase):