From c1d244e1c379bc52a8b68e1a0016fb74121a4d1f Mon Sep 17 00:00:00 2001 From: "joshua.sells" Date: Thu, 31 Dec 2020 14:19:43 -0600 Subject: [PATCH 1/3] make sure these tests don't block the main test thread for debug build. --- src/NetMQ.Tests/NetMQPollerTest.cs | 26 +++++++++++++++---- src/NetMQ/Core/Transports/ByteArraySegment.cs | 2 +- src/NetMQ/Core/Transports/V2Encoder.cs | 3 ++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/NetMQ.Tests/NetMQPollerTest.cs b/src/NetMQ.Tests/NetMQPollerTest.cs index f49030732..3e06e275f 100644 --- a/src/NetMQ.Tests/NetMQPollerTest.cs +++ b/src/NetMQ.Tests/NetMQPollerTest.cs @@ -51,8 +51,8 @@ public void ResponsePoll() } } - [Fact] - public void Monitoring() + [Fact(Timeout=5000)] + public async void Monitoring() { var listeningEvent = new ManualResetEvent(false); var acceptedEvent = new ManualResetEvent(false); @@ -80,11 +80,27 @@ public void Monitoring() req.Connect("tcp://127.0.0.1:" + port); req.SendFrame("a"); - rep.SkipFrame(); + // keep the test from blocking when something goes wrong. + var timeout = new CancellationTokenSource(5000).Token; + Exception e = null; + + await Task.Run(() => + { + try + { + rep.SkipFrame(); + + rep.SendFrame("b"); - rep.SendFrame("b"); + req.SkipFrame(); + } + catch (Exception ex) + { + e = ex; + } + }, timeout); - req.SkipFrame(); + Assert.Null(e); Assert.True(listeningEvent.WaitOne(300)); Assert.True(connectedEvent.WaitOne(300)); diff --git a/src/NetMQ/Core/Transports/ByteArraySegment.cs b/src/NetMQ/Core/Transports/ByteArraySegment.cs index 6ea54160b..b8ca99dea 100644 --- a/src/NetMQ/Core/Transports/ByteArraySegment.cs +++ b/src/NetMQ/Core/Transports/ByteArraySegment.cs @@ -57,7 +57,7 @@ public ByteArraySegment(ByteArraySegment otherSegment, int offset) /// /// Get the number of bytes within the buffer that is past the Offset (ie, buffer-length minus offset). /// - public int Size => m_innerBuffer.Length - Offset; + public int Size => m_innerBuffer == null ? 0 : m_innerBuffer.Length - Offset; /// /// Add the given value to the offset. diff --git a/src/NetMQ/Core/Transports/V2Encoder.cs b/src/NetMQ/Core/Transports/V2Encoder.cs index d622bc6fd..8d898293e 100644 --- a/src/NetMQ/Core/Transports/V2Encoder.cs +++ b/src/NetMQ/Core/Transports/V2Encoder.cs @@ -33,7 +33,8 @@ protected override void Next() private void SizeReady() { - Assumes.NotNull(m_inProgress.UnsafeData); + // this assumption is now deprecated. + //Assumes.NotNull(m_inProgress.UnsafeData); // Write message body into the buffer. NextStep(new ByteArraySegment(m_inProgress.UnsafeData, m_inProgress.UnsafeOffset), From c9e02a0077074f07fa1108e6a504be4b6686c1a9 Mon Sep 17 00:00:00 2001 From: "joshua.sells" Date: Thu, 31 Dec 2020 14:21:13 -0600 Subject: [PATCH 2/3] put the assertion in correct place to allow unit tests to run in debug --- src/NetMQ/Core/Transports/Tcp/TcpListener.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NetMQ/Core/Transports/Tcp/TcpListener.cs b/src/NetMQ/Core/Transports/Tcp/TcpListener.cs index 98b8261a5..15afafa53 100644 --- a/src/NetMQ/Core/Transports/Tcp/TcpListener.cs +++ b/src/NetMQ/Core/Transports/Tcp/TcpListener.cs @@ -123,12 +123,13 @@ public virtual void SetAddress(string addr) m_address.Resolve(addr, m_options.IPv4Only); Assumes.NotNull(m_address.Address); - Assumes.NotNull(m_handle); try { m_handle = AsyncSocket.Create(m_address.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); + Assumes.NotNull(m_handle); + if (!m_options.IPv4Only && m_address.Address.AddressFamily == AddressFamily.InterNetworkV6) { try From 11f3a451cb6292ae728f4747579ef3374bdd45d9 Mon Sep 17 00:00:00 2001 From: "joshua.sells" Date: Thu, 31 Dec 2020 14:55:20 -0600 Subject: [PATCH 3/3] InRange asserts give better failure messages --- src/NetMQ.Tests/NetMQPollerTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NetMQ.Tests/NetMQPollerTest.cs b/src/NetMQ.Tests/NetMQPollerTest.cs index 3e06e275f..249e1898e 100644 --- a/src/NetMQ.Tests/NetMQPollerTest.cs +++ b/src/NetMQ.Tests/NetMQPollerTest.cs @@ -783,8 +783,8 @@ public void ChangeTimerInterval() Assert.Equal(3, count); - Assert.True(Math.Abs(length1 - 30) <= 10.0); - Assert.True(Math.Abs(length2 - 60) <= 10.0); + Assert.InRange(Math.Abs(length1 - 30), 0, 10); + Assert.InRange(Math.Abs(length2 - 60), 0, 10); } [Fact]