コード例 #1
0
        public void IfSeveralActorsRegisteredToHandleTheMessage_TheAreFoundInRoundRobinManner()
        {
            var messageIdentifier = MessageIdentifier.Create <SimpleMessage>();
            var registrations     = EnumerableExtensions.Produce(Randomizer.Int32(6, 16),
                                                                 () => new InternalRouteRegistration
            {
                ReceiverIdentifier = ReceiverIdentities.CreateForActor(),
                DestinationSocket  = new LocalSocket <IMessage>(),
                MessageContracts   = new[] { new MessageContract {
                                                 Message = messageIdentifier
                                             } }
            });

            registrations.ForEach(r => internalRoutingTable.AddMessageRoute(r));
            //
            foreach (var registration in registrations)
            {
                var socket = internalRoutingTable.FindRoutes(new InternalRouteLookupRequest
                {
                    Message = messageIdentifier
                })
                             .First();
                var localSocket = registration.DestinationSocket;
                //
                Assert.AreEqual(localSocket, socket);
            }
        }
コード例 #2
0
        public void AddMessageRoute_AddsActorRoute()
        {
            var actor             = ReceiverIdentities.CreateForActor();
            var localSocket       = new LocalSocket <IMessage>();
            var messageIdentifier = MessageIdentifier.Create <SimpleMessage>();
            var registration      = new InternalRouteRegistration
            {
                ReceiverIdentifier = actor,
                DestinationSocket  = localSocket,
                MessageContracts   = new[] { new MessageContract {
                                                 Message = messageIdentifier
                                             } }
            };

            //
            internalRoutingTable.AddMessageRoute(registration);
            //
            var lookupRequest = new InternalRouteLookupRequest
            {
                ReceiverIdentity = actor,
                Message          = messageIdentifier
            };
            var socket = internalRoutingTable.FindRoutes(lookupRequest).First();

            Assert.AreEqual(localSocket, socket);
        }
コード例 #3
0
        public void FindRoutesByActorReceiverIdentifier_ReturnsActorHostSocket()
        {
            var messageIdentifier = MessageIdentifier.Create <SimpleMessage>();
            var registrations     = EnumerableExtensions.Produce(Randomizer.Int32(3, 6),
                                                                 () => new InternalRouteRegistration
            {
                ReceiverIdentifier = ReceiverIdentities.CreateForActor(),
                DestinationSocket  = new LocalSocket <IMessage>(),
                MessageContracts   = new[] { new MessageContract {
                                                 Message = messageIdentifier
                                             } }
            });

            registrations.ForEach(r => internalRoutingTable.AddMessageRoute(r));
            var lookupRoute = registrations.First();
            var actor       = lookupRoute.ReceiverIdentifier;
            var localSocket = lookupRoute.DestinationSocket;
            //
            var socket = internalRoutingTable.FindRoutes(new InternalRouteLookupRequest
            {
                Message          = messageIdentifier,
                ReceiverIdentity = actor
            })
                         .First();

            //
            Assert.AreEqual(localSocket, socket);
        }
コード例 #4
0
        public void AddMessageRoute_AddsLocalOrExternalActorRoute(bool keepLocal)
        {
            var actor             = ReceiverIdentities.CreateForActor();
            var localSocket       = new LocalSocket <IMessage>();
            var messageIdentifier = MessageIdentifier.Create <SimpleMessage>();
            var registration      = new InternalRouteRegistration
            {
                ReceiverIdentifier = actor,
                DestinationSocket  = localSocket,
                MessageContracts   = new[]
                {
                    new MessageContract
                    {
                        Message = messageIdentifier,
                        KeepRegistrationLocal = keepLocal
                    }
                }
            };

            //
            internalRoutingTable.AddMessageRoute(registration);
            //
            var route = internalRoutingTable.GetAllRoutes().Actors.First().Actors.First();

            Assert.AreEqual(keepLocal, route.LocalRegistration);
            Assert.IsTrue(Unsafe.ArraysEqual(actor.Identity, route.Identity));
        }
