Example #1
0
            public void CanAddFirstTimeoutForSaga()
            {
                var timeout = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);

                collection.Add(timeout);

                Assert.True(collection.Contains(timeout));
            }
            public void CanAddFirstTimeoutForSaga()
            {
                var timeout = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);

                collection.Add(timeout);

                Assert.True(collection.Contains(timeout));
            }
            public void NotEqualIfSagaIdsDiffer()
            {
                var now = DateTime.UtcNow;
                var lhs = new SagaTimeout(typeof(Saga1), Guid.NewGuid(), now);
                var rhs = new SagaTimeout(typeof(Saga1), Guid.NewGuid(), now);

                Assert.NotEqual(lhs, rhs);
            }
Example #4
0
            public void NotEqualIfSagaIdsDiffer()
            {
                var now = DateTime.UtcNow;
                var lhs = new SagaTimeout(typeof(Saga1), Guid.NewGuid(), now);
                var rhs = new SagaTimeout(typeof(Saga1), Guid.NewGuid(), now);

                Assert.NotEqual(lhs, rhs);
            }
Example #5
0
            public void NotEqualIfTimeoutsDiffer()
            {
                var now    = DateTime.UtcNow;
                var sagaId = Guid.NewGuid();
                var lhs    = new SagaTimeout(typeof(Saga1), sagaId, now);
                var rhs    = new SagaTimeout(typeof(Saga1), sagaId, now.AddMilliseconds(1));

                Assert.NotEqual(lhs, rhs);
            }
Example #6
0
            public void EqualIfSameSagaTypeAndIdAndTimeout()
            {
                var now    = DateTime.UtcNow;
                var sagaId = Guid.NewGuid();
                var lhs    = new SagaTimeout(typeof(Saga1), sagaId, now);
                var rhs    = new SagaTimeout(typeof(Saga1), sagaId, now);

                Assert.Equal(lhs, rhs);
            }
            public void NotEqualIfTimeoutsDiffer()
            {
                var now = DateTime.UtcNow;
                var sagaId = Guid.NewGuid();
                var lhs = new SagaTimeout(typeof(Saga1), sagaId, now);
                var rhs = new SagaTimeout(typeof(Saga1), sagaId, now.AddMilliseconds(1));

                Assert.NotEqual(lhs, rhs);
            }
            public void EqualIfSameSagaTypeAndIdAndTimeout()
            {
                var now = DateTime.UtcNow;
                var sagaId = Guid.NewGuid();
                var lhs = new SagaTimeout(typeof(Saga1), sagaId, now);
                var rhs = new SagaTimeout(typeof(Saga1), sagaId, now);

                Assert.Equal(lhs, rhs);
            }
Example #9
0
        public Task Handle(StartTimeoutSaga message, IMessageHandlerContext context)
        {
            var timeout = new SagaTimeout
            {
                OriginatingSagaType = GetType().Name
            };

            log.Warn("Saga started. Sending Timeout");
            return(RequestTimeout(context, TimeSpan.FromSeconds(10), timeout));
        }
            public void CanAddTimeoutsForMultipleSagas()
            {
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow.AddSeconds(1));

                collection.Add(timeout1);
                collection.Add(timeout2);

                Assert.True(collection.Contains(timeout1));
                Assert.True(collection.Contains(timeout2));
            }
Example #11
0
            public void CanAddTimeoutsForMultipleSagas()
            {
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow.AddSeconds(1));

                collection.Add(timeout1);
                collection.Add(timeout2);

                Assert.True(collection.Contains(timeout1));
                Assert.True(collection.Contains(timeout2));
            }
Example #12
0
            public void DoNotRemoveSagaTimeoutsForAnotherSagaReference()
            {
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);

                collection.Add(timeout1);
                collection.Add(timeout2);
                collection.Remove((SagaReference)timeout2);

                Assert.True(collection.Contains(timeout1));
                Assert.False(collection.Contains(timeout2));
            }
            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 });
            }
Example #14
0
            public void RemoveSagaReferenceIfLastSagaTimeout()
            {
                var now      = DateTime.UtcNow;
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), now);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), now);

                collection.Add(timeout1);
                collection.Add(timeout2);

                Assert.True(collection.Remove(timeout2));
                Assert.False(collection.Contains((SagaReference)timeout2));
            }
            public void CanAddSubsequentTimeoutsForSameSaga()
            {
                var sagaType = typeof(Saga);
                var sagaId = Guid.NewGuid();
                var timeout1 = new SagaTimeout(sagaType, sagaId, DateTime.UtcNow);
                var timeout2 = new SagaTimeout(sagaType, sagaId, DateTime.UtcNow.AddSeconds(2));

                collection.Add(timeout1);
                collection.Add(timeout2);

                Assert.True(collection.Contains(timeout1));
                Assert.True(collection.Contains(timeout2));
            }
            public void CanAddTwoIdenticalTimeouts()
            {
                var now = DateTime.UtcNow;
                var sagaType = typeof(Saga);
                var sagaId = Guid.NewGuid();
                var timeout1 = new SagaTimeout(sagaType, sagaId, now);
                var timeout2 = new SagaTimeout(sagaType, sagaId, now);

                collection.Add(timeout1);
                collection.Add(timeout2);

                Assert.Equal(2, collection.Count);
            }
