Skip to content

Commit

Permalink
Remove usage of TaskCache (#1889)
Browse files Browse the repository at this point in the history
* Remove usage of TaskCache
  • Loading branch information
pranavkm committed Jun 8, 2017
1 parent c6df7eb commit 3f771ef
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.Internal.System;
using Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines;
using Microsoft.AspNetCore.Server.Kestrel.Internal.System.Text.Encodings.Web.Utf8;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.AspNetCore.Http.Features;

// ReSharper disable AccessToModifiedClosure

Expand Down Expand Up @@ -526,7 +525,7 @@ public Task WriteAsync(ArraySegment<byte> data, CancellationToken cancellationTo
{
if (data.Count == 0)
{
return TaskCache.CompletedTask;
return Task.CompletedTask;
}
return WriteChunkedAsync(data, cancellationToken);
}
Expand All @@ -539,7 +538,7 @@ public Task WriteAsync(ArraySegment<byte> data, CancellationToken cancellationTo
else
{
HandleNonBodyResponseWrite();
return TaskCache.CompletedTask;
return Task.CompletedTask;
}
}

Expand Down Expand Up @@ -665,7 +664,7 @@ public Task InitializeResponse(int firstWriteByteCount)
{
if (HasResponseStarted)
{
return TaskCache.CompletedTask;
return Task.CompletedTask;
}

if (_onStarting != null)
Expand All @@ -681,7 +680,7 @@ public Task InitializeResponse(int firstWriteByteCount)
VerifyAndUpdateWrite(firstWriteByteCount);
ProduceStart(appCompleted: false);

return TaskCache.CompletedTask;
return Task.CompletedTask;
}

private async Task InitializeResponseAwaited(int firstWriteByteCount)
Expand Down Expand Up @@ -716,7 +715,7 @@ protected Task TryProduceInvalidRequestResponse()
return ProduceEnd();
}

return TaskCache.CompletedTask;
return Task.CompletedTask;
}

protected Task ProduceEnd()
Expand All @@ -727,7 +726,7 @@ protected Task ProduceEnd()
{
// We can no longer change the response, so we simply close the connection.
_requestProcessingStopping = true;
return TaskCache.CompletedTask;
return Task.CompletedTask;
}

// If the request was rejected, the error state has already been set by SetBadRequestState and
Expand Down Expand Up @@ -780,7 +779,7 @@ private Task WriteSuffix()
Log.ConnectionHeadResponseBodyWrite(ConnectionId, _responseBytesWritten);
}

return TaskCache.CompletedTask;
return Task.CompletedTask;
}

private async Task WriteAutoChunkSuffixAwaited()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