コード例 #5
0
        public void AssignActor_SendsRegisterationMessage()
        {
            var partition         = Guid.NewGuid().ToByteArray();
            var messageIdentifier = MessageIdentifier.Create <SimpleMessage>(partition);

            try
            {
                StartActorHost(actorHost);
                actorHost.AssignActor(new ConfigurableActor(new[]
                {
                    new MessageHandlerDefinition
                    {
                        Handler = _ => null,
                        Message = new MessageDefinition(messageIdentifier.Identity,
                                                        messageIdentifier.Version,
                                                        partition)
                    }
                }));
                AsyncOp.Sleep();
                Func <InternalRouteRegistration, bool> registrationRequest = (reg) => reg.MessageContracts.Any(id => Unsafe.ArraysEqual(id.Message.Identity, messageIdentifier.Identity) &&
                                                                                                               Unsafe.ArraysEqual(id.Message.Partition, messageIdentifier.Partition) &&
                                                                                                               id.Message.Version == messageIdentifier.Version);
                internalRegistrationSender.Verify(m => m.Send(It.Is <InternalRouteRegistration>(reg => registrationRequest(reg))), Times.Once);
            }
            finally
            {
                actorHost.Stop();
            }
        }
コード例 #6
0
 public static CallbackPoint Create <T1, T2, T3>(byte[] partition = null)
     where T1 : IIdentifier, new()
     where T2 : IIdentifier, new()
     where T3 : IIdentifier, new()
 => new CallbackPoint(MessageIdentifier.Create <T1>(partition),
                      MessageIdentifier.Create <T2>(partition),
                      MessageIdentifier.Create <T3>(partition));
コード例 #7
0
        public void RemoveMessageRouteForNode_RemovesAllActorRegistrationsForThisMessageForThisNode()
        {
            var nodeIdentity       = Guid.NewGuid().ToByteArray();
            var messageIdentifier  = MessageIdentifier.Create <SimpleMessage>();
            var routeRegistrations = EnumerableExtensions.Produce(Randomizer.Int32(3, 10),
                                                                  () => CreateActorRouteRegistration(messageIdentifier,
                                                                                                     ReceiverIdentities.CreateForActor(),
                                                                                                     nodeIdentity));

            routeRegistrations.ForEach(r => externalRoutingTable.AddMessageRoute(r));
            //
            var res = externalRoutingTable.RemoveMessageRoute(new ExternalRouteRemoval
            {
                Route = new MessageRoute {
                    Message = messageIdentifier
                },
                NodeIdentifier = nodeIdentity
            });

            //
            Assert.AreEqual(PeerConnectionAction.Disconnect, res.ConnectionAction);
            var externalRouteLookupRequest = new ExternalRouteLookupRequest
            {
                ReceiverNodeIdentity = new ReceiverIdentifier(nodeIdentity)
            };

            CollectionAssert.IsEmpty(externalRoutingTable.FindRoutes(externalRouteLookupRequest));
        }
コード例 #8
0
        public void CallbackReceiverIdentities_AreCopiedFromIncomingMessageProcessedSync()
        {
            try
            {
                actorHost.AssignActor(new EchoActor());
                var messageIn            = Message.CreateFlowStartMessage(new SimpleMessage()).As <Message>();
                var callbackReceiver     = Guid.NewGuid().ToByteArray();
                var callbackReceiverNode = Guid.NewGuid().ToByteArray();
                messageIn.RegisterCallbackPoint(callbackReceiverNode,
                                                callbackReceiver,
                                                MessageIdentifier.Create <SimpleMessage>(),
                                                Randomizer.Int32());
                receivingSocket.SetupMessageReceived(messageIn);
                //
                StartActorHost(actorHost);
                //
                localRouterSocket.WaitUntilMessageSent(AssertCallbackPropertiesCopied);

                bool AssertCallbackPropertiesCopied(Message messageOut)
                => messageIn.CallbackPoint.SequenceEqual(messageOut.CallbackPoint) &&
                Unsafe.ArraysEqual(messageIn.CallbackReceiverIdentity, messageOut.CallbackReceiverIdentity) &&
                Unsafe.ArraysEqual(messageIn.CallbackReceiverNodeIdentity, messageOut.CallbackReceiverNodeIdentity);
            }
            finally
            {
                actorHost.Stop();
            }
        }
