コード例 #1
0
ファイル: MDPBrokerTests.cs プロジェクト: fhchina/netmq
        public async void Run_ReceiveREADYMessageFromThreeWorkersSameServices_LogSuccessfulRegistration()
        {
            const string _END_POINT = "tcp://localhost:5555";
            var          log        = new List <string> ();
            var          id01       = Encoding.ASCII.GetBytes("W01");
            var          id02       = Encoding.ASCII.GetBytes("W02");
            var          id03       = Encoding.ASCII.GetBytes("W03");

            using (var cts = new CancellationTokenSource())
                using (var worker1 = new MDPWorker(_END_POINT, "echo", id01))
                    using (var worker2 = new MDPWorker(_END_POINT, "echo", id02))
                        using (var worker3 = new MDPWorker(_END_POINT, "echo", id03))
                            using (var broker = new MDPBroker(_END_POINT))
                            {
                                broker.Bind();
                                // collect all logging information from broker
                                broker.LogInfoReady += (s, e) => log.Add(e.Info);
                                // start broker session
                                broker.Run(cts.Token);
                                // start echo task
                                Task.Run(() => EchoWorker(worker1), cts.Token);
                                Task.Run(() => EchoWorker(worker2), cts.Token);
                                Task.Run(() => EchoWorker(worker3), cts.Token);
                                // wait for everything to happen
                                await Task.Delay(1000);

                                // cancel the tasks
                                cts.Cancel();
                                // check on the logging
                                Assert.That(log.Count, Is.EqualTo(7));
                                Assert.That(log.Count(s => s.Contains("Received Net")), Is.EqualTo(3));
                                Assert.That(log.Count(s => s.Contains("READY processed")), Is.EqualTo(3));
                            }
        }
コード例 #2
0
ファイル: MDPBrokerTests.cs プロジェクト: fhchina/netmq
        public async void Run_ReceiveREADYMessageFromWorker_LogSuccessfulRegistration()
        {
            const string _END_POINT = "tcp://localhost:5555";
            var          log        = new List <string> ();

            using (var cts = new CancellationTokenSource())
                using (var workerSession = new MDPWorker(_END_POINT, "echo"))
                    using (var broker = new MDPBroker(_END_POINT))
                    {
                        broker.Bind();
                        // collect all logging information from broker
                        broker.LogInfoReady += (s, e) => log.Add(e.Info);
                        // start broker session
                        broker.Run(cts.Token);
                        // start echo task
                        Task.Run(() => EchoWorker(workerSession), cts.Token);
                        // wait for everything to happen
                        await Task.Delay(500);

                        // cancel the tasks
                        cts.Cancel();
                        // check on the logging
                        Assert.That(log.Count, Is.EqualTo(3));
                        Assert.That(log[2], Is.StringContaining("added to service echo"));
                    }
        }
