Skip to content

Commit 2ec1ff7

Browse files
authored
Update EchoServerServiceTests.cs
1 parent e7b936f commit 2ec1ff7

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

NetSdrClientAppTests/EchoServerServiceTests.cs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,25 @@ public async Task HandleClientAsync_ShouldEchoReceivedData()
3737

3838
byte[] receivedData = new byte[] { 1, 2, 3, 4, 5 };
3939
byte[]? echoedData = null;
40+
int readCallCount = 0;
4041

41-
// ✅ ВИПРАВЛЕНО: використовуємо Func<> замість лямбди з параметрами
42+
// Setup: перший виклик повертає дані, другий - 0 (кінець)
4243
mockStream
43-
.SetupSequence(s => s.ReadAsync(
44+
.Setup(s => s.ReadAsync(
4445
It.IsAny<byte[]>(),
4546
It.IsAny<int>(),
4647
It.IsAny<int>(),
4748
It.IsAny<CancellationToken>()))
48-
.Returns(Task.FromResult(5))
49-
.Callback<byte[], int, int, CancellationToken>((buffer, offset, count, token) =>
49+
.ReturnsAsync((byte[] buffer, int offset, int count, CancellationToken token) =>
5050
{
51-
Array.Copy(receivedData, 0, buffer, offset, receivedData.Length);
52-
})
53-
.ReturnsAsync(0);
51+
if (readCallCount == 0)
52+
{
53+
Array.Copy(receivedData, 0, buffer, offset, receivedData.Length);
54+
readCallCount++;
55+
return receivedData.Length;
56+
}
57+
return 0; // Кінець стріму
58+
});
5459

5560
mockStream
5661
.Setup(s => s.WriteAsync(
@@ -151,25 +156,31 @@ public async Task HandleClientAsync_ShouldEchoMultipleMessages()
151156

152157
byte[] message1 = new byte[] { 1, 2, 3 };
153158
byte[] message2 = new byte[] { 4, 5, 6, 7 };
159+
int readCallCount = 0;
154160

155-
// ✅ ВИПРАВЛЕНО: правильний синтаксис для SetupSequence
161+
// Setup: 2 повідомлення, потім 0
156162
mockStream
157-
.SetupSequence(s => s.ReadAsync(
163+
.Setup(s => s.ReadAsync(
158164
It.IsAny<byte[]>(),
159165
It.IsAny<int>(),
160166
It.IsAny<int>(),
161167
It.IsAny<CancellationToken>()))
162-
.Returns(Task.FromResult(message1.Length))
163-
.Callback<byte[], int, int, CancellationToken>((buffer, offset, count, token) =>
168+
.ReturnsAsync((byte[] buffer, int offset, int count, CancellationToken token) =>
164169
{
165-
Array.Copy(message1, 0, buffer, offset, message1.Length);
166-
})
167-
.Returns(Task.FromResult(message2.Length))
168-
.Callback<byte[], int, int, CancellationToken>((buffer, offset, count, token) =>
169-
{
170-
Array.Copy(message2, 0, buffer, offset, message2.Length);
171-
})
172-
.ReturnsAsync(0);
170+
if (readCallCount == 0)
171+
{
172+
Array.Copy(message1, 0, buffer, offset, message1.Length);
173+
readCallCount++;
174+
return message1.Length;
175+
}
176+
else if (readCallCount == 1)
177+
{
178+
Array.Copy(message2, 0, buffer, offset, message2.Length);
179+
readCallCount++;
180+
return message2.Length;
181+
}
182+
return 0; // Кінець
183+
});
173184

174185
int writeCount = 0;
175186
mockStream

0 commit comments

Comments
 (0)