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

feat: allow cancelation of download of a different URL#71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
spikecurtis merged 1 commit intomainfromspike/cancel-different-download
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletionsTests.Vpn.Service/DownloaderTest.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -412,6 +412,32 @@ public async Task MismatchedETag(CancellationToken ct)
Assert.That(ex.Message, Does.Contain("ETag does not match SHA1 hash of downloaded file").And.Contains("beef"));
}

[Test(Description = "Timeout waiting for existing download")]
[CancelAfter(30_000)]
public async Task CancelledWaitingForOther(CancellationToken ct)
{
var testCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
using var httpServer = new TestHttpServer(async _ =>
{
await Task.Delay(TimeSpan.FromSeconds(5), testCts.Token);
});
var url0 = new Uri(httpServer.BaseUrl + "/test0");
var url1 = new Uri(httpServer.BaseUrl + "/test1");
var destPath = Path.Combine(_tempDir, "test");
var manager = new Downloader(NullLogger<Downloader>.Instance);

// first outer task succeeds, getting download started
var dlTask0 = await manager.StartDownloadAsync(new HttpRequestMessage(HttpMethod.Get, url0), destPath,
NullDownloadValidator.Instance, testCts.Token);

// The second request fails if the timeout is short
var smallerCt = new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token;
Assert.ThrowsAsync<TaskCanceledException>(async () => await manager.StartDownloadAsync(
new HttpRequestMessage(HttpMethod.Get, url1), destPath,
NullDownloadValidator.Instance, smallerCt));
await testCts.CancelAsync();
}

[Test(Description = "Timeout on response body")]
[CancelAfter(30_000)]
public async Task CancelledInner(CancellationToken ct)
Expand Down
18 changes: 17 additions & 1 deletionVpn.Service/Downloader.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -287,6 +287,7 @@ public async Task<DownloadTask> StartDownloadAsync(HttpRequestMessage req, strin
{
while (true)
{
ct.ThrowIfCancellationRequested();
var task = _downloads.GetOrAdd(destinationPath,
_ => new DownloadTask(_logger, req, destinationPath, validator));
// EnsureStarted is a no-op if we didn't create a new DownloadTask.
Expand DownExpand Up@@ -322,7 +323,22 @@ public async Task<DownloadTask> StartDownloadAsync(HttpRequestMessage req, strin
_logger.LogWarning(
"Download for '{DestinationPath}' is already in progress, but is for a different Url - awaiting completion",
destinationPath);
await task.Task;
await TaskOrCancellation(task.Task, ct);
}
}

/// <summary>
/// TaskOrCancellation waits for either the task to complete, or the given token to be canceled.
/// </summary>
internal static async Task TaskOrCancellation(Task task, CancellationToken cancellationToken)
{
var cancellationTask = new TaskCompletionSource();
await using (cancellationToken.Register(() => cancellationTask.TrySetCanceled()))
{
// Wait for either the task or the cancellation
var completedTask = await Task.WhenAny(task, cancellationTask.Task);
// Await to propagate exceptions, if any
await completedTask;
}
}
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp