Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite4408b8

Browse files
authored
chore: fix TestHttpServer test flake (#159)
Reworks the TestHttpServer threading to:- Use an async method instead (so we can wait with a cancellation token)- Use a cancellation token while awaiting the listener to return arequest- `GetContext()` doesn't take a CTS directly, so we call`Task.WaitAsync(CancellationToken ct)` instead- Adds some exception handling and discarding where necessary to ignoreexpected errors during shutdownClosescoder/internal#603
1 parentc1ea64d commite4408b8

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

‎Tests.Vpn.Service/TestHttpServer.cs‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TestHttpServer : IDisposable
1313
privatereadonlyCancellationTokenSource_cts=new();
1414
privatereadonlyFunc<HttpListenerContext,Task>_handler;
1515
privatereadonlyHttpListener_listener;
16-
privatereadonlyThread_listenerThread;
16+
privatereadonlyTask_listenerTask;
1717

1818
publicstringBaseUrl{get;privateset;}
1919

@@ -60,31 +60,45 @@ public TestHttpServer(Func<HttpListenerContext, Task> handler)
6060
thrownewInvalidOperationException("Could not find a free port to listen on");
6161
BaseUrl=$"http://localhost:{port}";
6262

63-
_listenerThread=newThread(()=>
64-
{
65-
while(!_cts.Token.IsCancellationRequested)
66-
try
67-
{
68-
varcontext=_listener.GetContext();
69-
Task.Run(()=>HandleRequest(context));
70-
}
71-
catch(HttpListenerException)when(_cts.Token.IsCancellationRequested)
72-
{
73-
break;
74-
}
75-
});
76-
77-
_listenerThread.Start();
63+
_listenerTask=RequestLoop();
7864
}
7965

8066
publicvoidDispose()
8167
{
8268
_cts.Cancel();
8369
_listener.Stop();
84-
_listenerThread.Join();
70+
try
71+
{
72+
_listenerTask.GetAwaiter().GetResult();
73+
}
74+
catch(TaskCanceledException)
75+
{
76+
// Ignore
77+
}
8578
GC.SuppressFinalize(this);
8679
}
8780

81+
privateasyncTaskRequestLoop()
82+
{
83+
while(!_cts.Token.IsCancellationRequested)
84+
try
85+
{
86+
varcontextTask=_listener.GetContextAsync();
87+
// Wait with a cancellation token.
88+
awaitcontextTask.WaitAsync(_cts.Token);
89+
// Get the context or throw if there was an error.
90+
varcontext=awaitcontextTask;
91+
// Run the handler in the background.
92+
_=Task.Run(()=>HandleRequest(context));
93+
}
94+
catch(HttpListenerException)when(_cts.Token.IsCancellationRequested)
95+
{
96+
// Ignore, we expect the listener to throw an exception when
97+
// it's stopped
98+
break;
99+
}
100+
}
101+
88102
privateasyncTaskHandleRequest(HttpListenerContextcontext)
89103
{
90104
try

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp