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 ForEach(CallbackPoint callback, int detalization = 100, bool linearInterpolation = true) { UpdateBoundingBox(); var startFrom = MinX; var limit = MaxX; var previousPoint = 0; var previousX = 0d; for (var i = 0; i <= detalization; i++) { var x = detalization == 0 ? limit : (limit - startFrom) * i / detalization + startFrom; for (var j = previousPoint; j < Count; j++) { var p = this[j]; if (p.X > x) { previousPoint = j > 0 ? j - 1 : 0; break; } if ((i == 0 || p.X > previousX) && p.X < x) { callback(p.X, p.Y); } } callback(x, linearInterpolation ? InterpolateLinear(x) : InterpolateCubic(x)); previousX = x; } }
private static void CreateLeaseProviderInstances(IEnumerable <string> instances, IMessageHub messageHub, byte[] partition) { if (instances.Any()) { var results = new List <CreateLeaseProviderInstanceResponseMessage>(); foreach (var instance in instances) { var message = Message.CreateFlowStartMessage(new CreateLeaseProviderInstanceRequestMessage { Instance = instance, Partition = partition }); //message.TraceOptions = MessageTraceOptions.Routing; var callback = new CallbackPoint(MessageIdentifier.Create <CreateLeaseProviderInstanceResponseMessage>(partition)); using (var promise = messageHub.Send(message, callback)) { results.Add(promise.GetResponse().Result.GetPayload <CreateLeaseProviderInstanceResponseMessage>()); } } var activationWaitTime = results.Max(r => r.ActivationWaitTime); Console.WriteLine($"Waiting {activationWaitTime.TotalSeconds} sec before LeaseProvider Instances are active..."); if (activationWaitTime > TimeSpan.Zero) { Thread.Sleep(activationWaitTime); } } }
private void MatchMessageAgainstCallbackPoint() { if (CallbackPoint.Any(identifier => Unsafe.ArraysEqual(Identity, identifier.Identity) && Version == identifier.Version && Unsafe.ArraysEqual(Partition, identifier.Partition))) { ReceiverIdentity = CallbackReceiverIdentity; ReceiverNodeIdentity = CallbackReceiverNodeIdentity; } }
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 <KinoModule>(); var container = builder.Build(); var messageRouter = container.Resolve <IMessageRouter>(); messageRouter.Start(); // Needed to let router bind to socket over INPROC. To be fixed by NetMQ in future. Thread.Sleep(TimeSpan.FromMilliseconds(30)); var messageHub = container.Resolve <IMessageHub>(); messageHub.Start(); // Host Actors var actorHostManager = container.Resolve <IActorHostManager>(); foreach (var actor in container.Resolve <IEnumerable <IActor> >()) { actorHostManager.AssignActor(actor); } Thread.Sleep(TimeSpan.FromSeconds(5)); Console.WriteLine($"Client is running... {DateTime.Now}"); var instances = Enumerable.Range(0, 1).Select(i => i.ToString()).ToArray(); var rnd = new Random(DateTime.UtcNow.Millisecond); var partition = "test".GetBytes(); CreateLeaseProviderInstances(instances, messageHub, partition); var run = 0; var ownerIdentity = Guid.NewGuid().ToByteArray(); while (true) { var leaseTimeSpan = TimeSpan.FromSeconds(5); var request = Message.CreateFlowStartMessage(new LeaseRequestMessage { Instance = instances[rnd.Next(0, instances.Length - 1)], LeaseTimeSpan = leaseTimeSpan, Requestor = new Node { Identity = ownerIdentity, Uri = "tpc://localhost" }, Partition = partition, MinValidityTimeFraction = 3 }); request.TraceOptions = MessageTraceOptions.None; var callbackPoint = new CallbackPoint(MessageIdentifier.Create <LeaseResponseMessage>(partition)); var promise = messageHub.Send(request, callbackPoint); var waitTimeout = TimeSpan.FromMilliseconds(500); if (promise.GetResponse().Wait(waitTimeout)) { var response = promise.GetResponse().Result.GetPayload <LeaseResponseMessage>(); if (response.LeaseAcquired) { Console.WriteLine($"{DateTime.UtcNow} " + $"Acquired: {response.LeaseAcquired} " + $"Instance: {response.Lease.Instance} " + $"Owner: {response.Lease.Owner.Uri} " + $"OwnerIdentity: {response.Lease.Owner.Identity.GetAnyString()} " + $"RequestorIdentity: {ownerIdentity.GetString()} " + $"ExpiresAt: {new DateTime(response.Lease.ExpiresAt, DateTimeKind.Utc)}"); leaseTimeSpan.DivideBy(2).Sleep(); } else { Console.WriteLine("Not a Leader"); } } else { Console.WriteLine($"Call timed out after {waitTimeout.TotalSeconds} sec."); } TimeSpan.FromSeconds(2).Sleep(); } Console.ReadLine(); messageHub.Stop(); messageRouter.Stop(); container.Dispose(); Console.WriteLine("Client stopped."); }
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."); }