Skip to content

Commit 792096a

Browse files
committed
handle test cancellation
1 parent 86a542a commit 792096a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/DiffEngineTray.Tests/PiperTest.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System.Net;
2+
using System.Net.Sockets;
3+
14
public class PiperTest :
25
IDisposable
36
{
@@ -64,6 +67,36 @@ public async Task Move()
6467
await Verify(received);
6568
}
6669

70+
[Fact]
71+
public async Task ClientDisconnectsAbruptly()
72+
{
73+
DeletePayload? received = null;
74+
var source = new CancelSource();
75+
var task = PiperServer.Start(_ => { }, s => received = s, source.Token);
76+
77+
// Connect and immediately close with RST (no data sent),
78+
// simulating a client that was canceled mid-connection.
79+
using (var client = new TcpClient())
80+
{
81+
await client.ConnectAsync(IPAddress.Loopback, PiperClient.Port);
82+
// Linger with timeout 0 causes a RST (forcible close) on Close
83+
client.LingerState = new(true, 0);
84+
}
85+
86+
// Give the server time to process the abrupt disconnect
87+
await Task.Delay(500);
88+
89+
// Server should still work after the abrupt disconnect
90+
await PiperClient.SendDeleteAsync("Foo", source.Token);
91+
await Task.Delay(1000, source.Token);
92+
source.Cancel();
93+
await task;
94+
95+
// Verify the server recovered and processed the subsequent valid message
96+
Assert.NotNull(received);
97+
Assert.Equal("Foo", received!.File);
98+
}
99+
67100
[Fact]
68101
public async Task SendOnly()
69102
{

src/DiffEngineTray/PiperServer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public static async Task Start(
3535
//when task is cancelled socket is disposed
3636
break;
3737
}
38+
catch (IOException exception)
39+
when (exception.InnerException is SocketException { SocketErrorCode: SocketError.ConnectionReset })
40+
{
41+
//client disconnected abruptly, e.g. test was canceled
42+
}
3843
catch (Exception exception)
3944
{
4045
if (cancel.IsCancellationRequested)
@@ -58,6 +63,7 @@ static async Task Handle(TcpListener listener, Action<MovePayload> move, Action<
5863
{
5964
using var client = await listener.AcceptTcpClientAsync(cancel);
6065
using var reader = new StreamReader(client.GetStream());
66+
6167
var payload = await reader.ReadToEndAsync(cancel);
6268

6369
if (payload.Contains("\"Type\":\"Move\"") ||

0 commit comments

Comments
 (0)