Exemplo n.º 1
0
        static void Registration(RestRequestConvertersRegistry <XmlMessage> registry)
        {
            registry.AddConverter(XmlConverter.FromGET(
                                      @"/order",
                                      (path, query) => XmlMessage.FromString(string.Format(@"<orderAction type=""get"" orderId=""{0}"" />", path.Length > 1 ? path[1] : "0")),
                                      message => message.ToJson()));

            registry.AddConverter(XmlConverter.FromPOST(
                                      @"/order",
                                      (path, bodyJson) =>
            {
                string xmlTemplate = @"
                        <orderAction type=""create"">
                            <requestId>{0}</requestId>
                            <order>
                                <side>{3}</side>
                                <instrument>{2}</instrument>
                                <quantity>{1}</quantity>
                            </order>
                        </orderAction>";
                string xml         = string.Format(
                    xmlTemplate,
                    System.Guid.NewGuid(),
                    bodyJson["Quantity"],
                    bodyJson["Instrument"],
                    bodyJson["Side"]);
                return(XmlMessage.FromString(xml));
            },
                                      true,
                                      message => message.ToJson()));
        }
Exemplo n.º 2
0
        public void AnActorCanTellAnotherOne()
        {
            var messageFactory = new XmlMessageFactory();
            var a1             = new XmlActor.ActionWithFilter
            {
                Action = (message, sender, self, resource, ms, logger) => TestActor.Tell(XmlMessage.FromString("<message>done</message>")),
                Filter = AlwaysTrue
            };
            var actor1 = Sys.ActorOf(Props.Create(() => new XmlActor(
                                                      new [] { a1 },
                                                      null,
                                                      messageFactory,
                                                      null,
                                                      null)), "actor1");
            var a2 = new XmlActor.ActionWithFilter
            {
                Action = (message, sender, self, resource, ms, logger) => actor1.Tell(XmlMessage.FromString("<message>OK received</message>")),
                Filter = AlwaysTrue
            };
            var actor2 = Sys.ActorOf(Props.Create(() => new XmlActor(
                                                      new [] { a2 },
                                                      null,
                                                      messageFactory,
                                                      null,
                                                      null)), "actor2");

            actor2.Tell(XmlMessage.FromString("<message>OK?</message>"), TestActor);
            ExpectMsg <XmlMessage>(message => message.Content == "<message>done</message>");
        }
        void XmlMessageSystem_must_work()
        {
            Within(TimeSpan.FromSeconds(60), () =>
            {
                RunOn(() =>
                {
                    _messageSystem.Start(_nodeConfig);
                }, _deployTarget1, _deployTarget2);
                EnterBarrier("2-deploy-target-started");

                RunOn(() =>
                {
                    _messageSystem.Start(_nodeConfig);
                    var sender = new ActorRefMessageReceiver <XmlMessage>(TestActor);
                    _messageSystem.SendMessage(XmlMessage.FromString(@"<question1></question1>"), sender);
                    _messageSystem.SendMessage(XmlMessage.FromString(@"<question1></question1>"), sender);
                    _messageSystem.SendMessage(XmlMessage.FromString(@"<question1></question1>"), sender);
                    new[] { ExpectMsg <BytesMessage>(), ExpectMsg <BytesMessage>(), ExpectMsg <BytesMessage>() }.Select(bm => XmlMessage.FromBytes(bm.Bytes).Content)
                    .Should().OnlyHaveUniqueItems();
                    _messageSystem.SendMessage(XmlMessage.FromString(@"<question2>log1</question2>"), sender);
                    _messageSystem.SendMessage(XmlMessage.FromString(@"<question2>log2</question2>"), sender);
                    _messageSystem.SendMessage(XmlMessage.FromString(@"<question2>log3</question2>"), sender);
                    new[] { ExpectMsg <BytesMessage>(), ExpectMsg <BytesMessage>(), ExpectMsg <BytesMessage>() }.Select(bm => XmlMessage.FromBytes(bm.Bytes).Content)
                    .Should().OnlyHaveUniqueItems();
                }, _deployer);

                EnterBarrier("3-done");
            });
        }