using System;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions;
using Microsoft.Extensions.Internal;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
{
Expand Down Expand Up @@ -42,7 +40,7 @@ public override void Flush()
public override Task FlushAsync(CancellationToken cancellationToken)
{
// No-op.
return TaskCache.CompletedTask;
return Task.CompletedTask;
}

public override long Seek(long offset, SeekOrigin origin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines;
using Microsoft.Extensions.Internal;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
{
Expand Down Expand Up @@ -110,7 +109,7 @@ public void Abort()
{
if (_completed)
{
return TaskCache.CompletedTask;
return Task.CompletedTask;
}

writableBuffer = _pipe.Writer.Alloc(1);
Expand Down Expand Up @@ -143,7 +142,7 @@ public void Abort()
if (awaitable.IsCompleted)
{
// The flush task can't fail today
return TaskCache.CompletedTask;
return Task.CompletedTask;
}
return FlushAsyncAwaited(awaitable, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<PackageReference Include="Microsoft.Net.Http.Headers" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.TaskCache.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(CoreFxVersion)" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Internal;

namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal
{
internal class ClosedStream : Stream
{
private static readonly Task<int> ZeroResultTask = Task.FromResult(result: 0);

public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
Expand Down Expand Up @@ -56,7 +57,7 @@ public override int Read(byte[] buffer, int offset, int count)

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return TaskCache<int>.DefaultCompletedTask;
return ZeroResultTask;
}

public override void Write(byte[] buffer, int offset, int count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.TaskCache.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.TaskCache.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Moq;
Expand Down Expand Up @@ -240,7 +239,7 @@ public async Task ThrowsWhenOnStartingIsSetAfterResponseStarted()

// Act/Assert
Assert.True(_frame.HasResponseStarted);
Assert.Throws<InvalidOperationException>(() => ((IHttpResponseFeature)_frame).OnStarting(_ => TaskCache.CompletedTask, null));
Assert.Throws<InvalidOperationException>(() => ((IHttpResponseFeature)_frame).OnStarting(_ => Task.CompletedTask, null));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
Expand Down Expand Up @@ -161,7 +161,7 @@ private static KestrelServer CreateServer(KestrelServerOptions options, ILogger

private static void StartDummyApplication(IServer server)
{
server.StartAsync(new DummyApplication(context => TaskCache.CompletedTask), CancellationToken.None).GetAwaiter().GetResult();
server.StartAsync(new DummyApplication(context => Task.CompletedTask), CancellationToken.None).GetAwaiter().GetResult();
}

private class MockTransportFactory : ITransportFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
Expand Down Expand Up @@ -398,7 +394,7 @@ public async Task CopyToAsyncDoesNotCopyBlocks(FrameRequestHeaders headers, stri
writeTcs.SetResult(buffer);
writeCount++;
})
.Returns(TaskCache.CompletedTask);
.Returns(Task.CompletedTask);

using (var input = new TestInput())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Microsoft.Extensions.Internal;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests.TestHelpers
{
public class MockFrameControl : IFrameControl
{
public Task FlushAsync(CancellationToken cancellationToken)
{
return TaskCache.CompletedTask;
return Task.CompletedTask;
}

public void ProduceContinue()
Expand All @@ -22,7 +21,7 @@ public void ProduceContinue()

public Task WriteAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
{
return TaskCache.CompletedTask;
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.Internal.System.IO.Pipelines;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Internal;
using Moq;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
{
Expand Down Expand Up @@ -82,12 +77,12 @@ public void Write(ArraySegment<byte> data, Action<Exception, object> callback, o

Task IFrameControl.WriteAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
{
return TaskCache.CompletedTask;
return Task.CompletedTask;
}

Task IFrameControl.FlushAsync(CancellationToken cancellationToken)
{
return TaskCache.CompletedTask;
return Task.CompletedTask;
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;
Expand Down Expand Up @@ -181,7 +180,7 @@ private async Task TestBadRequest(string request, string expectedResponseStatusC
.Setup(trace => trace.ConnectionBadRequest(It.IsAny<string>(), It.IsAny<BadHttpRequestException>()))
.Callback<string, BadHttpRequestException>((connectionId, exception) => loggedException = exception);

using (var server = new TestServer(context => TaskCache.CompletedTask, new TestServiceContext { Log = mockKestrelTrace.Object }))
using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext { Log = mockKestrelTrace.Object }))
{
using (var connection = server.CreateConnection())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Internal;
using Xunit;

namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
Expand Down Expand Up @@ -538,7 +537,7 @@ public async Task ChunkedNotFinalTransferCodingResultsIn400(ListenOptions listen
var testContext = new TestServiceContext();
using (var server = new TestServer(httpContext =>
{
return TaskCache.CompletedTask;
return Task.CompletedTask;
}, testContext, listenOptions))
{
using (var connection = server.CreateConnection())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Internal;
using Xunit;

namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
Expand All @@ -18,7 +17,7 @@ public async Task TestDefaultHeaders()
ServerOptions = { AddServerHeader = true }
};

using (var server = new TestServer(ctx => TaskCache.CompletedTask, testContext))
using (var server = new TestServer(ctx => Task.CompletedTask, testContext))
{
using (var connection = server.CreateConnection())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Internal;
using Xunit;

namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
Expand Down Expand Up @@ -126,7 +125,7 @@ public async Task UsesProvidedServerCertificate()
}
};

using (var server = new TestServer(context => TaskCache.CompletedTask, serviceContext, listenOptions))
using (var server = new TestServer(context => Task.CompletedTask, serviceContext, listenOptions))
{
using (var client = new TcpClient())
{
Expand Down Expand Up @@ -256,7 +255,7 @@ public async Task ClientCertificateValidationGetsCalledWithNotNullParameters(Cli
}
};

using (var server = new TestServer(context => TaskCache.CompletedTask, serviceContext, listenOptions))
using (var server = new TestServer(context => Task.CompletedTask, serviceContext, listenOptions))
{
using (var client = new TcpClient())
{
Expand Down Expand Up @@ -287,7 +286,7 @@ public async Task ValidationFailureRejectsConnection(ClientCertificateMode mode)
}
};

using (var server = new TestServer(context => TaskCache.CompletedTask, serviceContext, listenOptions))
using (var server = new TestServer(context => Task.CompletedTask, serviceContext, listenOptions))
{
using (var client = new TcpClient())
{
Expand Down Expand Up @@ -316,7 +315,7 @@ public async Task RejectsConnectionOnSslPolicyErrorsWhenNoValidation(ClientCerti
}
};

using (var server = new TestServer(context => TaskCache.CompletedTask, serviceContext, listenOptions))
using (var server = new TestServer(context => Task.CompletedTask, serviceContext, listenOptions))
{
using (var client = new TcpClient())
{
Expand Down

0 comments on commit 3f771ef

Please sign in to comment.