コード例 #9
0
ファイル: ActorHandlersMapTests.cs プロジェクト: lulzzz/kino
        public void WhenDuplicatedKeyExceptionThrown_NonOfTheActorHandlersIsAdded()
        {
            var actorHandlersMap = new ActorHandlerMap();

            var simpleMessageActor = new ConfigurableActor(new[]
            {
                new MessageHandlerDefinition
                {
                    Handler = null,
                    Message = MessageDefinition.Create <SimpleMessage>()
                }
            });
            var exceptionMessageActor = new ConfigurableActor(new[]
            {
                new MessageHandlerDefinition
                {
                    Handler = null,
                    Message = MessageDefinition.Create <ExceptionMessage>()
                },
                new MessageHandlerDefinition
                {
                    Handler = null,
                    Message = MessageDefinition.Create <SimpleMessage>()
                }
            });

            actorHandlersMap.Add(simpleMessageActor);
            actorHandlersMap.Get(MessageIdentifier.Create <SimpleMessage>());

            Assert.Throws <DuplicatedKeyException>(() => { actorHandlersMap.Add(exceptionMessageActor); });

            actorHandlersMap.Get(MessageIdentifier.Create <SimpleMessage>());
            Assert.Throws <KeyNotFoundException>(() => actorHandlersMap.Get(MessageIdentifier.Create <ExceptionMessage>()));
        }
コード例 #10
0
        public void Message_HasIdentitySet()
        {
            var message = Message.CreateFlowStartMessage(new SimpleMessage());

            Assert.IsNotNull(message.Identity);
            Assert.IsTrue(message.Equals(MessageIdentifier.Create <SimpleMessage>()));
        }
コード例 #11
0
        public void RegisterSelf_SendsOneRegistrationMessageForSpecifiedDomainButNotMessageDomain()
        {
            var actorIdentifier = ReceiverIdentities.CreateForActor();
            var registrations   = new[]
            {
                new MessageRoute
                {
                    Message  = MessageIdentifier.Create <SimpleMessage>(),
                    Receiver = actorIdentifier
                },
                new MessageRoute
                {
                    Message  = MessageIdentifier.Create <ExceptionMessage>(),
                    Receiver = actorIdentifier
                }
            };
            var simpleMessageDomain    = Guid.NewGuid().ToString();
            var exceptionMessageDomain = Guid.NewGuid().ToString();
            var allowedDomains         = new[] { exceptionMessageDomain, simpleMessageDomain, Guid.NewGuid().ToString() };

            securityProvider.Setup(m => m.GetDomain(MessageIdentifier.Create <SimpleMessage>().Identity)).Returns(simpleMessageDomain);
            securityProvider.Setup(m => m.GetDomain(MessageIdentifier.Create <ExceptionMessage>().Identity)).Returns(exceptionMessageDomain);
            securityProvider.Setup(m => m.GetAllowedDomains()).Returns(allowedDomains);
            //
            clusterMonitor.RegisterSelf(registrations, domain);
            //
            autoDiscoverySender.Verify(m => m.EnqueueMessage(It.Is <IMessage>(msg => msg.Domain == domain)), Times.Once);
        }
コード例 #12
0
ファイル: ActorHandlersMapTests.cs プロジェクト: lulzzz/kino
        public void ActorHandlersMap_CanAddTwoActorsHandlingSameMessageTypeInDifferentPartitions()
        {
            var actorHandlersMap      = new ActorHandlerMap();
            var partition             = Guid.NewGuid().ToByteArray();
            var actorWithoutPartition = new ConfigurableActor(new[]
            {
                new MessageHandlerDefinition
                {
                    Handler = null,
                    Message = MessageDefinition.Create <SimpleMessage>()
                }
            });

            var actorWithPartition = new ConfigurableActor(new[]
            {
                new MessageHandlerDefinition
                {
                    Handler = null,
                    Message = MessageDefinition.Create <SimpleMessage>(partition)
                }
            });

            actorHandlersMap.Add(actorWithoutPartition);
            actorHandlersMap.Add(actorWithPartition);

            actorHandlersMap.Get(MessageIdentifier.Create(typeof(SimpleMessage), partition));
            actorHandlersMap.Get(MessageIdentifier.Create(typeof(SimpleMessage)));
        }
