Skip to content

Commit db1ff94

Browse files
committed
* More code clean-up
* Remove the [Timeout] attributes again, they didn’t do anything
1 parent 8c2be45 commit db1ff94

11 files changed

Lines changed: 625 additions & 629 deletions

.editorconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ dotnet_style_allow_statement_immediately_after_block_experimental = true
8484
#### C# Coding Conventions ####
8585

8686
# var preferences
87-
csharp_style_var_elsewhere = false
88-
csharp_style_var_for_built_in_types = false
89-
csharp_style_var_when_type_is_apparent = false
87+
csharp_style_var_elsewhere = true
88+
csharp_style_var_for_built_in_types = true
89+
csharp_style_var_when_type_is_apparent = true
9090

9191
# Expression-bodied members
9292
csharp_style_expression_bodied_accessors = true

Tests/AjaxHandlerTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public sealed class AjaxHandlerTests
1212
{
1313
sealed class TempObject { public int Value; }
1414

15+
#pragma warning disable CA1822 // Mark members as static
1516
sealed class Ajax
1617
{
1718
[AjaxMethod]
@@ -29,8 +30,9 @@ sealed class Ajax
2930
[AjaxConverter]
3031
public TempObject ConvertTempObject(int tempObj) => new() { Value = tempObj };
3132
}
33+
#pragma warning restore CA1822 // Mark members as static
3234

33-
[TestMethod, Timeout(60 * 1000, CooperativeCancellation = true)]
35+
[TestMethod]
3436
public void TestErrorHandlerExceptions()
3537
{
3638
var instance = new HttpServer(TestHelpers.Port + 0, new HttpServerOptions { OutputExceptionInformation = true });
@@ -43,7 +45,7 @@ public void TestErrorHandlerExceptions()
4345

4446
static (HttpStatusCode status, string response) getResponse(string method, string payload)
4547
{
46-
TcpClient cl = new();
48+
var cl = new TcpClient();
4749
cl.Connect("localhost", TestHelpers.Port + 0);
4850
cl.ReceiveTimeout = 1000; // 1 sec
4951
var payloadFull = "data=" + payload.ToUtf8().Select(b => "%" + b.ToString("X2")).JoinString();

Tests/DisconnectionTests.cs

Lines changed: 83 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,108 +7,106 @@
77
using Microsoft.VisualStudio.TestTools.UnitTesting;
88
using RT.Util.ExtensionMethods;
99

10-
namespace RT.Servers.Tests
10+
namespace RT.Servers.Tests;
11+
12+
[TestClass]
13+
public sealed class DisconnectionTests
1114
{
12-
[TestClass]
13-
public sealed class DisconnectionTests
15+
private static IEnumerable<string> enumInfinite()
1416
{
15-
private IEnumerable<string> enumInfinite()
17+
while (true)
1618
{
17-
while (true)
18-
{
19-
yield return "blah!";
20-
Thread.Sleep(0);
21-
}
19+
yield return "blah!";
20+
Thread.Sleep(0);
2221
}
22+
}
2323

24-
[TestMethod, Timeout(60 * 1000, CooperativeCancellation = true)]
25-
public void TestMidResponseSocketClosure()
24+
[TestMethod]
25+
public void TestMidResponseSocketClosure()
26+
{
27+
var instance = new HttpServer(TestHelpers.Port + 1)
2628
{
27-
var instance = new HttpServer(TestHelpers.Port + 1)
28-
{
29-
Handler = new UrlResolver(
30-
new UrlMapping(req => { return HttpResponse.Create(enumInfinite(), "text/plain"); }, path: "/infinite-and-slow")
31-
).Handle
32-
};
33-
try
34-
{
35-
instance.StartListening();
36-
37-
ThreadStart thread = () =>
38-
{
39-
for (int i = 0; i < 20; i++)
40-
{
41-
TcpClient cl = new();
42-
cl.Connect("localhost", TestHelpers.Port + 1);
43-
cl.ReceiveTimeout = 1000; // 1 sec
44-
Socket sck = cl.Client;
45-
sck.Send("GET /infinite-and-slow HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n".ToUtf8());
46-
Thread.Sleep(100);
47-
sck.Close();
48-
GC.Collect();
49-
}
50-
};
51-
var threads = Enumerable.Range(0, 10).Select(_ => new Thread(thread)).ToList();
52-
foreach (var t in threads)
53-
t.Start();
54-
foreach (var t in threads)
55-
t.Join();
29+
Handler = new UrlResolver(
30+
new UrlMapping(req => { return HttpResponse.Create(enumInfinite(), "text/plain"); }, path: "/infinite-and-slow")
31+
).Handle
32+
};
33+
try
34+
{
35+
instance.StartListening();
5636

57-
instance.StopListening(true); // server must not throw after this; that’s the point of the test
58-
}
59-
finally
37+
static void thread()
6038
{
61-
instance.StopListening(brutal: true);
39+
for (var i = 0; i < 20; i++)
40+
{
41+
var cl = new TcpClient();
42+
cl.Connect("localhost", TestHelpers.Port + 1);
43+
cl.ReceiveTimeout = 1000; // 1 sec
44+
Socket sck = cl.Client;
45+
sck.Send("GET /infinite-and-slow HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n".ToUtf8());
46+
Thread.Sleep(100);
47+
sck.Close();
48+
GC.Collect();
49+
}
6250
}
63-
}
51+
var threads = Enumerable.Range(0, 10).Select(_ => new Thread(thread)).ToList();
52+
foreach (var t in threads)
53+
t.Start();
54+
foreach (var t in threads)
55+
t.Join();
6456

65-
[TestMethod, Timeout(60 * 1000, CooperativeCancellation = true)]
66-
public void TestHalfOpenConnection()
57+
instance.StopListening(true); // server must not throw after this; that’s the point of the test
58+
}
59+
finally
6760
{
68-
var instance = new HttpServer(TestHelpers.Port + 2, new HttpServerOptions { OutputExceptionInformation = true });
69-
instance.Handler = req => HttpResponse.PlainText(" thingy stuff ");
70-
try
71-
{
72-
instance.StartListening();
61+
instance.StopListening(brutal: true);
62+
}
63+
}
7364

74-
// A proper request ending in a half closed connection
75-
using (var cl = new TcpClient())
76-
{
77-
cl.ReceiveTimeout = 1000; // 1 sec
78-
cl.Connect("localhost", TestHelpers.Port + 2);
79-
cl.Client.Send("GET /static HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n".ToUtf8());
80-
cl.Client.Shutdown(SocketShutdown.Send);
81-
var response = Encoding.UTF8.GetString(cl.Client.ReceiveAllBytes());
82-
var code = (HttpStatusCode) int.Parse(response.Substring("HTTP/1.1 ".Length, 3));
83-
var parts = response.Split("\r\n\r\n");
84-
Assert.AreEqual(HttpStatusCode._200_OK, code);
85-
Assert.AreEqual(" thingy stuff ", parts[1]);
86-
}
65+
[TestMethod]
66+
public void TestHalfOpenConnection()
67+
{
68+
var instance = new HttpServer(TestHelpers.Port + 2, new HttpServerOptions { OutputExceptionInformation = true }) { Handler = req => HttpResponse.PlainText(" thingy stuff ") };
69+
try
70+
{
71+
instance.StartListening();
8772

88-
// An incomplete request ending in a half closed connection
89-
using (var cl = new TcpClient())
90-
{
91-
cl.Connect("localhost", TestHelpers.Port + 2);
92-
cl.Client.Send("GET /static HTTP/1.1\r\nHost: localhost\r\nConnection: close".ToUtf8());
93-
cl.Client.Shutdown(SocketShutdown.Send);
94-
var response = Encoding.UTF8.GetString(cl.Client.ReceiveAllBytes());
95-
// the test is that it doesn't wait forever
96-
}
73+
// A proper request ending in a half closed connection
74+
using (var cl = new TcpClient())
75+
{
76+
cl.ReceiveTimeout = 1000; // 1 sec
77+
cl.Connect("localhost", TestHelpers.Port + 2);
78+
cl.Client.Send("GET /static HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n".ToUtf8());
79+
cl.Client.Shutdown(SocketShutdown.Send);
80+
var response = Encoding.UTF8.GetString(cl.Client.ReceiveAllBytes());
81+
var code = (HttpStatusCode) int.Parse(response.Substring("HTTP/1.1 ".Length, 3));
82+
var parts = response.Split("\r\n\r\n");
83+
Assert.AreEqual(HttpStatusCode._200_OK, code);
84+
Assert.AreEqual(" thingy stuff ", parts[1]);
85+
}
9786

98-
// A malformed request ending in a half closed connection
99-
using (var cl = new TcpClient())
100-
{
101-
cl.Connect("localhost", TestHelpers.Port + 2);
102-
cl.Client.Send("xz".ToUtf8());
103-
cl.Client.Shutdown(SocketShutdown.Send);
104-
var response = Encoding.UTF8.GetString(cl.Client.ReceiveAllBytes());
105-
// the test is that it doesn't wait forever
106-
}
87+
// An incomplete request ending in a half closed connection
88+
using (var cl = new TcpClient())
89+
{
90+
cl.Connect("localhost", TestHelpers.Port + 2);
91+
cl.Client.Send("GET /static HTTP/1.1\r\nHost: localhost\r\nConnection: close".ToUtf8());
92+
cl.Client.Shutdown(SocketShutdown.Send);
93+
var response = Encoding.UTF8.GetString(cl.Client.ReceiveAllBytes());
94+
// the test is that it doesn't wait forever
10795
}
108-
finally
96+
97+
// A malformed request ending in a half closed connection
98+
using (var cl = new TcpClient())
10999
{
110-
instance.StopListening(brutal: true);
100+
cl.Connect("localhost", TestHelpers.Port + 2);
101+
cl.Client.Send("xz".ToUtf8());
102+
cl.Client.Shutdown(SocketShutdown.Send);
103+
var response = Encoding.UTF8.GetString(cl.Client.ReceiveAllBytes());
104+
// the test is that it doesn't wait forever
111105
}
112106
}
107+
finally
108+
{
109+
instance.StopListening(brutal: true);
110+
}
113111
}
114112
}

0 commit comments

Comments
 (0)