Skip to content

Commit

Permalink
Add tests of AssetUpload cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
KatoStoelen authored and shiftkey committed Feb 13, 2021
1 parent dcce25e commit 093a30c
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Octokit.Tests/Clients/ReleasesClientTests.cs
@@ -1,7 +1,9 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Internal;
using Xunit;

namespace Octokit.Tests.Clients
Expand Down Expand Up @@ -484,6 +486,42 @@ public async Task OverrideDefaultTimeout()

apiConnection.Received().Post<ReleaseAsset>(Arg.Any<Uri>(), uploadData.RawData, Arg.Any<string>(), uploadData.ContentType, newTimeout);
}

[Fact]
public async Task CanBeCancelled()
{
var httpClient = new CancellationTestHttpClient();
var connection = new Connection(new ProductHeaderValue("TEST"), httpClient);
var apiConnection = new ApiConnection(connection);

var fixture = new ReleasesClient(apiConnection);

var release = new Release("https://uploads.github.com/anything");
var uploadData = new ReleaseAssetUpload("good", "good/good", Stream.Null, null);

using (var cts = new CancellationTokenSource())
{
var uploadTask = fixture.UploadAsset(release, uploadData, cts.Token);

cts.Cancel();

await Assert.ThrowsAsync<TaskCanceledException>(() => uploadTask);
}
}

private class CancellationTestHttpClient : IHttpClient
{
public async Task<IResponse> Send(IRequest request, CancellationToken cancellationToken)
{
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);

throw new Exception("HTTP operation was not cancelled");
}

public void Dispose() { }

public void SetRequestTimeout(TimeSpan timeout) { }
}
}

public class TheGetAssetMethod
Expand Down

0 comments on commit 093a30c

Please sign in to comment.