コード例 #13
0
        public void WhenClusterMonitorStarts_ClusterRoutesAreRequested()
        {
            Func <Barrier, bool> setBarrier = (b) =>
            {
                b.SignalAndWait();
                return(true);
            };

            autoDiscoveryListener.Setup(m => m.StartBlockingListenMessages(It.IsAny <Action>(),
                                                                           It.IsAny <CancellationToken>(),
                                                                           It.Is <Barrier>(b => setBarrier(b))));
            autoDiscoverySender.Setup(m => m.StartBlockingSendMessages(It.IsAny <CancellationToken>(),
                                                                       It.Is <Barrier>(b => setBarrier(b))));
            //
            clusterMonitor.Start();
            //
            Func <Message, bool> isRequestClusterRoutesMessage = (msg) =>
            {
                if (msg.Equals(MessageIdentifier.Create <RequestClusterMessageRoutesMessage>()))
                {
                    var payload = msg.GetPayload <RequestClusterMessageRoutesMessage>();
                    Assert.IsTrue(Unsafe.ArraysEqual(scaleOutAddress.Identity, payload.RequestorNodeIdentity));
                    Assert.AreEqual(scaleOutAddress.Uri.ToSocketAddress(), payload.RequestorUri);
                    return(true);
                }

                return(false);
            };

            autoDiscoverySender.Verify(m => m.EnqueueMessage(It.Is <IMessage>(msg => isRequestClusterRoutesMessage(msg.As <Message>()))), Times.Once);
        }
コード例 #14
0
        public void IfCallbackIsRegistered_SyncExceptionMessageIsDeliveredToCallbackReceiver()
        {
            try
            {
                actorHost.AssignActor(new ExceptionActor());
                var messageIn            = Message.CreateFlowStartMessage(new SimpleMessage()).As <Message>();
                var callbackReceiver     = Guid.NewGuid().ToByteArray();
                var callbackReceiverNode = Guid.NewGuid().ToByteArray();
                var callbackPoints       = new[] { MessageIdentifier.Create <SimpleMessage>(), KinoMessages.Exception };
                messageIn.RegisterCallbackPoint(callbackReceiverNode,
                                                callbackReceiver,
                                                callbackPoints,
                                                Randomizer.Int32());
                receivingSocket.SetupMessageReceived(messageIn);
                //
                StartActorHost(actorHost);
                //
                localRouterSocket.WaitUntilMessageSent(AssertCallbackPropertiesCopied);

                bool AssertCallbackPropertiesCopied(Message messageOut)
                => messageOut.Equals(KinoMessages.Exception) &&
                messageIn.CallbackPoint.SequenceEqual(messageOut.CallbackPoint) &&
                Unsafe.ArraysEqual(messageIn.CallbackReceiverIdentity, messageOut.CallbackReceiverIdentity) &&
                Unsafe.ArraysEqual(messageIn.CallbackReceiverNodeIdentity, messageOut.CallbackReceiverNodeIdentity);
            }
            finally
            {
                actorHost.Stop();
            }
        }
コード例 #15
0
        private void IfNodeIdentifierIsNotProvidedOrNotFound_PeerRemovalResultIsNotFound(byte[] removeNodeIdentity)
        {
            var nodeIdentity      = Guid.NewGuid().ToByteArray();
            var messageIdentifier = MessageIdentifier.Create <SimpleMessage>();
            var routeRegistration = CreateActorRouteRegistration(messageIdentifier,
                                                                 ReceiverIdentities.CreateForActor(),
                                                                 nodeIdentity);

            externalRoutingTable.AddMessageRoute(routeRegistration);
            //
            var res = externalRoutingTable.RemoveMessageRoute(new ExternalRouteRemoval
            {
                Route = new MessageRoute {
                    Message = messageIdentifier
                },
                NodeIdentifier = removeNodeIdentity
            });

            //
            Assert.AreEqual(PeerConnectionAction.NotFound, res.ConnectionAction);
            var externalRouteLookupRequest = new ExternalRouteLookupRequest
            {
                ReceiverNodeIdentity = new ReceiverIdentifier(nodeIdentity)
            };

            CollectionAssert.IsNotEmpty(externalRoutingTable.FindRoutes(externalRouteLookupRequest));
        }
