Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ISystemClock with TimeProvider in OutputCaching, ResponseCaching #47753

Merged
merged 4 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 0 additions & 15 deletions src/Middleware/OutputCaching/src/ISystemClock.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ internal async Task<bool> TryServeCachedResponseAsync(OutputCacheContext context
}

context.CachedResponse = cacheEntry;
context.ResponseTime = _options.SystemClock.UtcNow;
context.ResponseTime = _options.Time.GetUtcNow();
var cacheEntryAge = context.ResponseTime.Value - context.CachedResponse.Created;
context.CachedEntryAge = cacheEntryAge > TimeSpan.Zero ? cacheEntryAge : TimeSpan.Zero;

Expand Down Expand Up @@ -464,7 +464,7 @@ private bool OnStartResponse(OutputCacheContext context)
if (!context.ResponseStarted)
{
context.ResponseStarted = true;
context.ResponseTime = _options.SystemClock.UtcNow;
context.ResponseTime = _options.Time.GetUtcNow();

return true;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Middleware/OutputCaching/src/OutputCacheOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;

namespace Microsoft.AspNetCore.OutputCaching;

/// <summary>
Expand Down Expand Up @@ -45,8 +43,7 @@ public class OutputCacheOptions
/// <summary>
/// For testing purposes only.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal ISystemClock SystemClock { get; set; } = new SystemClock();
internal TimeProvider Time { get; set; } = TimeProvider.System;
Tratcher marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Defines a <see cref="IOutputCachePolicy"/> which can be referenced by name.
Expand Down
15 changes: 0 additions & 15 deletions src/Middleware/OutputCaching/src/SystemClock.cs

This file was deleted.

40 changes: 20 additions & 20 deletions src/Middleware/OutputCaching/test/OutputCacheMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,44 +334,44 @@ public void ContentIsNotModified_IfNoneMatch_MatchesAtLeastOneValue_True()
[Fact]
public void StartResponseAsync_IfAllowResponseCaptureIsTrue_SetsResponseTime()
{
var clock = new TestClock
var time = new TestTime
Tratcher marked this conversation as resolved.
Show resolved Hide resolved
{
UtcNow = DateTimeOffset.UtcNow
};
var middleware = TestUtils.CreateTestMiddleware(options: new OutputCacheOptions
{
SystemClock = clock
Time = time
});
var context = TestUtils.CreateTestContext();
context.ResponseTime = null;

middleware.StartResponse(context);

Assert.Equal(clock.UtcNow, context.ResponseTime);
Assert.Equal(time.UtcNow, context.ResponseTime);
}

[Fact]
public void StartResponseAsync_IfAllowResponseCaptureIsTrue_SetsResponseTimeOnlyOnce()
{
var clock = new TestClock
var time = new TestTime
{
UtcNow = DateTimeOffset.UtcNow
};
var middleware = TestUtils.CreateTestMiddleware(options: new OutputCacheOptions
{
SystemClock = clock
Time = time
});
var context = TestUtils.CreateTestContext();
var initialTime = clock.UtcNow;
var initialTime = time.UtcNow;
context.ResponseTime = null;

middleware.StartResponse(context);
Assert.Equal(initialTime, context.ResponseTime);

clock.UtcNow += TimeSpan.FromSeconds(10);
time.UtcNow += TimeSpan.FromSeconds(10);

middleware.StartResponse(context);
Assert.NotEqual(clock.UtcNow, context.ResponseTime);
Assert.NotEqual(time.UtcNow, context.ResponseTime);
Assert.Equal(initialTime, context.ResponseTime);
}

Expand All @@ -393,20 +393,20 @@ public void FinalizeCacheHeadersAsync_ResponseValidity_IgnoresExpiryIfAvailable(
{
// The Expires header should not be used when set in the response

var clock = new TestClock
var time = new TestTime
{
UtcNow = DateTimeOffset.MinValue
};
var options = new OutputCacheOptions
{
SystemClock = clock
Time = time
};
var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, options: options);
var context = TestUtils.CreateTestContext();

context.ResponseTime = clock.UtcNow;
context.HttpContext.Response.Headers.Expires = HeaderUtilities.FormatDate(clock.UtcNow + TimeSpan.FromSeconds(11));
context.ResponseTime = time.UtcNow;
context.HttpContext.Response.Headers.Expires = HeaderUtilities.FormatDate(time.UtcNow + TimeSpan.FromSeconds(11));

middleware.FinalizeCacheHeaders(context);

Expand All @@ -419,25 +419,25 @@ public void FinalizeCacheHeadersAsync_ResponseValidity_UseMaxAgeIfAvailable()
{
// The MaxAge header should not be used if set in the response

var clock = new TestClock
var time = new TestTime
{
UtcNow = DateTimeOffset.UtcNow
};
var sink = new TestSink();
var options = new OutputCacheOptions
{
SystemClock = clock
Time = time
};
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, options: options);
var context = TestUtils.CreateTestContext();

context.ResponseTime = clock.UtcNow;
context.ResponseTime = time.UtcNow;
context.HttpContext.Response.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = TimeSpan.FromSeconds(12)
}.ToString();

context.HttpContext.Response.Headers.Expires = HeaderUtilities.FormatDate(clock.UtcNow + TimeSpan.FromSeconds(11));
context.HttpContext.Response.Headers.Expires = HeaderUtilities.FormatDate(time.UtcNow + TimeSpan.FromSeconds(11));

middleware.FinalizeCacheHeaders(context);

Expand All @@ -448,25 +448,25 @@ public void FinalizeCacheHeadersAsync_ResponseValidity_UseMaxAgeIfAvailable()
[Fact]
public void FinalizeCacheHeadersAsync_ResponseValidity_UseSharedMaxAgeIfAvailable()
{
var clock = new TestClock
var time = new TestTime
{
UtcNow = DateTimeOffset.UtcNow
};
var sink = new TestSink();
var options = new OutputCacheOptions
{
SystemClock = clock
Time = time
};
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, options: options);
var context = TestUtils.CreateTestContext();

context.ResponseTime = clock.UtcNow;
context.ResponseTime = time.UtcNow;
context.HttpContext.Response.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = TimeSpan.FromSeconds(12),
SharedMaxAge = TimeSpan.FromSeconds(13)
}.ToString();
context.HttpContext.Response.Headers.Expires = HeaderUtilities.FormatDate(clock.UtcNow + TimeSpan.FromSeconds(11));
context.HttpContext.Response.Headers.Expires = HeaderUtilities.FormatDate(time.UtcNow + TimeSpan.FromSeconds(11));