Example #17
0
            public void CanAddSubsequentTimeoutsForSameSaga()
            {
                var sagaType = typeof(Saga);
                var sagaId   = Guid.NewGuid();
                var timeout1 = new SagaTimeout(sagaType, sagaId, DateTime.UtcNow);
                var timeout2 = new SagaTimeout(sagaType, sagaId, DateTime.UtcNow.AddSeconds(2));

                collection.Add(timeout1);
                collection.Add(timeout2);

                Assert.True(collection.Contains(timeout1));
                Assert.True(collection.Contains(timeout2));
            }
Example #18
0
            public void CanAddTwoIdenticalTimeouts()
            {
                var now      = DateTime.UtcNow;
                var sagaType = typeof(Saga);
                var sagaId   = Guid.NewGuid();
                var timeout1 = new SagaTimeout(sagaType, sagaId, now);
                var timeout2 = new SagaTimeout(sagaType, sagaId, now);

                collection.Add(timeout1);
                collection.Add(timeout2);

                Assert.Equal(2, collection.Count);
            }
Example #19
0
            public void RemoveAllSagaTimeoutsForSpecifiedSagaReference()
            {
                var sagaReference = new SagaReference(typeof(Saga), Guid.NewGuid());
                var timeout1      = new SagaTimeout(typeof(Saga), sagaReference.SagaId, DateTime.UtcNow);
                var timeout2      = new SagaTimeout(typeof(Saga), sagaReference.SagaId, DateTime.UtcNow.AddSeconds(1));

                collection.Add(timeout1);
                collection.Add(timeout2);
                collection.Remove(sagaReference);

                Assert.False(collection.Contains(timeout1));
                Assert.False(collection.Contains(timeout2));
                Assert.False(collection.Contains(sagaReference));
            }
            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 OnPostSave()
            {
                now = DateTime.UtcNow;
                sagaStore = new Mock<IStoreSagas>();
                eventPublisher = new Mock<IPublishEvents>();
                timeoutDispatcher = new TimeoutDispatcher(new Lazy<IStoreSagas>(() => sagaStore.Object), new Lazy<IPublishEvents>(() => eventPublisher.Object), callback => timer = new FakeTimer(callback));
                sagaTimeout = new SagaTimeout(typeof(FakeSaga), GuidStrategy.NewGuid(), now.AddMinutes(5));

                SystemTime.OverrideWith(() => now);

                sagaStore.Setup(mock => mock.GetScheduledTimeouts(It.IsAny<DateTime>())).Returns(new[] { sagaTimeout });

                timer.InvokeCallback();
                timer.Reset();
            }
Example #22
0
            public void RemoveAllInstanceOfSagaTimeoutWhenRequested()
            {
                var now      = DateTime.UtcNow;
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), now);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), now);
                var timeout3 = new SagaTimeout(typeof(Saga), timeout2.SagaId, timeout2.Timeout);
                var timeout4 = new SagaTimeout(typeof(Saga), timeout2.SagaId, now.AddMilliseconds(1));

                collection.Add(timeout1);
                collection.Add(timeout2);
                collection.Add(timeout3);
                collection.Add(timeout4);

                Assert.True(collection.RemoveAll(timeout3));
                Assert.False(collection.Contains(timeout2));
            }
            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 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 AddTimeoutsInChronologicalOrder()
            {
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow.AddSeconds(1));
                var timeout3 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow.AddSeconds(1));
                var timeout4 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow.AddMinutes(1));

                collection.Add(timeout4);
                collection.Add(timeout2);
                collection.Add(timeout1);
                collection.Add(timeout3);

                var items = collection.ToArray();

                Assert.Equal(timeout1, items[0]);
                Assert.Equal(timeout2, items[1]);
                Assert.Equal(timeout3, items[2]);
                Assert.Equal(timeout4, items[3]);
            }
