Skip to content

Commit 31b43f6

Browse files
committed
chore: change temp file I/O to not overlap
1 parent 08002ef commit 31b43f6

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

Tests.Vpn.Service/DownloaderTest.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,23 +442,28 @@ public async Task CancelledWaitingForOther(CancellationToken ct)
442442
[CancelAfter(30_000)]
443443
public async Task CancelledInner(CancellationToken ct)
444444
{
445+
var httpCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
446+
var taskCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
445447
using var httpServer = new TestHttpServer(async ctx =>
446448
{
447449
ctx.Response.StatusCode = 200;
448-
await ctx.Response.OutputStream.WriteAsync("test"u8.ToArray(), ct);
449-
await ctx.Response.OutputStream.FlushAsync(ct);
450-
await Task.Delay(TimeSpan.FromSeconds(5), ct);
450+
await ctx.Response.OutputStream.WriteAsync("test"u8.ToArray(), httpCts.Token);
451+
await ctx.Response.OutputStream.FlushAsync(httpCts.Token);
452+
// wait up to 5 seconds.
453+
await Task.Delay(TimeSpan.FromSeconds(5), httpCts.Token);
451454
});
452455
var url = new Uri(httpServer.BaseUrl + "/test");
453456
var destPath = Path.Combine(_tempDir, "test");
454457

455458
var manager = new Downloader(NullLogger<Downloader>.Instance);
456459
// The "inner" Task should fail.
457-
var smallerCt = new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token;
460+
var taskCt = taskCts.Token;
458461
var dlTask = await manager.StartDownloadAsync(new HttpRequestMessage(HttpMethod.Get, url), destPath,
459-
NullDownloadValidator.Instance, smallerCt);
462+
NullDownloadValidator.Instance, taskCt);
463+
await taskCts.CancelAsync();
460464
var ex = Assert.ThrowsAsync<TaskCanceledException>(async () => await dlTask.Task);
461-
Assert.That(ex.CancellationToken, Is.EqualTo(smallerCt));
465+
Assert.That(ex.CancellationToken, Is.EqualTo(taskCt));
466+
await httpCts.CancelAsync();
462467
}
463468

464469
[Test(Description = "Validation failure")]

Vpn.Service/Downloader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ private async Task Download(HttpResponseMessage res, CancellationToken ct)
465465
FileStream tempFile;
466466
try
467467
{
468-
tempFile = File.Create(TempDestinationPath, BufferSize,
469-
FileOptions.Asynchronous | FileOptions.SequentialScan);
468+
tempFile = File.Create(TempDestinationPath, BufferSize, FileOptions.SequentialScan);
470469
}
471470
catch (Exception e)
472471
{

0 commit comments

Comments
 (0)