protected virtual void Init()
        {
            mBridgeRequest  = new CommunicationBridge(CommunicationBridgeMode.RoundRobin);
            mBridgeResponse = new CommunicationBridge(CommunicationBridgeMode.Broadcast);

            mWebApi = new UnityWebApiMicroservicePipeline("Web")
                      .CallOut(WebApiConfigure)
                      .AddChannelOutgoing("Request", "This is the outgoing request channel")
                      .AttachSender(mBridgeRequest.GetSender())
                      .Revert()
                      .AddChannelIncoming("Response", "This is the response channel back from the Service")
                      .AttachListener(mBridgeResponse.GetListener())
                      .Revert();


            mService1 = new MicroservicePipeline("Service")
                        .CallOut(ServiceConfigure)
                        .AddChannelIncoming("Request", "This is the incoming request channel from the API")
                        .AttachListener(mBridgeRequest.GetListener())
                        .Revert()
                        .AddChannelOutgoing("Response", "This is the outgoing request channel")
                        .AttachSender(mBridgeResponse.GetSender())
                        .Revert()
            ;


            mService1.Start();
            mWebApi.Start();
        }
Exemple #2
0
        public void TestMethod1()
        {
            try
            {
                var bridgeOut = new CommunicationBridge(CommunicationBridgeMode.RoundRobin);
                var bridgein  = new CommunicationBridge(CommunicationBridgeMode.Broadcast);

                PersistenceClient <Guid, BridgeMe> init;
                DebugMemoryDataCollector           memp1, memp2;

                var p1 = new MicroservicePipeline("Sender")
                         .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault   = true)
                         .AddDataCollector((c) => new DebugMemoryDataCollector(), (c) => memp1 = c)
                         .AddChannelIncoming("cresponse")
                         .AttachListener(bridgein.GetListener())
                         .Revert()
                         .AddChannelOutgoing("crequest")
                         .AttachSender(bridgeOut.GetSender())
                         .AttachPersistenceClient("cresponse", out init)
                         .Revert()
                ;

                var p2 = new MicroservicePipeline("Receiver")
                         .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault   = true)
                         .AddDataCollector((c) => new DebugMemoryDataCollector(), (c) => memp2 = c)
                         .AddChannelIncoming("crequest")
                         .AttachListener(bridgeOut.GetListener())
                         .AttachCommand(new PersistenceManagerHandlerMemory <Guid, BridgeMe>((e) => e.Id, (s) => new Guid(s)))
                         .Revert()
                         .AddChannelOutgoing("cresponse")
                         .AttachSender(bridgein.GetSender())
                ;

                p2.ToMicroservice().Events.ExecuteBegin += CommunicationBridgeTests_OnExecuteBegin;
                p1.Start();
                p2.Start();

                int check1 = p1.ToMicroservice().Commands.Count();
                int check2 = p2.ToMicroservice().Commands.Count();

                var entity = new BridgeMe()
                {
                    Message = "Momma"
                };
                var rs = init.Create(entity, new RepositorySettings()
                {
                    WaitTime = TimeSpan.FromMinutes(5)
                }).Result;
                var rs2 = init.Read(entity.Id).Result;

                Assert.IsTrue(rs2.IsSuccess);
                Assert.IsTrue(rs2.Entity.Message == "Momma");
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public void TestMethod1()
        {
            try
            {
                var bridgeOut = new CommunicationBridge(CommunicationBridgeMode.RoundRobin);
                var bridgein  = new CommunicationBridge(CommunicationBridgeMode.Broadcast);

                PersistenceClient <Guid, BridgeMe> init;
                DebugMemoryDataCollector           memp1, memp2;

                var p1 = new MicroservicePipeline("Sender")
                         .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault = true)
                         .AddDebugMemoryDataCollector(out memp1)
                         .AddChannelIncoming("cresponse")
                         .AttachListener(bridgein.GetListener())
                         .Revert()
                         .AddChannelOutgoing("crequest")
                         .AttachSender(bridgeOut.GetSender())
                         .AttachPersistenceClient("cresponse", out init)
                ;

                var p2 = new MicroservicePipeline("Receiver")
                         .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault   = true)
                         .AddDataCollector((c) => new DebugMemoryDataCollector(), (c) => memp2 = c)
                         .AddChannelIncoming("crequest")
                         .AttachMessageRedirectRule(
                    canRedirect: (p) => p.Message.MessageType.Equals("bridgeme", StringComparison.InvariantCultureIgnoreCase)
                    , redirect: (p) => p.Message.MessageType = "BridgeMe2"
                    )
                         .AttachListener(bridgeOut.GetListener())
                         .AttachCommand(new PersistenceManagerHandlerMemory <Guid, BridgeMe2>((e) => e.Id, (s) => new Guid(s)))
                         .Revert()
                         .AddChannelOutgoing("cresponse")
                         .AttachSender(bridgein.GetSender())
                ;

                p1.Start();
                p2.Start();

                int check1 = p1.ToMicroservice().Commands.Count();
                int check2 = p2.ToMicroservice().Commands.Count();

                var entity = new BridgeMe()
                {
                    Message = "Momma"
                };
                var rs = init.Create(entity, new RepositorySettings()
                {
                    WaitTime = TimeSpan.FromSeconds(20)
                }).Result;

                Assert.IsTrue(!rs.IsSuccess && rs.ResponseCode == 422);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public void TestReroute()
        {
            var                      bridgeOut = new CommunicationBridge(CommunicationBridgeMode.RoundRobin);
            bool                     success = false;
            ManualResetEvent         mre = new ManualResetEvent(false);
            DebugMemoryDataCollector memp1, memp2;

            var p1 = new MicroservicePipeline("Sender")
                     .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault = true)
                     .AddDebugMemoryDataCollector(out memp1)
                     .AddChannelIncoming("fredo")
                     .AttachCommand(typeof(IContractInitial), (rq, rst, ps) =>
            {
                rst.Add(new TransmissionPayload(rq.Message.Clone().SetDestination <IContractFinal>()));
                return(Task.FromResult(0));
            })
                     .Revert()
                     .AddChannelOutgoing("crequest")
                     .AttachSender(bridgeOut.GetSender())
                     .Revert()
            ;

            var p2 = new MicroservicePipeline("Receiver")
                     .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault = true)
                     .AddDebugMemoryDataCollector(out memp2)
                     .AddChannelIncoming("crequest")
                     .AttachListener(bridgeOut.GetListener())
                     .AttachCommand(typeof(IContractFinal), (rq, rst, ps) =>
            {
                var value = ps.PayloadDeserialize <string>(rq);
                success   = value == "Hello";
                mre.Set();
                return(Task.FromResult(0));
            })
                     .Revert()
            ;

            p1.Start();
            p2.Start();

            //Send the message to the command asyncronously.
            p1.ToMicroservice().Dispatch.Process <IContractInitial>("Hello");

            mre.WaitOne();

            Assert.IsTrue(success);

            p1.Stop();
            p2.Stop();
        }
            public void TestMethod1()
            {
                try
                {
                    var bridgeOut = new CommunicationBridge(CommunicationBridgeMode.RoundRobin);
                    var bridgein  = new CommunicationBridge(CommunicationBridgeMode.Broadcast);

                    PersistenceClient <Guid, SecureMe> init;
                    DebugMemoryDataCollector           memp1, memp2;

                    var p1 = new MicroservicePipeline("Sender")
                             .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault = true)
                             .AddAuthenticationHandlerJwtToken("id1", JwtHashAlgorithm.HS256, Encoding.UTF8.GetBytes("My big secret"))
                             .AddDebugMemoryDataCollector(out memp1)
                             .AddChannelIncoming("cresponse", boundaryLoggingEnabled: true)
                             .AttachListener(bridgein.GetListener())
                             .Revert()
                             .AddChannelOutgoing("crequest", boundaryLoggingEnabled: true)
                             .AttachSender(bridgeOut.GetSender())
                             .AttachTransportPayloadSignature("id1")
                             .AttachPersistenceClient("cresponse", out init)
                             .Revert()
                    ;

                    var p2 = new MicroservicePipeline("Receiver")
                             .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault = true)
                             .AddAuthenticationHandlerJwtToken("id1", JwtHashAlgorithm.HS256, Encoding.UTF8.GetBytes("My big secret"))
                             .AddDebugMemoryDataCollector(out memp2)
                             .AddChannelIncoming("crequest", boundaryLoggingEnabled: true)
                             .AttachListener(bridgeOut.GetListener())
                             .AttachTransportPayloadVerification("id1")
                             .AttachCommand(new PersistenceManagerHandlerMemory <Guid, SecureMe>((e) => e.Id, (s) => new Guid(s)))
                             .Revert()
                             .AddChannelOutgoing("cresponse", boundaryLoggingEnabled: true)
                             .AttachSender(bridgein.GetSender())
                             .Revert()
                    ;

                    p1.Start();
                    p2.Start();

                    int check1 = p1.ToMicroservice().Commands.Count();
                    int check2 = p2.ToMicroservice().Commands.Count();

                    var entity = new SecureMe()
                    {
                        Message = "Momma"
                    };
                    var rs = init.Create(entity, new RepositorySettings()
                    {
                        WaitTime = TimeSpan.FromSeconds(30)
                    }).Result;
                    var rs2 = init.Read(entity.Id).Result;

                    Assert.IsTrue(rs2.IsSuccess);
                    Assert.IsTrue(rs2.Entity.Message == "Momma");
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
Exemple #6
0
        public void MasterJobNegotiation()
        {
            ManualResetEvent mre = new ManualResetEvent(false);
            var services         = new Dictionary <string, MicroservicePipeline>();

            string masterName = null;

            Action <object, string> goingMaster = (o, s) =>
            {
                if (masterName != null)
                {
                    Assert.Fail();
                }
                masterName = s;

                mre.Set();
            };

            Action <TestMasterJobCommand> release = (c) =>
            {
                c.OnGoingMaster += (object o, string s) => goingMaster(o, s);
            };

            try
            {
                var bridgeOut    = new CommunicationBridge(CommunicationBridgeMode.RoundRobin);
                var bridgein     = new CommunicationBridge(CommunicationBridgeMode.Broadcast);
                var bridgeMaster = new CommunicationBridge(CommunicationBridgeMode.Broadcast);

                PersistenceClient <Guid, BridgeMe> init, init3;
                DebugMemoryDataCollector           memp1, memp2, memp3;
                TestMasterJobCommand mast1 = null, mast2 = null, mast3 = null;

                services.Add("Sender1", new MicroservicePipeline("Sender1")
                             .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault = true)
                             .AddDebugMemoryDataCollector(out memp1)
                             .AddChannelIncoming("local", internalOnly: true)
                             .AttachCommand(mast1 = new TestMasterJobCommand(), assign: release)
                             .Revert()
                             .AddChannelIncoming("cresponse")
                             .AttachListener(bridgein.GetListener())
                             .Revert()
                             .AddChannelOutgoing("crequest")
                             .AttachSender(bridgeOut.GetSender())
                             .AttachPersistenceClient("cresponse", out init)
                             .Revert()
                             .AddChannelBroadcast("negotiate")
                             .AttachListener(bridgeMaster.GetListener())
                             .AttachSender(bridgeMaster.GetSender())
                             .AssignMasterJob(mast1)
                             .Revert()
                             );

                services.Add("Sender3", new MicroservicePipeline("Sender3")
                             .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault = true)
                             .AddDebugMemoryDataCollector(out memp3)
                             .AddChannelIncoming("local", internalOnly: true)
                             .AttachCommand(mast3 = new TestMasterJobCommand(), assign: release)
                             .Revert()
                             .AddChannelIncoming("cresponse")
                             .AttachListener(bridgein.GetListener())
                             .Revert()
                             .AddChannelOutgoing("crequest")
                             .AttachSender(bridgeOut.GetSender())
                             .AttachPersistenceClient("cresponse", out init3)
                             .Revert()
                             .AddChannelBroadcast("negotiate")
                             .AttachListener(bridgeMaster.GetListener())
                             .AttachSender(bridgeMaster.GetSender())
                             .AssignMasterJob(mast3)
                             .Revert()
                             );

                services.Add("Receiver2", new MicroservicePipeline("Receiver2")
                             .AdjustPolicyCommunication((p, c) => p.BoundaryLoggingActiveDefault = true)
                             .AddDebugMemoryDataCollector(out memp2)
                             .AddChannelIncoming("local", internalOnly: true)
                             .AttachCommand(mast2 = new TestMasterJobCommand(), assign: release)
                             .Revert()
                             .AddChannelIncoming("crequest")
                             .AttachListener(bridgeOut.GetListener())
                             .AttachCommand(new PersistenceManagerHandlerMemory <Guid, BridgeMe>((e) => e.Id, (s) => new Guid(s)))
                             .Revert()
                             .AddChannelOutgoing("cresponse")
                             .AttachSender(bridgein.GetSender())
                             .Revert()
                             .AddChannelBroadcast("negotiate")
                             .AttachListener(bridgeMaster.GetListener())
                             .AttachSender(bridgeMaster.GetSender())
                             .AssignMasterJob(mast2)
                             .Revert()
                             );


                services.Values.ForEach((v) => v.Start());

                int check1 = services["Sender1"].ToMicroservice().Commands.Count();
                int check2 = services["Sender3"].ToMicroservice().Commands.Count();

                var entity = new BridgeMe()
                {
                    Message = "Momma"
                };
                var rs = init.Create(entity, new RepositorySettings()
                {
                    WaitTime = TimeSpan.FromMinutes(5)
                }).Result;
                var rs2 = init.Read(entity.Id).Result;
                var rs3 = init3.Read(entity.Id).Result;

                Assert.IsTrue(rs2.IsSuccess);
                Assert.IsTrue(rs3.IsSuccess);
                Assert.IsTrue(rs2.Entity.Message == "Momma");

                //Wait for one of the services to go master.
                mre.WaitOne();
                Assert.IsNotNull(masterName);

                //Ok, service 2 take over
                mre.Reset();
                var holdme1 = masterName;
                masterName = null;
                services[holdme1].Stop();
                mre.WaitOne();

                //Ok, service 3 take over
                mre.Reset();
                var holdme2 = masterName;
                masterName = null;
                services[holdme2].Stop();

                mre.WaitOne();
                Assert.IsNotNull(masterName);
            }
            catch (Exception ex)
            {
                throw;
            }
        }