コード例 #16
0
ファイル: RouteDiscoveryTests.cs プロジェクト: lulzzz/kino
        public void IfSameMessageRouteRequestedMultipleTimes_MessageForThatRouteIsSentOnlyOnce()
        {
            var receiverIdentifier = ReceiverIdentities.CreateForActor();
            var messageIdentifier  = MessageIdentifier.Create <SimpleMessage>();

            //
            for (var i = 0; i < Randomizer.Int32(5, 15); i++)
            {
                routeDiscovery.RequestRouteDiscovery(new MessageRoute
                {
                    Message  = messageIdentifier,
                    Receiver = receiverIdentifier
                });
            }
            routeDiscovery.Start();
            AsyncOp.Sleep();
            routeDiscovery.Stop();
            //
            Func <IMessage, bool> isDiscoveryMessage = msg =>
            {
                var payload = msg.GetPayload <DiscoverMessageRouteMessage>();
                Assert.Null(payload.ReceiverIdentity);
                Assert.True(Unsafe.ArraysEqual(payload.MessageContract.Identity, messageIdentifier.Identity));
                Assert.True(Unsafe.ArraysEqual(payload.MessageContract.Partition, messageIdentifier.Partition));
                Assert.Equal(payload.MessageContract.Version, messageIdentifier.Version);
                Assert.True(Unsafe.ArraysEqual(payload.RequestorNodeIdentity, scaleOutAddress.Identity));
                Assert.Equal(payload.RequestorUri, scaleOutAddress.Uri.ToSocketAddress());
                return(true);
            };

            autoDiscoverySender.Verify(m => m.EnqueueMessage(It.Is <IMessage>(msg => isDiscoveryMessage(msg))), Times.Once);
        }
コード例 #17
0
        public void PopCallBackHandlerForSpecificCallbackKey_RemovesAllOtherHandlersForThisCallbackKey()
        {
            var promise = new Promise(Randomizer.Int64());
            var simpleMessageIdentifier   = MessageIdentifier.Create <SimpleMessage>();
            var messageHandlerIdentifiers = new[]
            {
                simpleMessageIdentifier,
                MessageIdentifier.Create <ExceptionMessage>()
            };

            callbackHandlerStack.Push(promise, messageHandlerIdentifiers);
            //
            var handler = callbackHandlerStack.Pop(new CallbackHandlerKey
            {
                Identity    = simpleMessageIdentifier.Identity,
                Version     = simpleMessageIdentifier.Version,
                Partition   = simpleMessageIdentifier.Partition,
                CallbackKey = promise.CallbackKey.Value
            });

            Assert.NotNull(handler);

            handler = callbackHandlerStack.Pop(new CallbackHandlerKey
            {
                Identity    = KinoMessages.Exception.Identity,
                Version     = KinoMessages.Exception.Version,
                CallbackKey = promise.CallbackKey.Value
            });
            Assert.Null(handler);
        }
