From 606b6d7d08fcc5515b70ecb20f394ea3dbbf2bc1 Mon Sep 17 00:00:00 2001
From: Lee Miller <lee.miller@tutanota.com>
Date: Wed, 20 Dec 2023 03:02:42 +0200
Subject: [PATCH 1/2] Explicitly enable UDP in TestNetwork adding Announcer to
 the expected threads

---
 src/tests/test_network.py | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/tests/test_network.py b/src/tests/test_network.py
index e6a05717..cbdcb903 100644
--- a/src/tests/test_network.py
+++ b/src/tests/test_network.py
@@ -19,6 +19,7 @@ class TestNetwork(TestPartialRun):
         cls.state.maximumNumberOfHalfOpenConnections = 4
 
         cls.config.set('bitmessagesettings', 'sendoutgoingconnections', 'True')
+        cls.config.set('bitmessagesettings', 'udp', 'True')
 
         # config variable is still used inside of the network ):
         import network
@@ -34,18 +35,14 @@ class TestNetwork(TestPartialRun):
     def test_threads(self):
         """Ensure all the network threads started"""
         threads = {
-            "AddrBroadcaster", "Asyncore", "Downloader", "InvBroadcaster",
-            "Uploader"}
-        extra = (
-            self.config.getint('threads', 'receive')
-            + self.config.safeGetBoolean('bitmessagesettings', 'udp'))
+            "AddrBroadcaster", "Announcer", "Asyncore", "Downloader",
+            "InvBroadcaster", "Uploader"}
+        extra = self.config.getint('threads', 'receive')
         for thread in threading.enumerate():
             try:
                 threads.remove(thread.name)
             except KeyError:
-                extra -= (
-                    thread.name == "Announcer"
-                    or thread.name.startswith("ReceiveQueue_"))
+                extra -= thread.name.startswith("ReceiveQueue_")
 
         self.assertEqual(len(threads), 0)
         self.assertEqual(extra, 0)
-- 
2.45.1


From c5ced60d5de67571bc529956ccd76a78e59ff665 Mon Sep 17 00:00:00 2001
From: Lee Miller <lee.miller@tutanota.com>
Date: Wed, 20 Dec 2023 04:26:55 +0200
Subject: [PATCH 2/2] Move most of the logic of TestCore.test_udp() into the
 TestNetwork

---
 src/tests/core.py         | 20 --------------------
 src/tests/test_network.py | 25 +++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/src/tests/core.py b/src/tests/core.py
index a7247971..204c2cff 100644
--- a/src/tests/core.py
+++ b/src/tests/core.py
@@ -310,26 +310,6 @@ class TestCore(unittest.TestCase):
         else:
             return self.fail('No Announcer thread found')
 
-        for _ in range(20):  # wait for UDP socket
-            for sock in BMConnectionPool().udpSockets.values():
-                thread.announceSelf()
-                break
-            else:
-                time.sleep(1)
-                continue
-            break
-        else:
-            self.fail('UDP socket is not started')
-
-        for _ in range(20):
-            if state.discoveredPeers:
-                peer = state.discoveredPeers.keys()[0]
-                self.assertEqual(peer.port, 8444)
-                break
-            time.sleep(1)
-        else:
-            self.fail('No self in discovered peers')
-
     @staticmethod
     def _decode_msg(data, pattern):
         proto = BMProto()
diff --git a/src/tests/test_network.py b/src/tests/test_network.py
index cbdcb903..08cd95ce 100644
--- a/src/tests/test_network.py
+++ b/src/tests/test_network.py
@@ -63,6 +63,31 @@ class TestNetwork(TestPartialRun):
         else:
             self.fail('Have not started any connection in 30 sec')
 
+    def test_udp(self):
+        """Invoke AnnounceThread.announceSelf() and check discovered peers"""
+        for _ in range(20):
+            if self.pool.udpSockets:
+                break
+            time.sleep(1)
+        else:
+            self.fail('No UDP sockets found in 20 sec')
+
+        for _ in range(10):
+            try:
+                self.state.announceThread.announceSelf()
+            except AttributeError:
+                self.fail('state.announceThread is not set properly')
+            time.sleep(1)
+            try:
+                peer = self.state.discoveredPeers.popitem()[0]
+            except KeyError:
+                continue
+            else:
+                self.assertEqual(peer.port, 8444)
+                break
+        else:
+            self.fail('No self in discovered peers')
+
     @classmethod
     def tearDownClass(cls):
         super(TestNetwork, cls).tearDownClass()
-- 
2.45.1