Skip to content

Commit

Permalink
Replace ISystemClock with TimeProvider in ResponseCaching
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Apr 17, 2023
1 parent ab46872 commit d55d2a3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 58 deletions.
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
5 changes: 1 addition & 4 deletions src/Middleware/ResponseCaching/src/ResponseCachingOptions.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.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.

Original file line number Diff line number Diff line change
Expand Up @@ -356,44 +356,44 @@ public void ContentIsNotModified_IfNoneMatch_MatchesAtLeastOneValue_True()
[Fact]
public void StartResponsegAsync_IfAllowResponseCaptureIsTrue_SetsResponseTime()
{
var clock = new TestClock
var time = new TestTime
{
UtcNow = DateTimeOffset.UtcNow
};
var middleware = TestUtils.CreateTestMiddleware(options: new ResponseCachingOptions
{
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 ResponseCachingOptions
{
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 Down Expand Up @@ -448,19 +448,19 @@ public void FinalizeCacheHeadersAsync_DefaultResponseValidity_Is10Seconds()
[Fact]
public void FinalizeCacheHeadersAsync_ResponseValidity_UseExpiryIfAvailable()
{
var clock = new TestClock
var time = new TestTime
{
UtcNow = DateTimeOffset.MinValue
};
var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, options: new ResponseCachingOptions
{
SystemClock = clock
Time = time
});
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 @@ -471,24 +471,24 @@ public void FinalizeCacheHeadersAsync_ResponseValidity_UseExpiryIfAvailable()
[Fact]
public void FinalizeCacheHeadersAsync_ResponseValidity_UseMaxAgeIfAvailable()
{
var clock = new TestClock
var time = new TestTime
{
UtcNow = DateTimeOffset.UtcNow
};
var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, options: new ResponseCachingOptions
{
SystemClock = clock
Time = time
});
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 @@ -499,24 +499,24 @@ 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 middleware = TestUtils.CreateTestMiddleware(testSink: sink, options: new ResponseCachingOptions
{
SystemClock = clock
Time = time
});
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/ResponseCaching/test/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ internal static IResponseCachingKeyProvider CreateTestKeyProvider(ResponseCachin
{
responseCachingOptions.MaximumBodySize = options.MaximumBodySize;
responseCachingOptions.UseCaseSensitivePaths = options.UseCaseSensitivePaths;
responseCachingOptions.SystemClock = options.SystemClock;
responseCachingOptions.Time = options.Time;
}
});
})
Expand Down Expand Up @@ -390,7 +390,9 @@ public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
}
}

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

public override DateTimeOffset GetUtcNow() => UtcNow;
}

0 comments on commit d55d2a3

Please sign in to comment.