コード例 #18
0
ファイル: RouteDiscoveryTests.cs プロジェクト: lulzzz/kino
        public void IfSecurityExceptionThrownForOneMessageRoute_OthersAreStillSent()
        {
            var messageHub     = ReceiverIdentities.CreateForMessageHub();
            var allowedDomains = Randomizer.Int32(2, 5).Produce(() => Guid.NewGuid().ToString());

            securityProvider.Setup(m => m.GetAllowedDomains()).Returns(allowedDomains);
            securityProvider.Setup(m => m.GetDomain(It.IsAny <byte[]>())).Throws <SecurityException>();
            //
            routeDiscovery.RequestRouteDiscovery(new MessageRoute {
                Receiver = messageHub
            });
            routeDiscovery.RequestRouteDiscovery(new MessageRoute
            {
                Receiver = ReceiverIdentities.CreateForActor(),
                Message  = MessageIdentifier.Create <SimpleMessage>()
            });
            routeDiscovery.Start();
            AsyncOp.Sleep();
            routeDiscovery.Stop();
            //
            Func <IMessage, bool> isDiscoveryMessage = msg =>
            {
                var payload = msg.GetPayload <DiscoverMessageRouteMessage>();
                Assert.Contains(msg.Domain, allowedDomains);
                Assert.True(Unsafe.ArraysEqual(messageHub.Identity, payload.ReceiverIdentity));
                Assert.Null(payload.MessageContract);
                Assert.True(Unsafe.ArraysEqual(payload.RequestorNodeIdentity, scaleOutAddress.Identity));
                Assert.Equal(payload.RequestorUri, scaleOutAddress.Uri.ToSocketAddress());
                return(true);
            };

            autoDiscoverySender.Verify(m => m.EnqueueMessage(It.Is <IMessage>(msg => isDiscoveryMessage(msg))), Times.Exactly(allowedDomains.Count()));
            logger.Verify(m => m.Error(It.Is <Exception>(exc => exc is SecurityException)), Times.Once);
        }
コード例 #19
0
        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);
                }
            }
        }
コード例 #20
0
        public void RegisterOwnGlobalRoutes_RegisteresOnlyGlobalyRegisteredActors()
        {
            var actors = EnumerableExtensions.Produce(Randomizer.Int32(5, 15),
                                                      i => new ReceiverIdentifierRegistration(ReceiverIdentities.CreateForActor(), i % 2 == 0));
            var messageIdentifier = MessageIdentifier.Create <SimpleMessage>();
            var internalRoutes    = new InternalRouting
            {
                Actors = new[]
                {
                    new MessageActorRoute
                    {
                        Message = messageIdentifier,
                        Actors  = actors
                    }
                },
                MessageHubs = Enumerable.Empty <MessageHubRoute>()
            };

            internalRoutingTable.Setup(m => m.GetAllRoutes()).Returns(internalRoutes);
            var globalActors = internalRoutes.Actors
                               .SelectMany(r => r.Actors.Where(a => !a.LocalRegistration))
                               .Select(a => new ReceiverIdentifier(a.Identity));

            //
            registrar.RegisterOwnGlobalRoutes(domain);
            //
            Func <IEnumerable <MessageRoute>, bool> isGlobalMessageRoute = mrs =>
            {
                Assert.IsTrue(mrs.All(mr => mr.Message == messageIdentifier));
                CollectionAssert.AreEquivalent(globalActors, mrs.Select(mr => mr.Receiver));
                return(true);
            };

            clusterMonitor.Verify(m => m.RegisterSelf(It.Is <IEnumerable <MessageRoute> >(mrs => isGlobalMessageRoute(mrs)), domain), Times.Once);
        }
コード例 #21
0
        public void CallbackTriggeresForEveryMessageInCallbackPoint()
        {
            var callbackReceiverIdentity     = Guid.NewGuid().ToByteArray();
            var callbackReceiverNodeIdentity = Guid.NewGuid().ToByteArray();
            var callbackMessageIdentifier    = new[]
            {
                MessageIdentifier.Create <SimpleMessage>(),
                MessageIdentifier.Create <AsyncExceptionMessage>(),
                MessageIdentifier.Create <AsyncMessage>()
            };
            var messages = new[]
            {
                Message.Create(new SimpleMessage()),
                Message.Create(new AsyncExceptionMessage()),
                Message.Create(new AsyncMessage()),
            };

            foreach (var message in messages.OfType <Message>())
            {
                message.RegisterCallbackPoint(callbackReceiverNodeIdentity,
                                              callbackReceiverIdentity,
                                              callbackMessageIdentifier,
                                              Randomizer.Int32());
                CollectionAssert.AreEqual(callbackReceiverIdentity, message.CallbackReceiverIdentity);
            }
        }
