-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPiperTest.cs
More file actions
126 lines (111 loc) · 3.66 KB
/
PiperTest.cs
File metadata and controls
126 lines (111 loc) · 3.66 KB
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
using System.Net;
using System.Net.Sockets;
public class PiperTest :
IDisposable
{
readonly List<string> Logs = [];
readonly TraceListener listener;
public PiperTest()
{
listener = new LogCapture(Logs);
Trace.Listeners.Add(listener);
}
public void Dispose()
{
Trace.Listeners.Remove(listener);
listener.Dispose();
}
[Test]
public Task MoveJson() =>
Verify(
PiperClient.BuildMovePayload(
"theTempFilePath",
"theTargetFilePath",
"theExePath",
"TheArguments",
true,
1000));
[Test]
public Task DeleteJson() =>
Verify(
PiperClient.BuildMovePayload(
"theTempFilePath",
"theTargetFilePath",
"theExePath",
"TheArguments",
true,
1000));
[Test]
public async Task Delete()
{
DeletePayload received = null!;
var source = new CancelSource();
var task = PiperServer.Start(_ => { }, s => received = s, source.Token);
await PiperClient.SendDeleteAsync("Foo", source.Token);
await Task.Delay(1000, source.Token);
source.Cancel();
await task;
await Verify(received);
}
[Test]
public async Task Move()
{
MovePayload received = null!;
var source = new CancelSource();
var task = PiperServer.Start(s => received = s, _ => { }, source.Token);
await PiperClient.SendMoveAsync("Foo", "Bar", "theExe", "TheArguments \"s\"", true, 10, source.Token);
await Task.Delay(1000, source.Token);
source.Cancel();
await task;
await Verify(received);
}
[Test]
public async Task ClientDisconnectsAbruptly()
{
DeletePayload? received = null;
var source = new CancelSource();
var task = PiperServer.Start(_ => { }, s => received = s, source.Token);
// Connect and immediately close with RST (no data sent),
// simulating a client that was canceled mid-connection.
using (var client = new TcpClient())
{
await client.ConnectAsync(IPAddress.Loopback, PiperClient.Port);
// Linger with timeout 0 causes a RST (forcible close) on Close
client.LingerState = new(true, 0);
}
// Give the server time to process the abrupt disconnect
await Task.Delay(500);
// Server should still work after the abrupt disconnect
await PiperClient.SendDeleteAsync("Foo", source.Token);
await Task.Delay(1000, source.Token);
source.Cancel();
await task;
// Verify the server recovered and processed the subsequent valid message
Assert.NotNull(received);
Assert.Equal("Foo", received!.File);
}
[Test]
public async Task SendOnly()
{
var file = Path.GetFullPath("temp.txt");
File.Delete(file);
await File.WriteAllTextAsync(file, "a");
try
{
await PiperClient.SendMoveAsync(file, file, "theExe", "TheArguments \"s\"", true, 10);
await PiperClient.SendDeleteAsync(file);
}
catch (InvalidOperationException)
{
}
await Verify(Logs)
.ScrubLinesContaining("temp.txt")
//TODO: add "scrub source dir" to verify and remove the below
.ScrubLinesContaining("PiperClient");
}
class LogCapture(List<string> logs) : TraceListener
{
public override void Write(string? message) { }
public override void WriteLine(string? message) => logs.Add(message ?? "");
}
}