From 7befe0669b8d244b72d4b9269cd89113a52d2753 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 6 Apr 2026 09:03:28 +0200 Subject: [PATCH 1/2] [tests] Make NSStreamTest.ConnectToPeer time out cleanly. As opposed to hanging the entire test suite (and then getting aborted) if something goes wrong. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../monotouch-test/Foundation/NSStreamTest.cs | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/tests/monotouch-test/Foundation/NSStreamTest.cs b/tests/monotouch-test/Foundation/NSStreamTest.cs index 2fd9aaa4049a..f61666389803 100644 --- a/tests/monotouch-test/Foundation/NSStreamTest.cs +++ b/tests/monotouch-test/Foundation/NSStreamTest.cs @@ -83,9 +83,6 @@ public void ConnectToHost () [Test] public void ConnectToPeer () { - NSInputStream read; - NSOutputStream write; - int port; var listener = FindPort (out port); if (listener is null) { @@ -93,21 +90,44 @@ public void ConnectToPeer () return; } - var listenThread = new Thread (new ParameterizedThreadStart (DebugListener)); - listenThread.Start (listener); - NSStream.CreatePairWithPeerSocketSignature (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, new IPEndPoint (IPAddress.Loopback, port), out read, out write); - read.Open (); - write.Open (); - var send = new byte [] { 1, 2, 3, 4, 5 }; - Assert.AreEqual ((nint) 5, write.Write (send), "Write"); - var result = new byte [5]; - Assert.AreEqual ((nint) 5, read.Read (result, 5), "Read"); - for (int i = 0; i < 5; i++) - Assert.AreEqual (send [i] * 10, result [i]); - listenThread.Join (); - listener.Stop (); - read.Close (); - write.Close (); + Exception ex = null; + var thread = new Thread (() => { + try { + NSInputStream read = null; + NSOutputStream write = null; + + var listenThread = new Thread (new ParameterizedThreadStart (DebugListener)) { + IsBackground = true, + }; + var listenThreadCompleted = false; + try { + listenThread.Start (listener); + NSStream.CreatePairWithPeerSocketSignature (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, new IPEndPoint (IPAddress.Loopback, port), out read, out write); + read.Open (); + write.Open (); + var send = new byte [] { 1, 2, 3, 4, 5 }; + Assert.AreEqual ((nint) 5, write.Write (send), "Write"); + var result = new byte [5]; + Assert.AreEqual ((nint) 5, read.Read (result, 5), "Read"); + for (int i = 0; i < 5; i++) + Assert.AreEqual (send [i] * 10, result [i], "Item " + i); + listenThreadCompleted = listenThread.Join (TimeSpan.FromSeconds (5)); + Assert.That (listenThreadCompleted, Is.True, "Listener thread"); + } finally { + if (listenThreadCompleted) + listener.Stop (); + read?.Close (); + write?.Close (); + } + } catch (Exception e) { + ex = e; + } + }) { + IsBackground = true, + }; + thread.Start (); + Assert.That (thread.Join (TimeSpan.FromSeconds (10)), Is.True, "Background thread completion"); + Assert.IsNull (ex, "No exception"); } void DebugListener (object data) From 9ff2da9acb7d2f9cb2b4ca7dfdf9b7f0845cb229 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 6 Apr 2026 10:01:21 +0200 Subject: [PATCH 2/2] [tests] Clean up NSStreamTest peer listener Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/monotouch-test/Foundation/NSStreamTest.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/monotouch-test/Foundation/NSStreamTest.cs b/tests/monotouch-test/Foundation/NSStreamTest.cs index f61666389803..ee61e074b0e3 100644 --- a/tests/monotouch-test/Foundation/NSStreamTest.cs +++ b/tests/monotouch-test/Foundation/NSStreamTest.cs @@ -114,8 +114,7 @@ public void ConnectToPeer () listenThreadCompleted = listenThread.Join (TimeSpan.FromSeconds (5)); Assert.That (listenThreadCompleted, Is.True, "Listener thread"); } finally { - if (listenThreadCompleted) - listener.Stop (); + listener.Stop (); read?.Close (); write?.Close (); } @@ -133,7 +132,16 @@ public void ConnectToPeer () void DebugListener (object data) { var listener = data as TcpListener; - var client = listener.AcceptTcpClient (); + TcpClient client; + try { + client = listener.AcceptTcpClient (); + } catch (ObjectDisposedException) { + return; + } catch (SocketException) { + return; + } catch (InvalidOperationException) { + return; + } var stream = client.GetStream (); byte [] buffer = new byte [512];