middleware.FinalizeCacheHeaders(context);

Expand Down
6 changes: 4 additions & 2 deletions src/Middleware/OutputCaching/test/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private static IEnumerable<IHostBuilder> CreateBuildersWithOutputCaching(
{
outputCachingOptions.MaximumBodySize = options.MaximumBodySize;
outputCachingOptions.UseCaseSensitivePaths = options.UseCaseSensitivePaths;
outputCachingOptions.SystemClock = options.SystemClock;
outputCachingOptions.Time = options.Time;
outputCachingOptions.BasePolicies = options.BasePolicies;
outputCachingOptions.DefaultExpirationTimeSpan = options.DefaultExpirationTimeSpan;
outputCachingOptions.SizeLimit = options.SizeLimit;
Expand Down Expand Up @@ -347,9 +347,11 @@ public ValueTask SetAsync(string key, byte[] entry, string[]? tags, TimeSpan val
}
}

internal class TestClock : ISystemClock
internal class TestTime : TimeProvider
{
public DateTimeOffset UtcNow { get; set; }

public override DateTimeOffset GetUtcNow() => UtcNow;
}

internal class AllowTestPolicy : IOutputCachePolicy
Expand Down
15 changes: 0 additions & 15 deletions src/Middleware/ResponseCaching/src/Interfaces/ISystemClock.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ internal async Task<bool> TryServeCachedResponseAsync(ResponseCachingContext con

context.CachedResponse = cachedResponse;
context.CachedResponseHeaders = cachedResponse.Headers;
context.ResponseTime = _options.SystemClock.UtcNow;
context.ResponseTime = _options.Time.GetUtcNow();
var cachedEntryAge = context.ResponseTime.Value - context.CachedResponse.Created;
context.CachedEntryAge = cachedEntryAge > TimeSpan.Zero ? cachedEntryAge : TimeSpan.Zero;

Expand Down Expand Up @@ -374,7 +374,7 @@ private bool OnStartResponse(ResponseCachingContext context)
if (!context.ResponseStarted)
{
context.ResponseStarted = true;
context.ResponseTime = _options.SystemClock.UtcNow;
context.ResponseTime = _options.Time.GetUtcNow();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;

namespace Microsoft.AspNetCore.ResponseCaching;

/// <summary>
Expand Down Expand Up @@ -31,6 +29,5 @@ public class ResponseCachingOptions
/// <summary>
/// For testing purposes only.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal ISystemClock SystemClock { get; set; } = new SystemClock();
internal TimeProvider Time { get; set; } = TimeProvider.System;
}
15 changes: 0 additions & 15 deletions src/Middleware/ResponseCaching/src/SystemClock.cs

This file was deleted.

Loading