Exemplo n.º 4
0
        static void Register(PatternActionsRegistry <XmlMessage, XmlMessagePattern> registry)
        {
            registry.AddAction(
                new XmlMessagePattern(@"/ping"),
                (message, sender, self, resource, ms, logger) =>
            {
                // Pretend to use the lazy resource
                var res = resource.Value;
                logger.Info(@"Received {0}", message);
                dynamic xml = message.AsDynamic();
                if (xml.ping == "crash")
                {
                    throw new Exception("Crash requested");
                }
                sender.Tell(XmlMessage.FromString($@"
                        <pong>
                            <ping>{xml.ping}</ping>
                            <node>{self.Path}</node>
                        </pong>"), self);
            },
                "actor1");

            registry.SetProcessingUnitResourceFactory(
                "actor1",
                logger => new Lazy <IDisposable>(() => new FakeResource(logger))
                );
        }
Exemplo n.º 5
0
 static void Register(PatternActionsRegistry <XmlMessage, XmlMessagePattern> registry)
 {
     registry.AddAction(
         new XmlMessagePattern(@"/plic"),
         (message, sender, self, resource, ms, logger) =>
     {
         logger.Info(@"Received {0}", message);
         var xml = message.AsDynamic();
         sender.Tell(XmlMessage.FromString($@"<ploc>{xml.plic}</ploc>"), self);
     },
         "actor2");
 }
Exemplo n.º 6
0
        void XmlMessageSystem_must_work()
        {
            Within(TimeSpan.FromSeconds(30), () =>
            {
                RunOn(() =>
                {
                    _registry.AddAsyncAction(
                        new XmlMessagePattern($@"/request[@routeto = ""{_first.Name}""]"),
                        async(message, sender, self, resource, messageSystem, logger) =>
                    {
                        XmlMessage answer = await messageSystem.SendMessageAndAwaitResponseAsync(XmlMessage.FromString(@"<question>Why?</question>"), self);
                        answer.Should().Match <XmlMessage>(mess => mess.Match(@"/answer[. = ""Because.""]"));
                        TestActor.Tell("OK");
                    },
                        _first.Name);
                }, _first);

                RunOn(() =>
                {
                    _registry.AddAction(
                        new XmlMessagePattern(true, @"/request"),
                        (message, sender, self, resource, messageSystem, logger) =>
                    {
                        TestActor.Tell("OK");
                    },
                        _second.Name);
                    _registry.AddPowershellScriptBody(
                        new XmlMessagePattern(@"/question[. = ""Why?""]"),
                        @"'<answer>Because.</answer>'",
                        _second.Name
                        );
                }, _second);

                _messageSystem.Start(_nodeConfig, _registry);
                EnterBarrier("2-started");

                RunOn(() =>
                {
                    string xml = @"<request routeto=""first"">GO</request>";
                    _messageSystem.SendMessage(XmlMessage.FromString(xml), null);
                }, _third);
                RunOn(() => ExpectMsg <string>("OK"), _first);
                RunOn(() => ExpectMsg <string>("OK"), _second);

                EnterBarrier("3-done");
            });
        }
 static void RegisterActions(PatternActionsRegistry <XmlMessage, XmlMessagePattern> registry)
 {
     registry.AddAction(
         new XmlMessagePattern(@"/question1"),
         (m, sender, self, resource, ms, logger) =>
     {
         sender.Tell(XmlMessage.FromString($@"<answer1>{self.Path}</answer1>"), self);
     },
         "routed");
     registry.AddPowershellScriptBody(
         new XmlMessagePattern(@"/question2"),
         @"'<answer2>{0}</answer2>' -f $self.Path",
         "routed");
     registry.AddPowershellScriptBody(
         new XmlMessagePattern(true, @"/question2"),
         @"$xml = [xml]$message; $xml.question2",
         "monitor");
 }
Exemplo n.º 8
0
        public void AnActorCanBeAskedFromOutside()
        {
            var a1 = new XmlActor.ActionWithFilter
            {
                Action = (message, sender, self, resource, ms, logger) => sender.Tell(XmlMessage.FromString("<message>OK!</message>"), self),
                Filter = AlwaysTrue
            };
            var messageFactory = new XmlMessageFactory();
            var actor1         = Sys.ActorOf(Props.Create(() => new XmlActor(
                                                              new[] { a1 },
                                                              null,
                                                              messageFactory,
                                                              null,
                                                              null)), "actor1");
            var r =
                new ActorRefMessageReceiver <XmlMessage>(actor1)
                .AskAsync(XmlMessage.FromString("<message>test</message>"), messageFactory)
                .Result;

            r.Match(@"message[. = ""OK!""]").Should().BeTrue();
        }
Exemplo n.º 9
0
        public void AnActorCanAskAnotherOne()
        {
            var messageFactory = new XmlMessageFactory();
            var a1             = new XmlActor.ActionWithFilter
            {
                Action = (message, sender, self, resource, ms, logger) => sender.Tell(XmlMessage.FromString("<message>OK!</message>"), self),
                Filter = AlwaysTrue
            };
            var actor1 = Sys.ActorOf(Props.Create(() => new XmlActor(
                                                      new[] { a1 },
                                                      null,
                                                      messageFactory,
                                                      null,
                                                      null)), "actor1");
            var a2 = new XmlActor.AsyncActionWithFilter
            {
                Action = async(message, sender, self, resource, ms, logger) =>
                {
                    var r = await
                            new ActorRefMessageReceiver <XmlMessage>(actor1)
                            .AskAsync(XmlMessage.FromString("<message>OK?</message>"), messageFactory);
                    Assert.Equal("<message>OK!</message>", r.Content);
                    TestActor.Tell(XmlMessage.FromString("<message>done</message>"));
                },
                Filter = AlwaysTrue
            };
            var actor2 = Sys.ActorOf(Props.Create(() => new XmlActor(
                                                      null,
                                                      new[] { a2 },
                                                      messageFactory,
                                                      null,
                                                      null)), "actor2");

            actor2.Tell(XmlMessage.FromString("<message>test</message>"));
            ExpectMsg <XmlMessage>(message => message.Content == "<message>done</message>");
        }
Exemplo n.º 10
0
        void ActorDirectoryMustGetInSync()
        {
            Within(TimeSpan.FromSeconds(60), () =>
            {
                RunOn(() =>
                {
                    string puId = "test1";
                    _actorDirectory
                    .PublishPatterns(
                        _nodeConfig,
                        (
                            puId: puId,
                            actorPath: ActorPath.Parse(string.Format("akka://cluster/user/{0}", Hexagon.Constants.GetProcessingUnitName(_nodeConfig.NodeId, puId))),
                            patterns: new[]
                    {
                        new XmlMessagePattern(
                            new []
                        {
                            @"/root/value1[. = 1]",
                            @"/root/value2[@attr = ""a""]"
                        }),
                        new XmlMessagePattern(
                            new []
                        {
                            @"/root/value2[. = 2]"
                        })
                    }
                        )
                        );
                }, _first);

                RunOn(() =>
                {
                    string puId = "test2";
                    _actorDirectory
                    .PublishPatterns(
                        _nodeConfig,
                        (
                            puId: puId,
                            actorPath: ActorPath.Parse(string.Format("akka://cluster/user/{0}", Hexagon.Constants.GetProcessingUnitName(_nodeConfig.NodeId, puId))),
                            patterns: new[]
                    {
                        new XmlMessagePattern(
                            new []
                        {
                            @"/root/value3[. = 3]"
                        })
                    }));
                }, _second);

                RunOn(() =>
                {
                    _actorDirectory.PublishPatterns(null);
                }, _third);

                EnterBarrier("2-registered");

                var timeFrame = TimeSpan.FromSeconds(_nodeConfig.GossipTimeFrameInSeconds);
                System.Threading.Thread.Sleep(timeFrame);

                Cluster.State.Members.Where(member => member.Status == MemberStatus.Up).Should().HaveCount(3, "there should be 3 up cluster nodes");

                bool ready = _actorDirectory.IsReady(_nodeConfig.GossipSynchroAttemptCount, timeFrame);
                ready.Should().BeTrue("all nodes data must be ready");

                EnterBarrier("3-ready");

                RunOn(() =>
                {
                    var patternFactory = new XmlMessagePatternFactory();
                    string xml         = @"<root><value1>1</value1><value2 attr=""b"">2</value2><value3>3</value3></root>";
                    var actorPaths     = _actorDirectory.GetMatchingActorsAsync(XmlMessage.FromString(xml), patternFactory).Result.Select(ma => ma.Path);
                    actorPaths.Should().BeEquivalentTo($"/user/{_first.Name}_test1", $"/user/{_second.Name}_test2");
                }, _first, _second, _third);

                EnterBarrier("4-done");

                _actorDirectory.Dispose();

                EnterBarrier("5-exit");
            });