public void WhenGettingTemporaryErrorShoudBackoutForFiveMinutes() { var waitHandle = new AutoResetEventAdapter(false); var store = new Mock <IRepository>(); var messageProvider = new Mock <IMessageProvider>(); messageProvider.SetupGet(m => m.CanSend).Returns(true); this.PeriodicBackoutCheck.Setup(p => p.IsTimeElapsed("ForwardService", 1000)) .Returns(false); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 1000, 0, false, this.Logger.Object); forwardService.MessageProvider = messageProvider.Object; forwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(500); forwardService.Stop(); this.HttpClient.Verify(c => c.Post(It.IsAny <IMessage>()), Times.Never()); messageProvider.Verify(m => m.CanSend, Times.AtLeast(1)); messageProvider.Verify(m => m.GetNext(), Times.Never()); messageProvider.Verify(m => m.Delete(It.IsAny <IMessage>()), Times.Never()); messageProvider.Verify(m => m.Close(), Times.Never()); this.PeriodicBackoutCheck.Verify(p => p.IsTimeElapsed("ForwardService", 1000), Times.AtLeast(1)); }
public void WhenMessageProviderCanSendShouldCallClose() { this.HttpClient.Setup(c => c.Post(It.IsAny <IMessage>())) .Returns(Result.Ok); var store = new Mock <IRepository>(); var waitHandle = new AutoResetEventAdapter(false); var messageProvider = new Mock <IMessageProvider>(); messageProvider.SetupGet(m => m.CanSend).Returns(true); messageProvider.Setup(s => s.GetNext()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 1000, 0, false, this.Logger.Object); forwardService.MessageProvider = messageProvider.Object; this.ForwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(500); forwardService.Stop(); messageProvider.Verify(m => m.CanSend, Times.AtLeast(1)); messageProvider.Verify(m => m.GetNext(), Times.AtLeast(1)); messageProvider.Verify(m => m.Delete(It.IsAny <IMessage>()), Times.AtLeast(1)); messageProvider.Verify(m => m.Close(), Times.AtLeast(1)); }
public async void OnNext(KeyValuePair <string, object> pair) { if (pair.Key.Equals("Publish")) { var convert = Func(pair.Value); if (ForwardService is AbstractPublishService publishService) { await publishService.PublishAsync(convert); } else { await ForwardService.ExecuteAsync(convert); } } }
public void WhenThrowsWhileForwardingMessagesShouldNotStopThreadAndContinueSending() { this.HttpClient.Setup(c => c.Post(It.IsAny <IMessage>())) .Returns(Result.Ok); var store = new Mock <IRepository>(); var waitHandle = new AutoResetEventAdapter(false); var messageProvider = new Mock <IMessageProvider>(); messageProvider.SetupGet(m => m.CanSend).Returns(true); messageProvider.Setup(s => s.GetNext()).Returns(this.CreateMessage()); var exception = new InvalidOperationException("Error"); var callCount = 0; messageProvider.Setup(s => s.Delete(It.IsAny <IMessage>())) .Callback(() => { callCount++; if (callCount % 2 == 0) { throw exception; } }); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 1000, 0, false, this.Logger.Object); forwardService.MessageProvider = messageProvider.Object; this.ForwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(3000); forwardService.Stop(); messageProvider.Verify(m => m.CanSend, Times.AtLeast(1)); messageProvider.Verify(m => m.GetNext(), Times.AtLeast(1)); messageProvider.Verify(m => m.Delete(It.IsAny <IMessage>()), Times.AtLeast(1)); messageProvider.Verify(m => m.Close(), Times.AtLeast(1)); this.Logger.Verify(l => l.Err("ForwardService.ThreadStart. Error {0}", exception.ToString()), Times.AtLeast(1)); }
public void WhenSendingMessagesShouldSleepFor1SecondBetweenEachMessage() { this.HttpClient.Setup(c => c.Post(It.IsAny <IMessage>())) .Returns(Result.Ok); var waitHandle = new AutoResetEventAdapter(false); var store = new Mock <IRepository>(); store.Setup(s => s.Get()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 100, 1000, false, this.Logger.Object); forwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(3000); forwardService.Stop(); store.Verify(s => s.Get(), Times.AtLeast(3)); }
public void WhenReceivingTemporaryErrorMessageFromServerAndSleepFor5MinutesThenTerminateSignaledShouldExit() { this.HttpClient.Setup(c => c.Post(It.IsAny <IMessage>())) .Returns(Result.TemporaryError); var waitHandle = new AutoResetEventAdapter(false); var store = new Mock <IRepository>(); store.Setup(s => s.Get()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 5 * 60 * 1000, 0, false, this.Logger.Object); this.PeriodicBackoutCheck.Setup(p => p.IsTimeElapsed("ForwardService", 5 * 60 * 1000)) .Returns(true); forwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; new Thread(this.StopForwardService).Start(forwardService); forwardService.Start(); Thread.Sleep(1000); store.Verify(s => s.Get(), Times.Once()); }
public void WhenReceivingNotConnectedErrorShouldNotSleepAndNotDeletingMessages() { this.HttpClient.Setup(c => c.Post(It.IsAny <IMessage>())) .Returns(Result.NotConnected); var waitHandle = new AutoResetEventAdapter(false); var store = new Mock <IRepository>(); store.Setup(s => s.Get()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 1000, 0, false, this.Logger.Object); this.PeriodicBackoutCheck.Setup(p => p.IsTimeElapsed("ForwardService", 1000)) .Returns(true); forwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(500); forwardService.Stop(); store.Verify(s => s.Get(), Times.Once()); store.Verify(s => s.Remove(), Times.Never()); }
public void WhenSendingMessagesShouldSleepFor1SecondBetweenEachMessage() { this.HttpClient.Setup(c => c.Post(It.IsAny<IMessage>())) .Returns(Result.Ok); var waitHandle = new AutoResetEventAdapter(false); var store = new Mock<IRepository>(); store.Setup(s => s.Get()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 100, 1000, false, this.Logger.Object); forwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(3000); forwardService.Stop(); store.Verify(s => s.Get(), Times.AtLeast(3)); }
public void WhenReceivingNotConnectedErrorShouldNotSleepAndNotDeletingMessages() { this.HttpClient.Setup(c => c.Post(It.IsAny<IMessage>())) .Returns(Result.NotConnected); var waitHandle = new Mock<IWaitHandle>(); var store = new Mock<IRepository>(); store.Setup(s => s.Get()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle.Object, 1000, 0); forwardService.Start(); Thread.Sleep(500); forwardService.Stop(); store.Verify(s => s.Get(), Times.Once()); store.Verify(s => s.Remove(), Times.Never()); }
public void ShouldSetTemporaryErrorMillisecondsTo5Minutes() { var forwardService = new ForwardService(this.PersistentStore, this.HttpClient.Object, this.NetworkStateService.Object, this.WaitHandle, 300000, 0); Assert.Equal(300000, forwardService.TemporaryErrorMilliseconds); }
public void WhenConstructingWithNullNetworkStateParameterShouldNotThrow() { var forwardService = new ForwardService(new Mock <IRepository>().Object, new Mock <IHttpClient>().Object, null, this.WaitHandle, 0, 0, false, this.Logger.Object); Assert.NotNull(forwardService); }
public void WhenGettingTemporaryErrorShoudBackoutForFiveMinutes() { var waitHandle = new AutoResetEventAdapter(false); var store = new Mock<IRepository>(); var messageProvider = new Mock<IMessageProvider>(); messageProvider.SetupGet(m => m.CanSend).Returns(true); this.PeriodicBackoutCheck.Setup(p => p.IsTimeElapsed("ForwardService", 1000)) .Returns(false); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 1000, 0, false, this.Logger.Object); forwardService.MessageProvider = messageProvider.Object; forwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(500); forwardService.Stop(); this.HttpClient.Verify(c => c.Post(It.IsAny<IMessage>()), Times.Never()); messageProvider.Verify(m => m.CanSend, Times.AtLeast(1)); messageProvider.Verify(m => m.GetNext(), Times.Never()); messageProvider.Verify(m => m.Delete(It.IsAny<IMessage>()), Times.Never()); messageProvider.Verify(m => m.Close(), Times.Never()); this.PeriodicBackoutCheck.Verify(p => p.IsTimeElapsed("ForwardService", 1000), Times.AtLeast(1)); }
public void WhenThrowsWhileForwardingMessagesShouldNotStopThreadAndContinueSending() { this.HttpClient.Setup(c => c.Post(It.IsAny<IMessage>())) .Returns(Result.Ok); var store = new Mock<IRepository>(); var waitHandle = new AutoResetEventAdapter(false); var messageProvider = new Mock<IMessageProvider>(); messageProvider.SetupGet(m => m.CanSend).Returns(true); messageProvider.Setup(s => s.GetNext()).Returns(this.CreateMessage()); var exception = new InvalidOperationException("Error"); var callCount = 0; messageProvider.Setup(s => s.Delete(It.IsAny<IMessage>())) .Callback(() => { callCount++; if (callCount % 2 == 0) { throw exception; } }); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 1000, 0, false, this.Logger.Object); forwardService.MessageProvider = messageProvider.Object; this.ForwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(3000); forwardService.Stop(); messageProvider.Verify(m => m.CanSend, Times.AtLeast(1)); messageProvider.Verify(m => m.GetNext(), Times.AtLeast(1)); messageProvider.Verify(m => m.Delete(It.IsAny<IMessage>()), Times.AtLeast(1)); messageProvider.Verify(m => m.Close(), Times.AtLeast(1)); this.Logger.Verify(l => l.Err("ForwardService.ThreadStart. Error {0}", exception.ToString()), Times.AtLeast(1)); }
public void WhenMessageProviderCanSendShouldCallClose() { this.HttpClient.Setup(c => c.Post(It.IsAny<IMessage>())) .Returns(Result.Ok); var store = new Mock<IRepository>(); var waitHandle = new AutoResetEventAdapter(false); var messageProvider = new Mock<IMessageProvider>(); messageProvider.SetupGet(m => m.CanSend).Returns(true); messageProvider.Setup(s => s.GetNext()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 1000, 0, false, this.Logger.Object); forwardService.MessageProvider = messageProvider.Object; this.ForwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(500); forwardService.Stop(); messageProvider.Verify(m => m.CanSend, Times.AtLeast(1)); messageProvider.Verify(m => m.GetNext(), Times.AtLeast(1)); messageProvider.Verify(m => m.Delete(It.IsAny<IMessage>()), Times.AtLeast(1)); messageProvider.Verify(m => m.Close(), Times.AtLeast(1)); }
public void WhenReceivingTemporaryErrorMessageFromServerAndSleepFor5MinutesThenTerminateSignaledShouldExit() { this.HttpClient.Setup(c => c.Post(It.IsAny<IMessage>())) .Returns(Result.TemporaryError); var waitHandle = new AutoResetEventAdapter(false); var store = new Mock<IRepository>(); store.Setup(s => s.Get()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 5 * 60 * 1000, 0, false, this.Logger.Object); this.PeriodicBackoutCheck.Setup(p => p.IsTimeElapsed("ForwardService", 5 * 60 * 1000)) .Returns(true); forwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; new Thread(this.StopForwardService).Start(forwardService); forwardService.Start(); Thread.Sleep(1000); store.Verify(s => s.Get(), Times.Once()); }
public void WhenReceivingNotConnectedErrorShouldNotSleepAndNotDeletingMessages() { this.HttpClient.Setup(c => c.Post(It.IsAny<IMessage>())) .Returns(Result.NotConnected); var waitHandle = new AutoResetEventAdapter(false); var store = new Mock<IRepository>(); store.Setup(s => s.Get()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle, 1000, 0, false, this.Logger.Object); this.PeriodicBackoutCheck.Setup(p => p.IsTimeElapsed("ForwardService", 1000)) .Returns(true); forwardService.PeriodicBackoutCheck = this.PeriodicBackoutCheck.Object; forwardService.Start(); Thread.Sleep(500); forwardService.Stop(); store.Verify(s => s.Get(), Times.Once()); store.Verify(s => s.Remove(), Times.Never()); }
public void WhenSendingMessagesShouldSleepFor1SecondBetweenEachMessage() { this.HttpClient.Setup(c => c.Post(It.IsAny<IMessage>())) .Returns(Result.Ok); var waitHandle = new Mock<IWaitHandle>(); var store = new Mock<IRepository>(); store.Setup(s => s.Get()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle.Object, 1000, 1000); forwardService.Start(); Thread.Sleep(3000); forwardService.Stop(); store.Verify(s => s.Get(), Times.AtMost(3)); }
public void ShouldSetTemporaryErrorMillisecondsTo5Minutes() { var forwardService = new ForwardService(this.PersistentStore, this.HttpClient.Object, this.NetworkStateService.Object, this.WaitHandle, 300000, 0, false, this.Logger.Object); Assert.Equal(300000, forwardService.TemporaryErrorMilliseconds); }
public void ShouldSetSleepMillisecondsTo1Second() { var forwardService = new ForwardService(this.PersistentStore, this.HttpClient.Object, this.NetworkStateService.Object, this.WaitHandle, 300000, 1000); Assert.Equal(1000, forwardService.SleepMilliseconds); }
public void ShouldSetSleepMillisecondsTo1Second() { var forwardService = new ForwardService(this.PersistentStore, this.HttpClient.Object, this.NetworkStateService.Object, this.WaitHandle, 300000, 1000, false, this.Logger.Object); Assert.Equal(1000, forwardService.SleepMilliseconds); }
public void WhenReceivingTemporaryErrorMessageFromServerAndSleepFor5MinutesThenTerminateSignaledShouldExit() { this.HttpClient.Setup(c => c.Post(It.IsAny<IMessage>())) .Returns(Result.TemporaryError); var waitHandle = new Mock<IWaitHandle>(); var store = new Mock<IRepository>(); store.Setup(s => s.Get()).Returns(this.CreateMessage()); var forwardService = new ForwardService(store.Object, this.HttpClient.Object, this.NetworkStateService.Object, waitHandle.Object, 5 * 60 * 1000, 0); new Thread(this.StopForwardService).Start(forwardService); forwardService.Start(); Thread.Sleep(1000); store.Verify(s => s.Get(), Times.Once()); }
public void WhenConstructingWithNullNetworkStateParameterShouldNotThrow() { var forwardService = new ForwardService(new Mock<IRepository>().Object, new Mock<IHttpClient>().Object, null, this.WaitHandle, 0, 0, false, this.Logger.Object); Assert.NotNull(forwardService); }