Skip to content

Commit

Permalink
Aligned TimeProvider usage with actual .NET 8 behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcelhanon committed Feb 13, 2024
1 parent 6ddc3d5 commit 3eba10e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ protected override void Load(ContainerBuilder builder)
.As<IEdFiOdsConnectionStringWriter>()
.SingleInstance();

builder.RegisterType<TimeProvider>()
.AsSelf()
builder.RegisterInstance(TimeProvider.System)
.SingleInstance();

RegisterPipeLineStepProviders();
Expand Down
2 changes: 1 addition & 1 deletion Application/EdFi.Ods.Common/Configuration/CacheSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CacheSettings
internal ExternalCacheProviderOption ExternalCacheProviderOption
{
get => Enum.TryParse<ExternalCacheProviderOption>(ExternalCacheProvider, ignoreCase: true, out var parsed)
? parsed : ExternalCacheProviderOption.Undetermined;
? parsed : default;
}

public DescriptorsCacheConfiguration Descriptors { get; set; } = new();
Expand Down
6 changes: 5 additions & 1 deletion Application/EdFi.Ods.Common/Utils/TimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ namespace EdFi.Ods.Common.Utils;
// This class will be obsolete with .NET 8 and should be replaced.
public class TimeProvider
{
public virtual DateTime GetUtcNow() => DateTime.UtcNow;
protected TimeProvider(){}

public virtual DateTimeOffset GetUtcNow() => DateTimeOffset.UtcNow;

public static TimeProvider System { get; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void Receive(string messageContent)
{
// Always proceed the first time
evaluation.Proceed = true;
return evaluation.TimeProvider.GetUtcNow();
return evaluation.TimeProvider.GetUtcNow().UtcDateTime;
},
static (t, lastNotification, evaluation) =>
{
Expand All @@ -95,7 +95,7 @@ public void Receive(string messageContent)
}

evaluation.Proceed = true;
return evaluation.TimeProvider.GetUtcNow();
return evaluation.TimeProvider.GetUtcNow().UtcDateTime;
},
intervalEvaluation);

Expand Down
9 changes: 2 additions & 7 deletions tests/EdFi.Ods.Features.UnitTests/FakeTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class FakeTimeProvider : TimeProvider

public FakeTimeProvider()
{
_dateTimeOffset = DateTimeOffset.Now;
_dateTimeOffset = DateTimeOffset.Parse("2000-01-01");
}

public FakeTimeProvider(DateTimeOffset dateTimeOffset)
Expand All @@ -27,10 +27,5 @@ public void Advance(TimeSpan amount)
_dateTimeOffset = _dateTimeOffset.Add(amount);
}

public void SetUtcNow(DateTimeOffset dateTimeOffset)
{
_dateTimeOffset = dateTimeOffset;
}

public override DateTime GetUtcNow() => _dateTimeOffset.DateTime;
public override DateTimeOffset GetUtcNow() => _dateTimeOffset;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Receive_UnrecognizedMessageType_LogsDebug()
{
// Arrange
var mediator = A.Fake<IMediator>();
var sink = new NotificationsMessageSink(mediator, new Dictionary<string, TimeSpan>(), new TimeProvider());
var sink = new NotificationsMessageSink(mediator, new Dictionary<string, TimeSpan>(), TimeProvider.System);

// Act
sink.Receive(@"{""Type"":""non-attributed-test-notification"",""Data"":{}}");
Expand All @@ -63,7 +63,7 @@ public void Receive_RecognizedMessageType_PublishesNotification()
{
// Arrange
var mediator = A.Fake<IMediator>();
var sink = new NotificationsMessageSink(mediator, new Dictionary<string, TimeSpan>(), new TimeProvider());
var sink = new NotificationsMessageSink(mediator, new Dictionary<string, TimeSpan>(), TimeProvider.System);

var messageContent = @"{""Type"":""test-notification"",""Data"":{""Property"":""Value""}}";

Expand All @@ -79,7 +79,7 @@ public void Receive_InvalidMessageFormat_LogsError()
{
// Arrange
var mediator = A.Fake<IMediator>();
var sink = new NotificationsMessageSink(mediator, new Dictionary<string, TimeSpan>(), new TimeProvider());
var sink = new NotificationsMessageSink(mediator, new Dictionary<string, TimeSpan>(), TimeProvider.System);

var invalidMessageContent = "InvalidMessageContent";

Expand All @@ -98,7 +98,7 @@ public void Receive_ValidMessageFormat_LogsDebug()
{
// Arrange
var mediator = A.Fake<IMediator>();
var sink = new NotificationsMessageSink(mediator, new Dictionary<string, TimeSpan>(), new TimeProvider());
var sink = new NotificationsMessageSink(mediator, new Dictionary<string, TimeSpan>(), TimeProvider.System);

var validMessageContent = @"{""Type"":""test-notification"",""Data"":{""Property"":""Value""}}";

Expand All @@ -117,7 +117,7 @@ public void Receive_WithConfiguredMinimumInterval_PublishesNotificationOncePerIn
{
// Arrange
var mediator = A.Fake<IMediator>();
var fakeTimeProvider = new FakeTimeProvider();
var fakeTimeProvider = new FakeTimeProvider(DateTimeOffset.UtcNow);
var intervalByNotificationType = new Dictionary<string, TimeSpan>
{
{ "test-notification", TimeSpan.FromSeconds(5) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Should_subscribe_to_and_receive_Redis_pub_sub_notification_and_invok
var activity = new InitializeRedisNotifications(
_redisConnectionProvider,
_redisNotificationSettings,
new NotificationsMessageSink(mediator, _intervalsByNotificationType, new TimeProvider()));
new NotificationsMessageSink(mediator, _intervalsByNotificationType, TimeProvider.System));

activity.Execute();

Expand Down

0 comments on commit 3eba10e

Please sign in to comment.