Example #26
0
            public void AddTimeoutsInChronologicalOrder()
            {
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow.AddSeconds(1));
                var timeout3 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow.AddSeconds(1));
                var timeout4 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow.AddMinutes(1));

                collection.Add(timeout4);
                collection.Add(timeout2);
                collection.Add(timeout1);
                collection.Add(timeout3);

                var items = collection.ToArray();

                Assert.Equal(timeout1, items[0]);
                Assert.Equal(timeout2, items[1]);
                Assert.Equal(timeout3, items[2]);
                Assert.Equal(timeout4, items[3]);
            }
 public Task Timeout(SagaTimeout message, IMessageHandlerContext context)
 {
     // should not be called.
     return(Task.CompletedTask);
 }
 public Task Timeout(SagaTimeout state, IMessageHandlerContext context)
 {
     log.Info("Timeout received");
     return(Task.CompletedTask);
 }
            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 Task Timeout(SagaTimeout message, IMessageHandlerContext context)
 {
     testContext.SagaTimeoutReceived = true;
     return(Task.CompletedTask);
 }
            public void RemoveAllInstanceOfSagaTimeoutWhenRequested()
            {
                var now = DateTime.UtcNow;
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), now);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), now);
                var timeout3 = new SagaTimeout(typeof(Saga), timeout2.SagaId, timeout2.Timeout);
                var timeout4 = new SagaTimeout(typeof(Saga), timeout2.SagaId, now.AddMilliseconds(1));

                collection.Add(timeout1);
                collection.Add(timeout2);
                collection.Add(timeout3);
                collection.Add(timeout4);

                Assert.True(collection.RemoveAll(timeout3));
                Assert.False(collection.Contains(timeout2));
            }
            public void RemoveSagaReferenceIfLastSagaTimeout()
            {
                var now = DateTime.UtcNow;
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), now);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), now);

                collection.Add(timeout1);
                collection.Add(timeout2);

                Assert.True(collection.Remove(timeout2));
                Assert.False(collection.Contains((SagaReference)timeout2));
            }
            public void DoNotRemoveAnySagaTimeoutsIfSagaReferenceNotFound()
            {
                var timeout = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);

                Assert.False(collection.Remove(timeout));
            }
            public void DoNotRemoveSagaTimeoutsForAnotherSagaReference()
            {
                var timeout1 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);
                var timeout2 = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);

                collection.Add(timeout1);
                collection.Add(timeout2);
                collection.Remove((SagaReference)timeout2);

                Assert.True(collection.Contains(timeout1));
                Assert.False(collection.Contains(timeout2));
            }
            public void RemoveAllSagaTimeoutsForSpecifiedSagaReference()
            {
                var sagaReference = new SagaReference(typeof(Saga), Guid.NewGuid());
                var timeout1 = new SagaTimeout(typeof(Saga), sagaReference.SagaId, DateTime.UtcNow);
                var timeout2 = new SagaTimeout(typeof(Saga), sagaReference.SagaId, DateTime.UtcNow.AddSeconds(1));

                collection.Add(timeout1);
                collection.Add(timeout2);
                collection.Remove(sagaReference);

                Assert.False(collection.Contains(timeout1));
                Assert.False(collection.Contains(timeout2));
                Assert.False(collection.Contains(sagaReference));
            }
Example #36
0
 public Task Timeout(SagaTimeout state, IMessageHandlerContext context)
 {
     log.Warn($"Received Timeout from {state.OriginatingSagaType}");
     MarkAsComplete();
     return(Task.FromResult(0));
 }
Example #37
0
            public void DoNotRemoveAnySagaTimeoutsIfSagaReferenceNotFound()
            {
                var timeout = new SagaTimeout(typeof(Saga), Guid.NewGuid(), DateTime.UtcNow);

                Assert.False(collection.Remove(timeout));
            }
        /// <summary>
        /// Schedule the specified <paramref name="sagaTimeout"/> (thread-safe).
        /// </summary>
        /// <param name="sagaTimeout">The saga timeout to schedule.</param>
        public void ScheduleTimeout(SagaTimeout sagaTimeout)
        {
            Log.Trace("Scheduling saga timeout for {0}", sagaTimeout);

            lock (syncLock)
            {
                ScheduleTimeoutInternal(sagaTimeout);
            }

            Log.Trace("Saga timeout scheduled for {0}", sagaTimeout);
        }
 /// <summary>
 /// Schedule the specified <paramref name="sagaTimeout"/>.
 /// </summary>
 /// <param name="sagaTimeout">The saga timeout to schedule.</param>
 private void ScheduleTimeoutInternal(SagaTimeout sagaTimeout)
 {
     if (sagaTimeout.Timeout < maximumCachedTimeout)
         sagaTimeouts.Add(sagaTimeout);
 }
            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);
            }
Example #41
0
 public Task Timeout(SagaTimeout state, IMessageHandlerContext context)
 {
     // throw only for sample purposes
     throw new Exception("Expected Timeout in MyTimeoutSagaVersion2. EndpointVersion1 may have been incorrectly started.");
 }
 public Task Timeout(SagaTimeout state, IMessageHandlerContext context)
 {
     context.Logger().Information("Timeout received");
     return(Task.CompletedTask);
 }
            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);
            }