Skip to content

Commit 3d3766e

Browse files
committed
improved unit tests
1 parent 29e81e2 commit 3d3766e

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

test/SuperSocket.Tests/MainTest.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public async Task TestSecurityOptions(string security, SslProtocols protocols, b
109109
var hostConfigurator = new SecureHostConfigurator();
110110
var listener = default(ListenOptions);
111111

112+
var autoResetEvent = new AutoResetEvent(false);
113+
112114
var createServer = new Func<IServer>(() =>
113115
{
114116
return CreateSocketServerBuilder<TextPackageInfo, LinePipelineFilter>(hostConfigurator)
@@ -123,6 +125,16 @@ public async Task TestSecurityOptions(string security, SslProtocols protocols, b
123125
{
124126
listener = serverOptions.Listeners.FirstOrDefault();
125127
})
128+
.UseSessionHandler(onConnected: (session) =>
129+
{
130+
autoResetEvent.Set();
131+
return ValueTask.CompletedTask;
132+
},
133+
onClosed: (session, reason) =>
134+
{
135+
autoResetEvent.Set();
136+
return ValueTask.CompletedTask;
137+
})
126138
.BuildAsServer();
127139
});
128140

@@ -147,7 +159,7 @@ public async Task TestSecurityOptions(string security, SslProtocols protocols, b
147159
{
148160
Assert.Equal("TestServer", server.Name);
149161

150-
Assert.True(await server.StartAsync());
162+
Assert.True(await server.StartAsync(TestContext.Current.CancellationToken));
151163
OutputHelper.WriteLine("Started.");
152164

153165
Assert.Equal(0, server.SessionCount);
@@ -156,16 +168,16 @@ public async Task TestSecurityOptions(string security, SslProtocols protocols, b
156168
using (var socket = CreateClient(hostConfigurator))
157169
{
158170
var socketStream = await hostConfigurator.GetClientStream(socket);
159-
await Task.Delay(500);
171+
autoResetEvent.WaitOne(1000);
160172
Assert.Equal(1, server.SessionCount);
161173
OutputHelper.WriteLine("SessionCount:" + server.SessionCount);
162174
}
163175

164-
await Task.Delay(500);
176+
autoResetEvent.WaitOne(1000);
165177
Assert.Equal(0, server.SessionCount);
166178
OutputHelper.WriteLine("SessionCount:" + server.SessionCount);
167179

168-
await server.StopAsync();
180+
await server.StopAsync(TestContext.Current.CancellationToken);
169181
}
170182
}
171183

@@ -178,14 +190,18 @@ public async Task TestSessionHandlers(Type hostConfiguratorType)
178190
var connected = false;
179191
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);
180192

193+
var autoResetEvent = new AutoResetEvent(false);
194+
181195
using (var server = CreateSocketServerBuilder<TextPackageInfo, LinePipelineFilter>(hostConfigurator)
182196
.UseSessionHandler((s) =>
183197
{
184198
connected = true;
199+
autoResetEvent.Set();
185200
return new ValueTask();
186201
}, (s, e) =>
187202
{
188203
connected = false;
204+
autoResetEvent.Set();
189205
return new ValueTask();
190206
})
191207
.UsePackageHandler(async (s, p) =>
@@ -196,7 +212,7 @@ public async Task TestSessionHandlers(Type hostConfiguratorType)
196212
{
197213
Assert.Equal("TestServer", server.Name);
198214

199-
Assert.True(await server.StartAsync());
215+
Assert.True(await server.StartAsync(TestContext.Current.CancellationToken));
200216
OutputHelper.WriteLine("Started.");
201217

202218
var client = hostConfigurator.CreateClient();
@@ -212,7 +228,7 @@ public async Task TestSessionHandlers(Type hostConfiguratorType)
212228

213229
OutputHelper.WriteLine("Connected.");
214230

215-
await Task.Delay(1000);
231+
autoResetEvent.WaitOne(1000);
216232

217233
Assert.True(connected);
218234

@@ -228,7 +244,7 @@ public async Task TestSessionHandlers(Type hostConfiguratorType)
228244
client.Close();
229245
}
230246

231-
await Task.Delay(1000);
247+
autoResetEvent.WaitOne(1000);
232248

233249
if (outputStream != null)
234250
{
@@ -237,7 +253,7 @@ public async Task TestSessionHandlers(Type hostConfiguratorType)
237253

238254
Assert.False(connected);
239255

240-
await server.StopAsync();
256+
await server.StopAsync(TestContext.Current.CancellationToken);
241257
}
242258
}
243259

0 commit comments

Comments
 (0)