Skip to content
Closed
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
13 changes: 7 additions & 6 deletions src/Docker.DotNet/Endpoints/StreamUtil.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Text;
Expand Down Expand Up @@ -32,7 +31,7 @@ internal static async Task MonitorStreamForMessagesAsync<T>(Task<Stream> streamT
var tcs = new TaskCompletionSource<bool>();

using (var stream = await streamTask)
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
using (var reader = new StreamReader(stream, new UTF8Encoding(false), false))
using (var jsonReader = new JsonTextReader(reader) { SupportMultipleContent = true })
using (cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken)))
{
Expand All @@ -44,12 +43,14 @@ internal static async Task MonitorStreamForMessagesAsync<T>(Task<Stream> streamT
}
}

internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseMessage> responseTask, DockerClient client, CancellationToken cancel, IProgress<T> progress)
internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseMessage> responseTask, DockerClient client, CancellationToken cancellationToken, IProgress<T> progress)
{
using (var response = await responseTask)
using (var response = await responseTask
.ConfigureAwait(false))
{
await MonitorStreamForMessagesAsync<T>(response.Content.ReadAsStreamAsync(), client, cancel, progress);
await MonitorStreamForMessagesAsync(response.Content.ReadAsStreamAsync(), client, cancellationToken, progress)
.ConfigureAwait(false);
}
}
}
}
}
42 changes: 25 additions & 17 deletions test/Docker.DotNet.Tests/IContainerOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Docker.DotNet.Tests
{
[Collection(nameof(TestCollection))]
public class IContainerOperationsTests
public sealed class IContainerOperationsTests : IDisposable
{
private readonly CancellationTokenSource _cts;

Expand Down Expand Up @@ -107,6 +107,8 @@ await _dockerClient.Containers.StartContainerAsync(
_cts.Token
);

await Task.Delay(TimeSpan.FromSeconds(5), default);

containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
Expand Down Expand Up @@ -154,6 +156,8 @@ await _dockerClient.Containers.StartContainerAsync(
_cts.Token
);

await Task.Delay(TimeSpan.FromSeconds(5), default);

containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
Expand Down Expand Up @@ -203,6 +207,8 @@ await _dockerClient.Containers.StartContainerAsync(
_cts.Token
);

await Task.Delay(TimeSpan.FromSeconds(5), default);

containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
Expand All @@ -215,11 +221,9 @@ await _dockerClient.Containers.StartContainerAsync(
Follow = false
},
containerLogsCts.Token,
new Progress<string>(m => { _output.WriteLine(m); logList.Add(m); })
new Progress<string>(m => { logList.Add(m); _output.WriteLine(m); })
);

await Task.Delay(TimeSpan.FromSeconds(5));

await _dockerClient.Containers.StopContainerAsync(
createContainerResponse.ID,
new ContainerStopParameters(),
Expand Down Expand Up @@ -253,9 +257,11 @@ await _dockerClient.Containers.StartContainerAsync(
_cts.Token
);

await Task.Delay(TimeSpan.FromSeconds(5), default);

containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

await Assert.ThrowsAsync<TaskCanceledException>(() => _dockerClient.Containers.GetContainerLogsAsync(
var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
createContainerResponse.ID,
new ContainerLogsParameters
{
Expand All @@ -266,7 +272,9 @@ await Assert.ThrowsAsync<TaskCanceledException>(() => _dockerClient.Containers.G
},
containerLogsCts.Token,
new Progress<string>(m => _output.WriteLine(m))
));
);

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

[Fact]
Expand All @@ -290,6 +298,8 @@ await _dockerClient.Containers.StartContainerAsync(
_cts.Token
);

await Task.Delay(TimeSpan.FromSeconds(5), default);

containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
Expand Down Expand Up @@ -330,6 +340,8 @@ await _dockerClient.Containers.StartContainerAsync(
_cts.Token
);

await Task.Delay(TimeSpan.FromSeconds(5), default);

containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
Expand All @@ -342,18 +354,9 @@ await _dockerClient.Containers.StartContainerAsync(
Follow = true
},
containerLogsCts.Token,
new Progress<string>(m => { _output.WriteLine(m); logList.Add(m); })
);

await Task.Delay(TimeSpan.FromSeconds(5));

await _dockerClient.Containers.StopContainerAsync(
createContainerResponse.ID,
new ContainerStopParameters(),
_cts.Token
new Progress<string>(m => { logList.Add(m); _output.WriteLine(m); })
);


await Assert.ThrowsAsync<TaskCanceledException>(() => containerLogsTask);
_output.WriteLine($"Line count: {logList.Count}");

Expand Down Expand Up @@ -808,5 +811,10 @@ public async Task MultiplexedStreamWriteAsync_DoesNotThrowAnException()
// Then
Assert.Null(exception);
}

public void Dispose()
{
_cts.Dispose();
}
}
}
}
11 changes: 8 additions & 3 deletions test/Docker.DotNet.Tests/IImageOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Docker.DotNet.Tests
{
[Collection(nameof(TestCollection))]
public class IImageOperationsTests
public sealed class IImageOperationsTests : IDisposable
{
private readonly CancellationTokenSource _cts;

Expand All @@ -34,7 +34,7 @@ public IImageOperationsTests(TestFixture testFixture, ITestOutputHelper outputHe
}

[Fact]
public async Task CreateImageAsync_TaskCancelled_ThowsTaskCanceledException()
public async Task CreateImageAsync_TaskCancelled_ThrowsTaskCanceledException()
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token);

Expand Down Expand Up @@ -114,5 +114,10 @@ await _dockerClient.Images.DeleteImageAsync(
Assert.NotNull(inspectExistingImageResponse);
await Assert.ThrowsAsync<DockerImageNotFoundException>(() => inspectDeletedImageTask);
}

public void Dispose()
{
_cts.Dispose();
}
}
}
}
Loading