コード例 #3
0
ファイル: MDPBrokerTests.cs プロジェクト: fhchina/netmq
        public async void Run_ReceiveREPLYMessageFromThreeDifferentWorker_ShouldLogAndReturnCorrectReplies()
        {
            const string _END_POINT = "tcp://localhost:5555";
            var          log        = new List <string> ();

            var idW01 = new[] { (byte)'W', (byte)'1' };
            var idW02 = new[] { (byte)'W', (byte)'2' };
            var idW03 = new[] { (byte)'W', (byte)'3' };
            var idC01 = new[] { (byte)'C', (byte)'1' };
            var idC02 = new[] { (byte)'C', (byte)'2' };
            var idC03 = new[] { (byte)'C', (byte)'3' };

            const int _LONG_HEARTBEAT_INTERVAL = 10000;     // 10s heartbeat -> stay out of my testing for now

            using (var broker = new MDPBroker(_END_POINT, _LONG_HEARTBEAT_INTERVAL))
                using (var cts = new CancellationTokenSource())
                    using (var client01 = new MDPClient(_END_POINT, idC01))
                        using (var client02 = new MDPClient(_END_POINT, idC02))
                            using (var client03 = new MDPClient(_END_POINT, idC03))
                                using (var worker01 = new MDPWorker(_END_POINT, "echo", idW01))
                                    using (var worker02 = new MDPWorker(_END_POINT, "double echo", idW02))
                                        using (var worker03 = new MDPWorker(_END_POINT, "add hello", idW03))
                                        {
                                            broker.Bind();
                                            // collect all logging information from broker
                                            broker.LogInfoReady += (s, e) => log.Add(e.Info);
                                            // follow more details
                                            //broker.DebugInfoReady += (s, e) => debugLog.Add (e.Info);
                                            // start broker session
                                            broker.Run(cts.Token);
                                            // wait a little for broker to get started
                                            await Task.Delay(250);

                                            // get the task for simulating the worker & start it
                                            Task.Run(() => EchoWorker(worker01, _LONG_HEARTBEAT_INTERVAL), cts.Token);
                                            Task.Run(() => DoubleEchoWorker(worker02, _LONG_HEARTBEAT_INTERVAL), cts.Token);
                                            Task.Run(() => AddHelloWorker(worker03, _LONG_HEARTBEAT_INTERVAL), cts.Token);
                                            // wait a little for worker to get started & registered
                                            await Task.Delay(250);

                                            // get the task for simulating the client
                                            var client01Task = new Task(() => EchoClient(client01, "echo"));
                                            var client02Task = new Task(() => DoubleEchoClient(client02, "double echo"));
                                            var client03Task = new Task(() => AddHelloClient(client03, "add hello"));
                                            // start and wait for completion of client
                                            client01Task.Start();
                                            client02Task.Start();
                                            client03Task.Start();
                                            // the task completes when the message exchange is done
                                            Task.WaitAll(client01Task, client02Task, client03Task);
                                            // cancel the broker
                                            cts.Cancel();

                                            Assert.That(log.Count, Is.EqualTo(19));
                                            Assert.That(log.Count(s => s.Contains("READY")), Is.EqualTo(3));
                                            Assert.That(log.Count(s => s.Contains("REPLY")), Is.EqualTo(3));
                                            Assert.That(log.Count(s => s.Contains("Dispatching")), Is.EqualTo(3));
                                        }
        }
コード例 #4
0
        public async void Run_ReceiveREPLYMessageFromWorker_ShouldLogCorrectReply()
        {
            const string endPoint = "tcp://localhost:5555";
            var          log      = new List <string> ();
            var          debugLog = new List <string> ();

            var idW01 = new[] { (byte)'W', (byte)'1' };
            var idC01 = new[] { (byte)'C', (byte)'1' };

            const int longHeartbeatInterval = 10000; // 10s heartbeat -> stay out of my testing for now

            using (var broker = new MDPBroker(endPoint, longHeartbeatInterval))
                using (var cts = new CancellationTokenSource())
                    using (var echoClient = new MDPClient(endPoint, idC01))
                        using (var echoWorker = new MDPWorker(endPoint, "echo", idW01))
                        {
                            broker.Bind();
                            // collect all logging information from broker
                            broker.LogInfoReady += (s, e) => log.Add(e.Info);
                            // follow more details
                            broker.DebugInfoReady += (s, e) => debugLog.Add(e.Info);
                            // start broker session
                            broker.Run(cts.Token);
                            // wait a little for broker to get started
                            await Task.Delay(250);

                            // get the task for simulating the worker & start it
                            Task.Run(() => EchoWorker.Run(echoWorker, longHeartbeatInterval), cts.Token);
                            // wait a little for worker to get started & registered
                            await Task.Delay(250);

                            // get the task for simulating the client
                            var echoClientTask = new Task(() => EchoClient(echoClient, "echo"));
                            // start and wait for completion of client
                            echoClientTask.Start();
                            // the task completes when the message exchange is done
                            await echoClientTask;
                            // cancel the broker
                            cts.Cancel();

                            Assert.That(log.Count, Is.EqualTo(2));
                            Assert.That(log.Count(s => s.Contains("Starting to listen for incoming messages")), Is.EqualTo(1));
                            Assert.That(log.Count(s => s.Contains("READY processed. Worker W1 added to service echo")), Is.EqualTo(1));

                            if (debugLog.Count > 0)
                            {
                                Assert.That(debugLog.Count, Is.EqualTo(16));
                                Assert.That(debugLog.Count(s => s.Contains("Received")), Is.EqualTo(3));
                                Assert.That(debugLog.Contains("[MDP BROKER DEBUG] Dispatching -> NetMQMessage[C1,,Helo World!] to echo"));
                                Assert.That(debugLog.Contains("[MDP BROKER DEBUG] REPLY from W1 received and send to C1 -> NetMQMessage[MDPC01,echo,Helo World!]"));
                            }
                        }
        }
