-
Notifications
You must be signed in to change notification settings - Fork 750
/
Copy pathClientServer.cs
163 lines (131 loc) · 4.97 KB
/
ClientServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Threading;
using System.Threading.Tasks;
using NetMQ;
using NetMQ.Sockets;
using Xunit;
using Xunit.Abstractions;
namespace NetMQ.Tests
{
public class ClientServer
{
private readonly ITestOutputHelper m_testOutputHelper;
public ClientServer(ITestOutputHelper testOutputHelper)
{
m_testOutputHelper = testOutputHelper;
}
[Fact]
public void Inproc()
{
using var server = new ServerSocket();
using var client = new ClientSocket();
server.Bind("inproc://client-server");
client.Connect("inproc://client-server");
client.Send("Hello");
var (routingId, clientMsg) = server.ReceiveString();
Assert.NotEqual<uint>(0, routingId);
Assert.Equal("Hello", clientMsg);
server.Send(routingId, "World");
var serverMsg = client.ReceiveString();
Assert.Equal("World", serverMsg);
}
[Fact]
public void Tcp()
{
using var server = new ServerSocket();
using var client = new ClientSocket();
int port = server.BindRandomPort("tcp://*");
client.Connect($"tcp://localhost:{port}");
client.Send("Hello");
var (routingId, clientMsg) = server.ReceiveString();
Assert.NotEqual<uint>(0, routingId);
Assert.Equal("Hello", clientMsg);
server.Send(routingId, "World");
var serverMsg = client.ReceiveString();
Assert.Equal("World", serverMsg);
}
[Fact]
public async Task Async()
{
using var server = new ServerSocket();
using var client = new ClientSocket();
int port = server.BindRandomPort("tcp://*");
client.Connect($"tcp://localhost:{port}");
await client.SendAsync("Hello");
var (routingId, clientMsg) = await server.ReceiveStringAsync();
Assert.NotEqual<uint>(0, routingId);
Assert.Equal("Hello", clientMsg);
await server.SendAsync(routingId, "World");
var serverMsg = await client.ReceiveStringAsync();
Assert.Equal("World", serverMsg);
}
[Fact]
public async Task AsyncWithCancellationToken()
{
using CancellationTokenSource source = new CancellationTokenSource();
using var server = new ServerSocket();
source.CancelAfter(100);
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await server.ReceiveStringAsync(source.Token));
}
#if NETCOREAPP3_1
[Fact(Timeout = 120)]
public async Task AsyncEnumerableCanceled()
{
using CancellationTokenSource source = new CancellationTokenSource();
using var server = new ServerSocket();
source.CancelAfter(100);
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
{
await foreach (var msg in server.ReceiveStringAsyncEnumerable(source.Token))
return;
});
}
[Fact(Timeout = 1000)]
public void AsyncEnumerable()
{
using var server = new ServerSocket();
int port = server.BindRandomPort("tcp://*");
using var client = new ClientSocket();
client.Connect($"tcp://127.0.0.1:{port}");
int totalCount = 0;
var t1 = Task.Run(async () =>
{
int count = 0;
await foreach (var (_, msg) in server.ReceiveStringAsyncEnumerable())
{
count++;
Interlocked.Increment(ref totalCount);
if (msg == "1")
{
m_testOutputHelper.WriteLine($"T1 read {count} messages");
return;
}
}
});
var t2 = Task.Run(async () => {
int count = 0;
await foreach (var (_, msg) in server.ReceiveStringAsyncEnumerable())
{
count++;
Interlocked.Increment(ref totalCount);
if (msg == "1")
{
m_testOutputHelper.WriteLine($"T2 read {count} messages");
return;
}
}
});
for (int i = 0; i < 15000; i++)
{
client.Send("0");
}
// Send the end message to both of the threads
client.Send("1");
client.Send("1");
t1.Wait();
t2.Wait();
Assert.Equal(15002, totalCount);
}
#endif
}
}