public WhenEnsuringElapsedTimeoutsDispatched() { now = DateTime.UtcNow; SystemTime.OverrideWith(() => now); sagaStore = new Mock <IStoreSagas>(); eventPublisher = new Mock <IPublishEvents>(); }
public void CanClearOveride() { SystemTime.OverrideWith(() => DateTime.UtcNow.Subtract(TimeSpan.FromDays(1))); SystemTime.ClearOverride(); Assert.InRange(SystemTime.Now, DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(1)), DateTime.UtcNow.Add(TimeSpan.FromMinutes(1))); }
public void MustOverrideWithUtcTime() { var expectedEx = new ArgumentOutOfRangeException("timeRetriever", DateTimeKind.Local, Exceptions.ArgumentNotEqualToValue.FormatWith(DateTimeKind.Utc)); var actualEx = Assert.Throws <ArgumentOutOfRangeException>(() => SystemTime.OverrideWith(() => DateTime.Now)); Assert.Equal(expectedEx.Message, actualEx.Message); }
public void When_overriding_now_it_should_return_overriden_value() { DateTime fix = new DateTime(2019, 9, 19, 1, 1, 1); SystemTime.OverrideWith(() => fix); SystemTime.Now.ShouldBe(fix); }
public void ReturnCurrentSystemTimeIfNotSpecified() { var headers = HeaderCollection.Empty; var now = DateTime.UtcNow; SystemTime.OverrideWith(() => now); Assert.InRange(headers.GetTimestamp().Ticks, now.Ticks - 100, now.Ticks + 100); }
public void ReturnCurrentSystemTimeIfHeaderValueNull() { var header = new Header(Header.Timestamp, null, checkReservedNames: false); var headers = new HeaderCollection(header.ToEnumerable()); var now = DateTime.UtcNow; SystemTime.OverrideWith(() => now); Assert.InRange(headers.GetTimestamp().Ticks, now.Ticks - 100, now.Ticks + 100); }
public void ReturnTrueIfSystemTimeLessThanTimeout() { var now = DateTime.UtcNow; SystemTime.OverrideWith(() => now); var backoff = new ExponentialBackoff(TimeSpan.FromMinutes(1)); Assert.True(backoff.CanRetry); }
public void SetTimestampToSystemTimeIfNotAlreadySet() { var now = DateTime.UtcNow; var messageFactory = new FakeMessageFactory(); SystemTime.OverrideWith(() => now); var message = messageFactory.Create(HeaderCollection.Empty, new Object()); Assert.Equal(now.ToString(DateTimeFormat.RFC1123), message.Headers.GetTimestamp().ToString(DateTimeFormat.RFC1123)); }
public void ReturnFalseIfSystemTimeGreaterThanTimeout() { var now = DateTime.UtcNow; SystemTime.OverrideWith(() => now); var backoff = new ExponentialBackoff(TimeSpan.FromMinutes(1)); SystemTime.OverrideWith(() => now.AddMinutes(2)); Assert.False(backoff.CanRetry); }
public WhenDispatchingCommits() { now = DateTime.UtcNow; sagaStore = new Mock <IStoreSagas>(); eventPublisher = new Mock <IPublishEvents>(); sagaTimeout = new SagaTimeout(typeof(FakeSaga), GuidStrategy.NewGuid(), now.AddMinutes(-5)); SystemTime.OverrideWith(() => now); timeoutDispatcher = new TimeoutDispatcher(new Lazy <IStoreSagas>(() => sagaStore.Object), new Lazy <IPublishEvents>(() => eventPublisher.Object), callback => timer = new FakeTimer(callback)); sagaStore.Setup(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>())).Returns(new[] { sagaTimeout }); }
public void AlwaysSetTimestamp() { var now = DateTime.UtcNow; var messageFactory = new FakeMessageFactory(); var timestamp = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(1)); SystemTime.OverrideWith(() => now); var message = messageFactory.Create(new[] { new Header(Header.Timestamp, timestamp.ToString(DateTimeFormat.RoundTrip), checkReservedNames: false) }, new Object()); Assert.True(message.Headers.GetTimestamp() > timestamp); }
public void ReturnEmptySetIfNoTimeoutsCached() { var now = DateTime.UtcNow; var futureTime = now.AddMinutes(5); var sagaStore = new Mock <IStoreSagas>(); var cache = new SagaTimeoutCache(sagaStore.Object, TimeSpan.FromMinutes(20)); SystemTime.OverrideWith(() => futureTime); sagaStore.Setup(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>())).Returns(new SagaTimeout[0]); Assert.Equal(0, cache.GetElapsedTimeouts().Count()); }
public void RemoveKnownSagaReferences() { var now = DateTime.UtcNow; var sagaStore = new Mock <IStoreSagas>(); var cache = new SagaTimeoutCache(sagaStore.Object, TimeSpan.FromMinutes(5)); var sagaTimeout = new SagaTimeout(typeof(Saga), GuidStrategy.NewGuid(), now.AddMinutes(10)); sagaStore.Setup(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>())).Returns(new[] { sagaTimeout }); SystemTime.OverrideWith(() => now); cache.ClearTimeout(new SagaReference(sagaTimeout.SagaType, sagaTimeout.SagaId)); Assert.Equal(0, cache.Count); }
public void DoNotCacheScheduledTimeoutsIfMaximumCachedTimeoutInFuture() { var now = DateTime.UtcNow; var futureTime = now.AddMinutes(5); var sagaStore = new Mock <IStoreSagas>(); var cache = new SagaTimeoutCache(sagaStore.Object, TimeSpan.FromMinutes(20)); sagaStore.Setup(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>())).Returns(new SagaTimeout[0]); SystemTime.OverrideWith(() => futureTime); cache.GetElapsedTimeouts(); SystemTime.OverrideWith(() => now); cache.GetElapsedTimeouts(); sagaStore.Verify(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>()), Times.Once()); }
public void DoNotCacheTimeoutsFarInFuture() { var now = DateTime.UtcNow; var sagaStore = new Mock <IStoreSagas>(); var cache = new SagaTimeoutCache(sagaStore.Object, TimeSpan.FromMinutes(5)); var sagaTimeout = new SagaTimeout(typeof(Saga), GuidStrategy.NewGuid(), now.AddMinutes(10)); sagaStore.Setup(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>())).Returns(new[] { sagaTimeout }); SystemTime.OverrideWith(() => now); cache.GetElapsedTimeouts(); cache.ScheduleTimeout(sagaTimeout); Assert.Equal(0, cache.Count); }
public void ReturnNextScheduledTimeoutIfCacheHasItems() { var now = DateTime.UtcNow; var nextTimeout = now.AddMinutes(1); var sagaStore = new Mock <IStoreSagas>(); var cache = new SagaTimeoutCache(sagaStore.Object, TimeSpan.FromMinutes(10)); SystemTime.OverrideWith(() => now); sagaStore.Setup(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>())).Returns(new[] { new SagaTimeout(typeof(Saga), GuidStrategy.NewGuid(), nextTimeout), new SagaTimeout(typeof(Saga), GuidStrategy.NewGuid(), nextTimeout.AddMinutes(5)) }); //NOTE: Because we are using an overriden SystemTime; the get will not actually clear out the cached items. Assert.Equal(0, cache.GetElapsedTimeouts().Count()); Assert.Equal(nextTimeout, cache.GetNextScheduledTimeout()); }
public void CanTolerateCachingSameSagaReferenceMoreThanOnce() { var now = DateTime.UtcNow; var futureTime = now.AddMinutes(10); var sagaStore = new Mock <IStoreSagas>(); var cache = new SagaTimeoutCache(sagaStore.Object, TimeSpan.FromMinutes(5)); var sagaTimeout = new SagaTimeout(typeof(Saga), GuidStrategy.NewGuid(), futureTime); sagaStore.Setup(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>())).Returns(new[] { sagaTimeout }); SystemTime.OverrideWith(() => now); cache.GetElapsedTimeouts(); SystemTime.OverrideWith(() => futureTime); cache.GetElapsedTimeouts(); sagaStore.Verify(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>()), Times.Exactly(2)); }
public void ReturnScheduledTimeoutsInPast() { var now = DateTime.UtcNow; var futureTime = now.AddMinutes(5); var sagaStore = new Mock <IStoreSagas>(); var cache = new SagaTimeoutCache(sagaStore.Object, TimeSpan.FromMinutes(20)); SystemTime.OverrideWith(() => futureTime); sagaStore.Setup(mock => mock.GetScheduledTimeouts(It.IsAny <DateTime>())).Returns(new[] { new SagaTimeout(typeof(Saga), GuidStrategy.NewGuid(), now.AddMinutes(1)), new SagaTimeout(typeof(Saga), GuidStrategy.NewGuid(), now.AddMinutes(5)), new SagaTimeout(typeof(Saga), GuidStrategy.NewGuid(), now.AddMinutes(10)) }); var elapsedTimeouts = cache.GetElapsedTimeouts().ToArray(); Assert.Equal(2, elapsedTimeouts.Length); Assert.Equal(now.AddMinutes(1), elapsedTimeouts[0].Timeout); Assert.Equal(now.AddMinutes(5), elapsedTimeouts[1].Timeout); }