public void WhenMessageReceivedAndNoHandlerRegistered_PromiseIsNotResolved() { var callbackHandlerStack = new CallbackHandlerStack(); var messageHub = new MessageHub(callbackHandlerStack, routerSocket.Object, registrationSocket.Object, localSocketFactory.Object, scaleOutConfigurationProvider.Object, securityProvider.Object, logger); try { var message = Message.CreateFlowStartMessage(new SimpleMessage()); var callback = CallbackPoint.Create <NullMessage>(); var promise = messageHub.EnqueueRequest(message, callback); var callbackMessage = Message.Create(new NullMessage()).As <Message>(); var nonExistingCallbackKey = -1L; callbackMessage.RegisterCallbackPoint(Guid.NewGuid().ToByteArray(), Guid.NewGuid().ToByteArray(), callback.MessageIdentifiers, nonExistingCallbackKey); receivingSocket.SetupMessageReceived(callbackMessage, ReceiveMessageDelay); // messageHub.Start(); ReceiveMessageCompletionDelay.Sleep(); // Assert.IsFalse(promise.GetResponse().Wait(AsyncOpCompletionDelay)); } finally { messageHub.Stop(); } }
public void EnqueueRequest_RegistersMessageAndExceptionHandlers() { try { messageHub.Start(); var message = Message.CreateFlowStartMessage(new SimpleMessage()); var callback = CallbackPoint.Create <SimpleMessage>(); // messageHub.EnqueueRequest(message, callback); AsyncOpCompletionDelay.Sleep(); // callbackHandlerStack.Verify(m => m.Push(It.IsAny <IPromise>(), It.Is <IEnumerable <MessageIdentifier> >(en => ContainsMessageAndExceptionRegistrations(en))), Times.Once); } finally { messageHub.Stop(); } }
public void WhenMessageReceived_CorrespondingPromiseResultSet() { try { var message = Message.CreateFlowStartMessage(new SimpleMessage()); var callback = CallbackPoint.Create <SimpleMessage>(); var promise = messageHub.EnqueueRequest(message, callback); callbackHandlerStack.Setup(m => m.Pop(It.IsAny <CallbackHandlerKey>())).Returns(promise); // receivingSocket.SetupMessageReceived(message); messageHub.Start(); var response = promise.GetResponse().Result; // Assert.AreEqual(message, response); } finally { messageHub.Stop(); } }
public void EnqueueRequest_SendsMessageWithCallbackSetToThisMessageHub() { try { messageHub.Start(); var message = Message.CreateFlowStartMessage(new SimpleMessage()); var callback = CallbackPoint.Create <SimpleMessage>(); // messageHub.EnqueueRequest(message, callback); AsyncOpCompletionDelay.Sleep(); // Func <IMessage, bool> routerSocketIsReceiver = msg => Unsafe.ArraysEqual(msg.As <Message>().ReceiverNodeIdentity, scaleOutAddress.Identity) && Unsafe.ArraysEqual(msg.As <Message>().ReceiverIdentity, messageHub.ReceiverIdentifier.Identity); routerSocket.WaitUntilMessageSent(routerSocketIsReceiver); } finally { messageHub.Stop(); } }
public void WhenExceptionMessageReceived_PromiseThrowsException() { var callbackHandlerStack = new CallbackHandlerStack(); var messageHub = new MessageHub(callbackHandlerStack, routerSocket.Object, registrationSocket.Object, localSocketFactory.Object, scaleOutConfigurationProvider.Object, securityProvider.Object, logger); try { var message = Message.CreateFlowStartMessage(new SimpleMessage()); var callback = CallbackPoint.Create <NullMessage>(); var promise = messageHub.EnqueueRequest(message, callback); var errorMessage = Guid.NewGuid().ToString(); var exception = Message.Create(new ExceptionMessage { Exception = new Exception(errorMessage) }).As <Message>(); exception.RegisterCallbackPoint(Guid.NewGuid().ToByteArray(), Guid.NewGuid().ToByteArray(), callback.MessageIdentifiers, promise.CallbackKey.Value); receivingSocket.SetupMessageReceived(exception, ReceiveMessageDelay); // messageHub.Start(); ReceiveMessageCompletionDelay.Sleep(); // Assert.Throws <AggregateException>(() => { var _ = promise.GetResponse().Result; }, errorMessage); } finally { messageHub.Stop(); } }
public void WhenResultMessageIsDelivered_PromiseIsDisposedAndItsCallbackIsRemoved() { var callbackHandlerStack = new CallbackHandlerStack(); var messageHub = new MessageHub(callbackHandlerStack, routerSocket.Object, registrationSocket.Object, localSocketFactory.Object, scaleOutConfigurationProvider.Object, securityProvider.Object, logger); try { var message = Message.CreateFlowStartMessage(new SimpleMessage()); var callback = CallbackPoint.Create <NullMessage>(); var promise = messageHub.EnqueueRequest(message, callback); var callbackMessage = Message.Create(new NullMessage()).As <Message>(); callbackMessage.RegisterCallbackPoint(Guid.NewGuid().ToByteArray(), Guid.NewGuid().ToByteArray(), callback.MessageIdentifiers, promise.CallbackKey.Value); receivingSocket.SetupMessageReceived(callbackMessage, ReceiveMessageDelay); // messageHub.Start(); ReceiveMessageCompletionDelay.Sleep(); // Assert.IsNull(callbackHandlerStack.Pop(new CallbackHandlerKey { Version = callback.MessageIdentifiers.Single().Version, Identity = callback.MessageIdentifiers.Single().Identity, Partition = callback.MessageIdentifiers.Single().Partition, CallbackKey = promise.CallbackKey.Value })); } finally { messageHub.Stop(); } }
private static void Main(string[] args) { var builder = new ContainerBuilder(); builder.RegisterModule <MainModule>(); builder.RegisterModule <SecurityModule>(); var container = builder.Build(); //var logger = container.Resolve<ILogger>(); var kino = container.Resolve <kino.kino>(); //kino.SetResolver(new DependencyResolver(container)); kino.Start(); // Needed to let router bind to socket over INPROC. To be fixed by NetMQ in future. var messageHub = kino.GetMessageHub(); messageHub.Start(); WriteLine($"Client is running... {DateTime.Now}"); var runs = 10000; var messageIdentifier = MessageIdentifier.Create <HelloMessage>(); var routesRequest = Message.CreateFlowStartMessage(new RequestMessageExternalRoutesMessage { MessageContract = new MessageContract { Identity = messageIdentifier.Identity, Version = messageIdentifier.Version, Partition = messageIdentifier.Partition } }); //var response = messageHub.EnqueueRequest(routesRequest, CallbackPoint.Create<MessageExternalRoutesMessage>()); //var route = response.GetResponse().Result.GetPayload<MessageExternalRoutesMessage>().Routes.First(); //Thread.Sleep(TimeSpan.FromSeconds(5)); while (true) { var promises = new List <IPromise>(runs); var timer = new Stopwatch(); timer.Start(); for (var i = 0; i < runs; i++) { var request = Message.CreateFlowStartMessage(new HelloMessage { Greeting = Guid.NewGuid().ToString() }); request.TraceOptions = MessageTraceOptions.None; //request.SetReceiverActor(new ReceiverIdentifier(route.NodeIdentity), new ReceiverIdentifier(route.ReceiverIdentity.First())); //request.SetReceiverNode(new ReceiverIdentifier(route.NodeIdentity)); var callbackPoint = CallbackPoint.Create <EhlloMessage>(); promises.Add(messageHub.EnqueueRequest(request, callbackPoint)); } var timeout = TimeSpan.FromMilliseconds(4000); foreach (var promise in promises) { using (promise) { if (promise.GetResponse().Wait(timeout)) { promise.GetResponse().Result.GetPayload <EhlloMessage>(); } else { var fc = ForegroundColor; ForegroundColor = ConsoleColor.Yellow; WriteLine($"{DateTime.UtcNow} Call timed out after {timeout.TotalSeconds} sec."); ForegroundColor = fc; promises.ForEach(p => p.Dispose()); break; } } } timer.Stop(); var messagesPerTest = 2; var performance = (timer.ElapsedMilliseconds > 0) ? ((messagesPerTest * runs) / (double)timer.ElapsedMilliseconds * 1000).ToString("##.00") : "Infinite"; WriteLine($"Done {runs} times in {timer.ElapsedMilliseconds} ms with {performance} msg/sec"); //Thread.Sleep(TimeSpan.FromSeconds(2)); } ReadLine(); messageHub.Stop(); kino.Stop(); container.Dispose(); WriteLine("Client stopped."); }