Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private async Task InvokeAwaited(HttpContext httpContext, IReadOnlyList<IOutputC

var executed = false;

if (context.AllowLocking)
if (context.AllowLocking && !string.IsNullOrEmpty(context.CacheKey))
{
var cacheEntry = await _requestDispatcher.ScheduleAsync(context.CacheKey, key => ExecuteResponseAsync());

Expand Down
48 changes: 48 additions & 0 deletions src/Middleware/OutputCaching/test/OutputCacheMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,54 @@ public async Task Locking_ExecuteAllRequestsWhenDisabled()
Assert.Equal(2, responseCounter);
}

[Fact]
public async Task Locking_InvalidCacheKey_DoesNotCoalesceRequests()
{
var responseCounter = 0;

var blocker1 = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var blocker2 = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

var options = new OutputCacheOptions();
options.AddBasePolicy(build => build.Cache());

var middleware = TestUtils.CreateTestMiddleware(
options: options,
keyProvider: new TestResponseCachingKeyProvider(string.Empty),
next: async c =>
{
var index = Interlocked.Increment(ref responseCounter);
if (index == 1)
{
blocker1.SetResult(true);
await blocker2.Task;
Comment thread
Youssef1313 marked this conversation as resolved.
}
else if (index == 2)
{
blocker2.SetResult(true);
}
});

var context1 = TestUtils.CreateTestContext();
context1.HttpContext.Request.Method = "GET";
context1.HttpContext.Request.Path = "/a";

var context2 = TestUtils.CreateTestContext();
context2.HttpContext.Request.Method = "GET";
context2.HttpContext.Request.Path = "/b";

var task1 = Task.Run(() => middleware.Invoke(context1.HttpContext));

// Wait for the first request to enter the pipeline.
await blocker1.Task;

var task2 = Task.Run(() => middleware.Invoke(context2.HttpContext));

await Task.WhenAll(task1, task2);

Assert.Equal(2, responseCounter);
}

[Fact]
public async Task EmptyCacheKey_IsNotCached()
{
Expand Down
Loading