Ejemplo n.º 1
0
        public async void Run_ReceiveREADYMessageFromWorker_LogSuccessfulRegistration()
        {
            const string endPoint = "tcp://localhost:5555";
            var          log      = new List <string> ();

            using (var cts = new CancellationTokenSource())
                using (var workerSession = new MDPWorker(endPoint, "echo"))
                    using (var broker = new MDPBroker(endPoint))
                    {
                        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.Run(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(2));
                        Assert.That(log[1], Is.StringContaining("added to service echo"));
                    }
        }
Ejemplo n.º 2
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!]"));
                            }
                        }
        }
Ejemplo n.º 3
0
        public async void Run_ReceiveREADYMessageFromThreeWorkersDifferentServices_LogSuccessfulRegistration()
        {
            const string endPoint = "tcp://localhost:5555";
            var          log      = new List <string> ();
            var          id01     = Encoding.ASCII.GetBytes("Worker01");
            var          id02     = Encoding.ASCII.GetBytes("Worker02");
            var          id03     = Encoding.ASCII.GetBytes("Worker03");

            using (var cts = new CancellationTokenSource())
                using (var worker1 = new MDPWorker(endPoint, "echo", id01))
                    using (var worker2 = new MDPWorker(endPoint, "double echo", id02))
                        using (var worker3 = new MDPWorker(endPoint, "add hello", id03))
                            using (var broker = new MDPBroker(endPoint))
                            {
                                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.Run(worker1), cts.Token);
                                Task.Run(() => DoubleEchoWorker(worker2), cts.Token);
                                Task.Run(() => AddHelloWorker(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(4));
                                Assert.That(log.Count(s => s.Contains("service echo")), Is.EqualTo(1));
                                Assert.That(log.Count(s => s.Contains("service double echo")), Is.EqualTo(1));
                                Assert.That(log.Count(s => s.Contains("service add hello")), Is.EqualTo(1));
                                Assert.That(log.Count(s => s.Contains("READY processed")), Is.EqualTo(3));
                            }
        }
Ejemplo n.º 4
0
        public async void Run_ReceiveREPLYMessageFromThreeDifferentWorker_ShouldLogAndReturnCorrectReplies()
        {
            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' };
            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 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 client01 = new MDPClient(endPoint, idC01))
                        using (var client02 = new MDPClient(endPoint, idC02))
                            using (var client03 = new MDPClient(endPoint, idC03))
                                using (var worker01 = new MDPWorker(endPoint, "echo", idW01))
                                    using (var worker02 = new MDPWorker(endPoint, "double echo", idW02))
                                        using (var worker03 = new MDPWorker(endPoint, "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.Run(worker01, longHeartbeatInterval), cts.Token);
                                            Task.Run(() => DoubleEchoWorker(worker02, longHeartbeatInterval), cts.Token);
                                            Task.Run(() => AddHelloWorker(worker03, longHeartbeatInterval), 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(4));
                                            Assert.That(log.Count(s => s.Contains("READY")), Is.EqualTo(3));

                                            if (debugLog.Count > 0)
                                            {
                                                Assert.That(debugLog.Count(s => s.Contains("Received: NetMQMessage[W1")), Is.GreaterThanOrEqualTo(1));
                                                Assert.That(debugLog.Count(s => s.Contains("Received: NetMQMessage[W2")), Is.GreaterThanOrEqualTo(1));
                                                Assert.That(debugLog.Count(s => s.Contains("Received: NetMQMessage[W3")), Is.GreaterThanOrEqualTo(1));
                                                Assert.That(debugLog.Count(s => s.Contains("Dispatching -> NetMQMessage[C1")), Is.GreaterThanOrEqualTo(1));
                                                Assert.That(debugLog.Count(s => s.Contains("Dispatching -> NetMQMessage[C2")), Is.GreaterThanOrEqualTo(1));
                                                Assert.That(debugLog.Count(s => s.Contains("Dispatching -> NetMQMessage[C3")), Is.GreaterThanOrEqualTo(1));
                                                Assert.That(debugLog.Count(s => s.Contains("REPLY from W1")), Is.EqualTo(1));
                                                Assert.That(debugLog.Count(s => s.Contains("REPLY from W2")), Is.EqualTo(1));
                                                Assert.That(debugLog.Count(s => s.Contains("REPLY from W3")), Is.EqualTo(1));
                                            }
                                        }
        }