コード例 #22
0
        public void WhenCusterMonitorStops_UnregisterNodeMessageIsSent()
        {
            Func <Barrier, bool> setBarrier = (b) =>
            {
                b.SignalAndWait();
                return(true);
            };

            autoDiscoveryListener.Setup(m => m.StartBlockingListenMessages(It.IsAny <Action>(),
                                                                           It.IsAny <CancellationToken>(),
                                                                           It.Is <Barrier>(b => setBarrier(b))));
            autoDiscoverySender.Setup(m => m.StartBlockingSendMessages(It.IsAny <CancellationToken>(),
                                                                       It.Is <Barrier>(b => setBarrier(b))));
            //
            clusterMonitor.Start();
            AsyncOp.Sleep();
            clusterMonitor.Stop();
            //
            Func <IMessage, bool> isUnregistrationMessage = msg =>
            {
                if (msg.Equals(MessageIdentifier.Create <UnregisterNodeMessage>()))
                {
                    var payload = msg.GetPayload <UnregisterNodeMessage>();
                    Assert.IsTrue(Unsafe.ArraysEqual(scaleOutAddress.Identity, payload.ReceiverNodeIdentity));
                    Assert.AreEqual(scaleOutAddress.Uri.ToSocketAddress(), payload.Uri);
                    return(true);
                }

                return(false);
            };

            autoDiscoverySender.Verify(m => m.EnqueueMessage(It.Is <IMessage>(msg => isUnregistrationMessage(msg))), Times.Once);
        }
コード例 #23
0
        public void IfMessageHandlerIsNotRegistered_GetMessageHandlerReturnsNull()
        {
            var serviceMessageHandler = new Mock <IServiceMessageHandler>();

            serviceMessageHandler.Setup(m => m.TargetMessage).Returns(MessageIdentifier.Create <SimpleMessage>());
            var serviceMessageHandlers = new[] { serviceMessageHandler.Object };

            //
            Assert.IsNull(new ServiceMessageHandlerRegistry(serviceMessageHandlers).GetMessageHandler(MessageIdentifier.Create <AsyncMessage>()));
        }
コード例 #24
0
        public void IfThereAreTwoMessageHandlersForTheSameMessage_DuplicatedKeyExceptionIsThrown()
        {
            var serviceMessageHandler = new Mock <IServiceMessageHandler>();

            serviceMessageHandler.Setup(m => m.TargetMessage).Returns(MessageIdentifier.Create <SimpleMessage>());
            var serviceMessageHandlers = new[] { serviceMessageHandler.Object, serviceMessageHandler.Object };

            //
            Assert.Throws <DuplicatedKeyException>(() => new ServiceMessageHandlerRegistry(serviceMessageHandlers));
        }
コード例 #25
0
        public void GetMessageHandler_ReturnsReturnsRegisteredMessageHandler()
        {
            var serviceMessageHandler = new Mock <IServiceMessageHandler>();
            var messageIdentifier     = MessageIdentifier.Create <SimpleMessage>();

            serviceMessageHandler.Setup(m => m.TargetMessage).Returns(messageIdentifier);
            var serviceMessageHandlers = new[] { serviceMessageHandler.Object };

            //
            Assert.AreEqual(serviceMessageHandler.Object, new ServiceMessageHandlerRegistry(serviceMessageHandlers).GetMessageHandler(messageIdentifier));
        }
