Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: spike/69-download-different-url-same-dest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 deletions Tests.Vpn.Service/DownloaderTest.cs
Original file line number Diff line number Diff 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 deletion Vpn.Service/Downloader.cs
Original file line number Diff line number Diff 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 Down Expand 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