コード例 #5
0
        private static async Task RunBroker(CancellationTokenSource cts)
        {
            using (var broker = new MDPBroker("tcp://*:5555"))
            {
                if (_Verbose)
                {
                    broker.LogInfoReady += (s, e) => Console.WriteLine(e.Info);
                }

                await broker.Run(cts.Token);
            }
        }
コード例 #6
0
ファイル: MDPBrokerTests.cs プロジェクト: fhchina/netmq
        public void Bind_ReBindWhileBrokerIsRunning_ShouldThrowInvalidOperationException()
        {
            using (var sut = new MDPBroker("tcp://localhost:5556"))
            {
                var cts = new CancellationTokenSource();

                sut.Run(cts.Token);

                Assert.Throws <InvalidOperationException> (() => sut.Bind("tcp://localhost:5555"));

                cts.Cancel();
            }
        }
コード例 #7
0
ファイル: MDPBrokerTests.cs プロジェクト: fhchina/netmq
        public async void Run_ProcessMultipleRequestsWithMultipleClientsAndMultipleWorker_ShouldCorrectlyRouteReplies()
        {
            const string _END_POINT = "tcp://localhost:5555";
            var          log        = new List <string> ();

            var idW01 = new[] { (byte)'W', (byte)'1' };
            var idW02 = new[] { (byte)'W', (byte)'2' };
            var idC01 = new[] { (byte)'C', (byte)'1' };
            var idC02 = new[] { (byte)'C', (byte)'2' };

            const int _LONG_HEARTBEAT_INTERVAL = 10000;     // 10s heartbeat -> stay out of my testing for now

            using (var broker = new MDPBroker(_END_POINT, _LONG_HEARTBEAT_INTERVAL))
                using (var cts = new CancellationTokenSource())
                    using (var client1 = new MDPClient(_END_POINT, idC01))
                        using (var client2 = new MDPClient(_END_POINT, idC02))
                            using (var worker1 = new MDPWorker(_END_POINT, "echo", idW01))
                                using (var worker2 = new MDPWorker(_END_POINT, "echo", idW02))
                                {
                                    broker.Bind();
                                    // collect all logging information from broker
                                    broker.LogInfoReady += (s, e) => log.Add(e.Info);
                                    // follow more details
                                    //broker.DebugInfoReady += (s, e) => debugLog.Add (e.Info);
                                    // start broker session
                                    broker.Run(cts.Token);
                                    // wait a little for broker to get started
                                    await Task.Delay(250);

                                    // get the task for simulating the worker & start it
                                    Task.Run(() => MultipleRequestWorker(worker1, heartbeatinterval: _LONG_HEARTBEAT_INTERVAL), cts.Token);
                                    Task.Run(() => MultipleRequestWorker(worker2, heartbeatinterval: _LONG_HEARTBEAT_INTERVAL), cts.Token);
                                    // wait a little for worker to get started & registered
                                    await Task.Delay(250);

                                    // Create and run
                                    var c1 = Task.Run(() => MultipleRequestClient("echo", _END_POINT, client1));
                                    var c2 = Task.Run(() => MultipleRequestClient("echo", _END_POINT, client2));

                                    Task.WaitAll(c1, c2);
                                    // cancel the broker
                                    cts.Cancel();
                                }
        }
コード例 #8
0
        public async void Run_ProcessMultipleRequestsClientStopsAndReconnectsWithMultipleWorker_ShouldCorrectlyRouteReplies()
        {
            const string endPoint = "tcp://localhost:5555";
            var          log      = new List <string> ();
            var          debugLog = new List <string> ();

            var idW01 = new[] { (byte)'W', (byte)'1' };
            var idW02 = new[] { (byte)'W', (byte)'2' };

            const int longHeartbeatInterval = 10000; // 10s heartbeat -> stay out of my testing for now

            using (var broker = new MDPBroker(endPoint, longHeartbeatInterval))
                using (var cts = new CancellationTokenSource())
                    using (var worker1 = new MDPWorker(endPoint, "echo", idW01))
                        using (var worker2 = new MDPWorker(endPoint, "echo", idW02))
                        {
                            broker.Bind();
                            // collect all logging information from broker
                            broker.LogInfoReady += (s, e) => log.Add(e.Info);
                            // follow more details
                            broker.DebugInfoReady += (s, e) => debugLog.Add(e.Info);
                            // start broker session
                            broker.Run(cts.Token);
                            // wait a little for broker to get started
                            await Task.Delay(250);

                            // get the task for simulating the worker & start it
                            Task.Run(() => MultipleRequestWorker(worker1, heartbeatinterval: longHeartbeatInterval), cts.Token);
                            Task.Run(() => MultipleRequestWorker(worker2, heartbeatinterval: longHeartbeatInterval), cts.Token);
                            // wait a little for worker to get started & registered
                            await Task.Delay(250);

                            // create and run
                            await Task.Run(() => MultipleRequestClient ("echo", endPoint));

                            // recreate and run
                            await Task.Run(() => MultipleRequestClient ("echo", endPoint));

                            // cancel the broker & worker
                            cts.Cancel();
                        }
        }
コード例 #9
0
ファイル: MDPBrokerTests.cs プロジェクト: fhchina/netmq
        public async void Run_ProcessMultipleRequestsWorkerStops_ShouldCorrectlyRouteReplies()
        {
            const string _END_POINT = "tcp://localhost:5555";
            var          log        = new List <string> ();

            var idW01 = new[] { (byte)'W', (byte)'1' };

            const int _LONG_HEARTBEAT_INTERVAL = 10000;     // 10s heartbeat -> stay out of my testing for now

            using (var broker = new MDPBroker(_END_POINT, _LONG_HEARTBEAT_INTERVAL))
                using (var cts = new CancellationTokenSource())
                    using (var ctsWorker = new CancellationTokenSource())
                    {
                        broker.Bind();
                        // collect all logging information from broker
                        broker.LogInfoReady += (s, e) => log.Add(e.Info);
                        // follow more details
                        //broker.DebugInfoReady += (s, e) => debugLog.Add (e.Info);
                        // start broker session
                        broker.Run(cts.Token);
                        // wait a little for broker to get started
                        await Task.Delay(250);

                        // get the task for simulating the worker & start it
                        Task.Run(() => MultipleRequestWorker(null, _END_POINT, _LONG_HEARTBEAT_INTERVAL), ctsWorker.Token);
                        // wait a little for worker to get started & registered
                        await Task.Delay(250);

                        // get the task for simulating the client
                        var clientTask = new Task(() => MultipleRequestClient("echo", _END_POINT));
                        // start and wait for completion of client
                        clientTask.Start();
                        // Cancel worker
                        ctsWorker.Cancel();
                        // the task completes when the message exchange is done
                        await clientTask;
                        // cancel the broker
                        cts.Cancel();
                    }
        }