コード例 #26
0
        public void RegisterSelf_SendsRegistrationMessageForEachReceiver()
        {
            var actorIdentifier      = ReceiverIdentities.CreateForActor();
            var messageHubIdentifier = ReceiverIdentities.CreateForMessageHub();
            var registrations        = new[]
            {
                new MessageRoute
                {
                    Message  = MessageIdentifier.Create <SimpleMessage>(),
                    Receiver = actorIdentifier
                },
                new MessageRoute
                {
                    Message  = MessageIdentifier.Create <ExceptionMessage>(),
                    Receiver = actorIdentifier
                },
                new MessageRoute
                {
                    Receiver = messageHubIdentifier
                }
            };

            //
            clusterMonitor.RegisterSelf(registrations, domain);
            //
            Func <IMessage, bool> messageIsConsistent = msg =>
            {
                if (msg.Equals(MessageIdentifier.Create <RegisterExternalMessageRouteMessage>()))
                {
                    var payload = msg.GetPayload <RegisterExternalMessageRouteMessage>();
                    Assert.Equal(payload.Uri, scaleOutAddress.Uri.ToSocketAddress());
                    Assert.True(Unsafe.ArraysEqual(payload.NodeIdentity, scaleOutAddress.Identity));
                    Assert.Equal(payload.Health.Uri, heartBeatUri.ToSocketAddress());
                    Assert.Equal(payload.Health.HeartBeatInterval, heartBeatInterval);
                    var actorRoutes = payload.Routes
                                      .First(r => Unsafe.ArraysEqual(r.ReceiverIdentity, actorIdentifier.Identity));
                    foreach (var registration in registrations.Where(r => r.Receiver == actorIdentifier))
                    {
                        Assert.True(actorRoutes.MessageContracts.Any(mc => Unsafe.ArraysEqual(mc.Identity, registration.Message.Identity) &&
                                                                     Unsafe.ArraysEqual(mc.Partition, registration.Message.Partition) &&
                                                                     mc.Version == registration.Message.Version));
                    }
                    var messageHub = payload.Routes
                                     .First(r => Unsafe.ArraysEqual(r.ReceiverIdentity, messageHubIdentifier.Identity));
                    Assert.Null(messageHub.MessageContracts);
                    return(true);
                }

                return(false);
            };

            autoDiscoverySender.Verify(m => m.EnqueueMessage(It.Is <IMessage>(msg => messageIsConsistent(msg))), Times.Once);
        }
コード例 #27
0
        public void MessageContent_IsConsistentlyTransferredViaMultipartMessage()
        {
            var messageText = Guid.NewGuid().ToString();
            var message     = (Message)Message.CreateFlowStartMessage(new SimpleMessage {
                Content = messageText
            });

            var multipart = new MultipartMessage(message);

            message = Message.FromMultipartMessage(multipart);

            Assert.AreEqual(messageText, message.GetPayload <SimpleMessage>().Content);
            Assert.IsTrue(message.Equals(MessageIdentifier.Create <SimpleMessage>()));
        }
コード例 #28
0
ファイル: Actor.cs プロジェクト: silicondioxide/kino
        private MessageHandlerDefinition Selector(MethodInfo method)
        {
            var @delegate         = (MessageHandler)Delegate.CreateDelegate(typeof(MessageHandler), this, method);
            var attr              = method.GetCustomAttribute <MessageHandlerDefinitionAttribute>();
            var messageIdentifier = MessageIdentifier.Create(attr.MessageType);

            return(new MessageHandlerDefinition
            {
                Message = new MessageDefinition(messageIdentifier.Identity,
                                                messageIdentifier.Version,
                                                messageIdentifier.Partition),
                Handler = @delegate,
                KeepRegistrationLocal = attr.KeepRegistrationLocal
            });
        }
コード例 #29
0
        public void FindRoutesByMessageSentUnicast_ReturnsOneRoutes()
        {
            var messageIdentifier  = MessageIdentifier.Create <SimpleMessage>();
            var routeRegistrations = EnumerableExtensions.Produce(Randomizer.Int32(3, 10),
                                                                  () => CreateActorRouteRegistration(messageIdentifier));

            routeRegistrations.ForEach(r => externalRoutingTable.AddMessageRoute(r));
            //
            var externalRouteLookupRequest = new ExternalRouteLookupRequest
            {
                Message      = messageIdentifier,
                Distribution = DistributionPattern.Unicast
            };

            Assert.DoesNotThrow(() => externalRoutingTable.FindRoutes(externalRouteLookupRequest).Single());
        }
コード例 #30
0
        public void CallbackKey_IsConsistentlyTransferredViaMultipartMessage()
        {
            var message     = Message.CreateFlowStartMessage(new SimpleMessage()).As <Message>();
            var callbackKey = Randomizer.Int32(1, Int32.MaxValue);
            var callbackMessageIdentifier = MessageIdentifier.Create <SimpleMessage>();

            message.RegisterCallbackPoint(Guid.NewGuid().ToByteArray(),
                                          Guid.NewGuid().ToByteArray(),
                                          callbackMessageIdentifier,
                                          callbackKey);
            var multipart = new MultipartMessage(message);

            message = Message.FromMultipartMessage(multipart);

            Assert.AreEqual(callbackKey, message.CallbackKey);
        }