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

Commit35abc67

Browse files
committed
handle exceptions and fix UTs
1 parent4bd6855 commit35abc67

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

‎Tests.Vpn.Service/DownloaderTest.cs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -375,17 +375,17 @@ public async Task DownloadExistingDifferentContent(CancellationToken ct)
375375

376376
[Test(Description="Unexpected response code from server")]
377377
[CancelAfter(30_000)]
378-
publicvoidUnexpectedResponseCode(CancellationTokenct)
378+
publicasyncTaskUnexpectedResponseCode(CancellationTokenct)
379379
{
380380
usingvarhttpServer=newTestHttpServer(ctx=>{ctx.Response.StatusCode=404;});
381381
varurl=newUri(httpServer.BaseUrl+"/test");
382382
vardestPath=Path.Combine(_tempDir,"test");
383383

384384
varmanager=newDownloader(NullLogger<Downloader>.Instance);
385-
// The "outer" Task should fail.
386-
varex=Assert.ThrowsAsync<HttpRequestException>(async()=>
387-
awaitmanager.StartDownloadAsync(newHttpRequestMessage(HttpMethod.Get,url),destPath,
388-
NullDownloadValidator.Instance,ct));
385+
// The "inner" Task should fail.
386+
vardlTask=awaitmanager.StartDownloadAsync(newHttpRequestMessage(HttpMethod.Get,url),destPath,
387+
NullDownloadValidator.Instance,ct);
388+
varex=Assert.ThrowsAsync<HttpRequestException>(async()=>awaitdlTask.Task);
389389
Assert.That(ex.Message,Does.Contain("404"));
390390
}
391391

@@ -412,22 +412,6 @@ public async Task MismatchedETag(CancellationToken ct)
412412
Assert.That(ex.Message,Does.Contain("ETag does not match SHA1 hash of downloaded file").And.Contains("beef"));
413413
}
414414

415-
[Test(Description="Timeout on response headers")]
416-
[CancelAfter(30_000)]
417-
publicvoidCancelledOuter(CancellationTokenct)
418-
{
419-
usingvarhttpServer=newTestHttpServer(async _=>{awaitTask.Delay(TimeSpan.FromSeconds(5),ct);});
420-
varurl=newUri(httpServer.BaseUrl+"/test");
421-
vardestPath=Path.Combine(_tempDir,"test");
422-
423-
varmanager=newDownloader(NullLogger<Downloader>.Instance);
424-
// The "outer" Task should fail.
425-
varsmallerCt=newCancellationTokenSource(TimeSpan.FromSeconds(1)).Token;
426-
Assert.ThrowsAsync<TaskCanceledException>(async()=>awaitmanager.StartDownloadAsync(
427-
newHttpRequestMessage(HttpMethod.Get,url),destPath,
428-
NullDownloadValidator.Instance,smallerCt));
429-
}
430-
431415
[Test(Description="Timeout on response body")]
432416
[CancelAfter(30_000)]
433417
publicasyncTaskCancelledInner(CancellationTokenct)
@@ -479,12 +463,10 @@ public async Task ValidationFailureExistingFile(CancellationToken ct)
479463
awaitFile.WriteAllTextAsync(destPath,"test",ct);
480464

481465
varmanager=newDownloader(NullLogger<Downloader>.Instance);
466+
vardlTask=awaitmanager.StartDownloadAsync(newHttpRequestMessage(HttpMethod.Get,url),destPath,
467+
newTestDownloadValidator(newException("test exception")),ct);
482468
// The "outer" Task should fail because the inner task never starts.
483-
varex=Assert.ThrowsAsync<Exception>(async()=>
484-
{
485-
awaitmanager.StartDownloadAsync(newHttpRequestMessage(HttpMethod.Get,url),destPath,
486-
newTestDownloadValidator(newException("test exception")),ct);
487-
});
469+
varex=Assert.ThrowsAsync<Exception>(async()=>{awaitdlTask.Task;});
488470
Assert.That(ex.Message,Does.Contain("Existing file failed validation"));
489471
Assert.That(ex.InnerException,Is.Not.Null);
490472
Assert.That(ex.InnerException!.Message,Is.EqualTo("test exception"));

‎Vpn.Service/Downloader.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
usingSystem.Formats.Asn1;
44
usingSystem.Net;
55
usingSystem.Runtime.CompilerServices;
6+
usingSystem.Runtime.ExceptionServices;
67
usingSystem.Security.Cryptography;
78
usingSystem.Security.Cryptography.X509Certificates;
89
usingCoder.Desktop.Vpn.Utilities;
@@ -290,7 +291,24 @@ public async Task<DownloadTask> StartDownloadAsync(HttpRequestMessage req, strin
290291
_=>newDownloadTask(_logger,req,destinationPath,validator));
291292
// EnsureStarted is a no-op if we didn't create a new DownloadTask.
292293
// So, we will only remove the destination once for each time we start a new task.
293-
task.EnsureStarted(tsk=>_downloads.TryRemove(destinationPath,out_),ct);
294+
task.EnsureStarted(tsk=>
295+
{
296+
// remove the key first, before checking the exception, to ensure
297+
// we still clean up.
298+
_downloads.TryRemove(destinationPath,out_);
299+
if(tsk.Exception==null)
300+
{
301+
return;
302+
}
303+
304+
if(tsk.Exception.InnerException!=null)
305+
{
306+
ExceptionDispatchInfo.Capture(tsk.Exception.InnerException).Throw();
307+
}
308+
309+
// not sure if this is hittable, but just in case:
310+
throwtsk.Exception;
311+
},ct);
294312

295313
// If the existing (or new) task is for the same URL, return it.
296314
if(task.Request.RequestUri==req.RequestUri)
@@ -363,7 +381,7 @@ internal void EnsureStarted(Action<Task> continuation, CancellationToken ct = de
363381
{
364382
usingvar_=_semaphore.Lock();
365383
if(Task==null!)
366-
Task=Start(ct).ContinueWith(continuation,CancellationToken.None);
384+
Task=Start(ct).ContinueWith(continuation,ct);
367385
}
368386

369387
/// <summary>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp