Skip to content

Commit 6003287

Browse files
committed
test: 增加单元测试
1 parent 4ce7310 commit 6003287

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

test/UnitTest/Services/TcpSocketFactoryTest.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,22 @@ public async Task FixLengthDataPackageHandler_Sticky()
249249

250250
// 验证接收到的数据
251251
Assert.Equal(receivedBuffer.ToArray(), [1, 2, 3, 4, 5, 3, 4]);
252-
253-
// 等待第二次数据
254252
receivedBuffer = Memory<byte>.Empty;
255253
tcs = new TaskCompletionSource();
254+
255+
// 等待第二次数据
256256
await tcs.Task;
257257

258258
// 验证第二次收到的数据
259-
Assert.Equal(receivedBuffer.ToArray(), [1, 2, 3, 4, 5, 6, 7]);
259+
Assert.Equal(receivedBuffer.ToArray(), [2, 2, 3, 4, 5, 6, 7]);
260+
tcs = new TaskCompletionSource();
261+
await tcs.Task;
262+
263+
// 等待第二次数据
264+
await tcs.Task;
265+
266+
// 验证第三次收到的数据
267+
Assert.Equal(receivedBuffer.ToArray(), [3, 2, 3, 4, 5, 6, 7]);
260268

261269
// 关闭连接
262270
client.Close();
@@ -398,16 +406,16 @@ private static async Task MockStickyPackageAsync(TcpClient client)
398406
await stream.WriteAsync(block, CancellationToken.None);
399407

400408
// 模拟延时
401-
await Task.Delay(50);
409+
await Task.Delay(10);
402410

403411
// 模拟拆包发送第二段数据
404-
await stream.WriteAsync(new byte[] { 0x3, 0x4, 0x1, 0x2 }, CancellationToken.None);
412+
await stream.WriteAsync(new byte[] { 0x3, 0x4, 0x2, 0x2 }, CancellationToken.None);
405413

406414
// 模拟延时
407-
await Task.Delay(50);
415+
await Task.Delay(10);
408416

409417
// 模拟粘包发送后续数据
410-
await stream.WriteAsync(new byte[] { 0x3, 0x4, 0x5, 0x6, 0x7 }, CancellationToken.None);
418+
await stream.WriteAsync(new byte[] { 0x3, 0x4, 0x5, 0x6, 0x7, 0x3, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x1 }, CancellationToken.None);
411419
}
412420
}
413421

0 commit comments

Comments
 (0)