Skip to content

Commit 13e55bd

Browse files
committed
Fix xUnit1031 warnings
1 parent fec3cc2 commit 13e55bd

File tree

6 files changed

+71
-16
lines changed

6 files changed

+71
-16
lines changed

src/NetMQ.Tests/NetMQMonitorTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void StartAsync()
6868
Thread.Sleep(200);
6969
Assert.Equal(TaskStatus.Running, task.Status);
7070
monitor.Stop();
71-
Assert.True(task.Wait(TimeSpan.FromMilliseconds(1000)));
71+
Assert.True(Utils.Tasks.Wait(task, TimeSpan.FromMilliseconds(1000)));
7272
}
7373
}
7474
#endif
@@ -154,7 +154,7 @@ public void MonitorDisposeProperlyWhenDisposedAfterMonitoredTcpSocket()
154154
}
155155
Thread.Sleep(100);
156156
// Monitor.Dispose should complete
157-
var completed = Task.Factory.StartNew(() => monitor.Dispose()).Wait(1000);
157+
var completed = Utils.Tasks.Wait(Task.Factory.StartNew(() => monitor.Dispose()), TimeSpan.FromMilliseconds(1000));
158158
Assert.True(completed);
159159
}
160160
// NOTE If this test fails, it will hang because context.Dispose will block

src/NetMQ.Tests/NetMQPollerTest.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public void RemoveSocket()
395395

396396
poller.Stop();
397397
// await the pollerTask, 1ms should suffice
398-
pollerTask.Wait(1);
398+
Utils.Tasks.Wait(pollerTask, TimeSpan.FromMilliseconds(1));
399399
Assert.True(pollerTask.IsCompleted);
400400
}
401401
}
@@ -879,7 +879,7 @@ public void OneTask()
879879
Assert.True(poller.CanExecuteTaskInline, "Should be on NetMQPoller thread");
880880
});
881881
task.Start(poller);
882-
task.Wait();
882+
Utils.Tasks.Wait(task);
883883

884884
Assert.True(triggered);
885885
}
@@ -894,7 +894,7 @@ public void SetsCurrentTaskScheduler()
894894

895895
var task = new Task(() => Assert.Same(TaskScheduler.Current, poller));
896896
task.Start(poller);
897-
task.Wait();
897+
Utils.Tasks.Wait(task);
898898
}
899899
}
900900

@@ -911,7 +911,7 @@ public void CanExecuteTaskInline()
911911

912912
var task = new Task(() => Assert.True(poller.CanExecuteTaskInline));
913913
task.Start(poller);
914-
task.Wait();
914+
Utils.Tasks.Wait(task);
915915
}
916916
}
917917

@@ -941,8 +941,8 @@ public void ContinueWith()
941941
}, poller);
942942

943943
task.Start(poller);
944-
task.Wait();
945-
task2.Wait();
944+
Utils.Tasks.Wait(task);
945+
Utils.Tasks.Wait(task2);
946946

947947
Assert.Equal(threadId1, threadId2);
948948
Assert.Equal(1, runCount1);
@@ -982,9 +982,9 @@ public void TwoThreads()
982982
}
983983
});
984984

985-
t1.Wait(1000);
986-
t2.Wait(1000);
987-
Task.WaitAll(allTasks.ToArray(), 1000);
985+
Utils.Tasks.Wait(t1, TimeSpan.FromMilliseconds(1000));
986+
Utils.Tasks.Wait(t2, TimeSpan.FromMilliseconds(1000));
987+
Utils.Tasks.WaitAll(allTasks.ToArray(), TimeSpan.FromMilliseconds(1000));
988988

989989
Assert.Equal(100, count1);
990990
Assert.Equal(100, count2);

src/NetMQ.Tests/NetMQQueueTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void EnqueueShouldNotBlockWhenCapacityIsZero()
3939
}
4040
});
4141

42-
bool completed = task.Wait(TimeSpan.FromSeconds(1));
42+
bool completed = Utils.Tasks.Wait(task, TimeSpan.FromSeconds(1));
4343
Assert.True(completed, "Enqueue task should have completed " + socketWatermarkCapacity + " enqueue within 1 second");
4444
}
4545
}

src/NetMQ.Tests/PgmTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ public void Sending1000Messages()
230230
}
231231
});
232232

233-
pubTask.Wait();
234-
subTask.Wait();
233+
Utils.Tasks.Wait(pubTask);
234+
Utils.Tasks.Wait(subTask);
235235

236236
Assert.Equal(1000, count);
237237
}
@@ -291,7 +291,7 @@ public void SubscriberCleanupOnUnbind(string address)
291291

292292
monitor.Stop();
293293

294-
monitorTask.Wait();
294+
Utils.Tasks.Wait(monitorTask);
295295
}
296296
}
297297
}

src/NetMQ.Tests/SocketTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void ReceiveMessageWithTimeout()
134134
t1.Start();
135135
t2.Start();
136136

137-
Task.WaitAll(t1, t2);
137+
Utils.Tasks.WaitAll(new[]{t1, t2});
138138
}
139139
}
140140

src/NetMQ/Utils/Tasks.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace NetMQ.Utils
9+
{
10+
internal class Tasks
11+
{
12+
internal static async Task PollUntil(Func<bool> condition, TimeSpan timeout)
13+
{
14+
var cts = new CancellationTokenSource();
15+
cts.CancelAfter(timeout);
16+
17+
await PollUntil(condition, cts.Token);
18+
}
19+
20+
internal static async Task PollUntil(Func<bool> condition, CancellationToken ct = default)
21+
{
22+
try
23+
{
24+
while (!condition())
25+
{
26+
await Task.Delay(25, ct).ConfigureAwait(true);
27+
}
28+
}
29+
catch (TaskCanceledException)
30+
{
31+
// Task was cancelled. Ignore exception and return.
32+
}
33+
}
34+
35+
internal static bool WaitAll(Task[] tasks, TimeSpan timeout)
36+
{
37+
return PollUntil(() => tasks.All(t => t.IsCompleted), timeout).Status == TaskStatus.RanToCompletion;
38+
}
39+
40+
internal static bool WaitAll(Task[] tasks)
41+
{
42+
return WaitAll(tasks, TimeSpan.MaxValue);
43+
}
44+
45+
internal static bool Wait(Task task, TimeSpan timeout)
46+
{
47+
return PollUntil(() => task.IsCompleted, timeout).Status == TaskStatus.RanToCompletion;
48+
}
49+
50+
internal static bool Wait(Task task)
51+
{
52+
return Wait(task, TimeSpan.MaxValue);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)