public void Many_publish_request_should_work() { const int repeatCount = 5000; LocalBus.SubscribeHandler <PingMessage>(x => LocalBus.Context().Respond(new PongMessage())); ManualResetEvent completed = new ManualResetEvent(false); int responsesReceived = 0; Stopwatch stopwatch = Stopwatch.StartNew(); for (int i = 0; i < repeatCount; i++) { LocalBus.PublishRequestAsync(new PingMessage(), x => { x.Handle <PongMessage>(message => { if (Interlocked.Increment(ref responsesReceived) == repeatCount) { completed.Set(); } }); }); } bool success = completed.WaitOne(60.Seconds()); Assert.IsTrue(success, "The repeat series did not complete"); Trace.WriteLine(string.Format("Elapsed Time for {0} requests = {1}", repeatCount, stopwatch.Elapsed)); Trace.WriteLine(string.Format("Requests Per Second = {0}", repeatCount * 1000 / stopwatch.ElapsedMilliseconds)); Trace.WriteLine(string.Format("Elapsed Time for {0} messages = {1}", repeatCount * 2, stopwatch.Elapsed)); Trace.WriteLine(string.Format("Messages Per Second = {0}", repeatCount * 2000 / stopwatch.ElapsedMilliseconds)); }
public void Should_not_complete_timeout_if_handler_completes() { var pongReceived = new FutureMessage <PongMessage>(); var continueCalled = new FutureMessage <Task <PongMessage> >(); var timeoutCalled = new FutureMessage <PingMessage>(); TimeSpan timeout = 8.Seconds(); var ping = new PingMessage(); ITaskRequest <PingMessage> request = LocalBus.PublishRequestAsync(ping, x => { x.HandleTimeout(4.Seconds(), timeoutCalled.Set); x.Handle <PongMessage>(pongReceived.Set) .ContinueWith(continueCalled.Set); }); pongReceived.IsAvailable(timeout).ShouldBeTrue("The pong was not received"); request.Task.Wait(timeout).ShouldBeTrue("Task was not completed"); request.GetResponseTask <PongMessage>().Wait(timeout).ShouldBeTrue("The response task was not completed"); continueCalled.IsAvailable(timeout).ShouldBeTrue("The continuation was not called"); timeoutCalled.IsAvailable(2.Seconds()).ShouldBeFalse("The timeout should not have been called"); }
public void Should_call_timeout_callback_if_timeout_occurs() { var pongCompleted = new FutureMessage <PongMessage>(); var pongCancelled = new FutureMessage <bool>(); Task <PongMessage> pongTask; var ping = new PingMessage(); ITaskRequest <PingMessage> request = LocalBus.PublishRequestAsync(ping, x => { x.SetTimeout(1.Seconds()); pongTask = x.Handle <PongMessage>(message => { }); pongTask.ContinueWith(t => pongCompleted.Set(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); pongTask.ContinueWith((Task t) => pongCancelled.Set(t.IsCanceled), TaskContinuationOptions.OnlyOnCanceled); }); var aggregateException = Assert.Throws <AggregateException>(() => request.Task.Wait(8.Seconds())); Assert.IsInstanceOf <RequestTimeoutException>(aggregateException.InnerExceptions.First()); pongCompleted.IsAvailable(1.Seconds()).ShouldBeFalse("We only asked to be notified on success"); pongCancelled.IsAvailable(1.Seconds()).ShouldBeTrue("We like to know we were cancelled due to timeout"); }
public void Should_throw_an_exception_from_the_timeout() { var ping = new PingMessage(); ITaskRequest <PingMessage> request = LocalBus.PublishRequestAsync(ping, x => { x.SetTimeout(1.Seconds()); }); var aggregateException = Assert.Throws <AggregateException>(() => request.Task.Wait(8.Seconds())); Assert.IsInstanceOf <RequestTimeoutException>(aggregateException.InnerExceptions.First()); }
public void Should_call_timeout_callback_if_timeout_occurs() { var ping = new Messages.PingMessage(); ITaskRequest <Messages.PingMessage> request = LocalBus.PublishRequestAsync(ping, x => { // x.SetTimeout(1.Seconds()); }); var aggregateException = Assert.Throws <AggregateException>(() => request.Task.Wait(8.Seconds())); Assert.IsInstanceOf <RequestTimeoutException>(aggregateException.InnerExceptions.First()); }
public void Should_call_timeout_callback_if_timeout_occurs_and_not_fault() { var continueCalled = new FutureMessage <PingMessage>(); var ping = new PingMessage(); ITaskRequest <PingMessage> request = LocalBus.PublishRequestAsync(ping, x => { // x.HandleTimeout(1.Seconds(), continueCalled.Set); }); request.Task.Wait(8.Seconds()).ShouldBeTrue("Should have completed successfully"); continueCalled.IsAvailable(8.Seconds()).ShouldBeTrue("The timeout continuation was not called"); }