Exemplo n.º 1
0
        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"));
                    }
        }
Exemplo n.º 2
0
        public void ReceiveImplicitConnect_ValidScenario_ShouldReturnRequest()
        {
            const string hostAddress     = "tcp://localhost:5557";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var context = NetMQContext.Create())
                using (var broker = context.CreateRouterSocket())
                    using (var poller = new Poller())
                        using (var session = new MDPWorker(hostAddress, "test", new[] { (byte)'1' }))
                        {
                            broker.Bind(hostAddress);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                var msg = e.Socket.ReceiveMultipartMessage();
                                // we expect to receive a 5 Frame message
                                // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                                if (msg.FrameCount != 5)
                                {
                                    Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                                }
                                // make sure the frames are as expected
                                Assert.That(msg[1], Is.EqualTo(NetMQFrame.Empty));
                                Assert.That(msg[2].ConvertToString(), Is.EqualTo("MDPW01"));
                                Assert.That(msg[3].BufferSize, Is.EqualTo(1));
                                Assert.That(msg[3].Buffer[0], Is.EqualTo((byte)MDPCommand.Ready));
                                Assert.That(msg[4].ConvertToString(), Is.EqualTo("test"));

                                // tell worker to stop gracefully
                                var reply = new NetMQMessage();
                                reply.Push(new[] { (byte)MDPCommand.Kill });
                                // push MDP Version
                                reply.Push(msg[2]);
                                // push separator
                                reply.Push(NetMQFrame.Empty);
                                // push worker address
                                reply.Push(msg[0]);
                                // send reply which is a request for the worker
                                e.Socket.SendMessage(reply);
                            };

                            poller.AddSocket(broker);
                            Task.Factory.StartNew(poller.PollTillCancelled);

                            // set the event handler to receive the logging messages
                            session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                            // initialise the worker - broker protocol
                            session.Receive(null);

                            poller.CancelAndJoin();
                            poller.RemoveSocket(broker);

                            Assert.That(loggingMessages.Count, Is.EqualTo(5));
                            Assert.That(loggingMessages[0], Is.EqualTo("[WORKER] connected to broker at tcp://localhost:5557"));
                            Assert.That(loggingMessages[1].Contains("[WORKER] sending"), Is.True);
                            Assert.That(loggingMessages[2].Contains("[WORKER] received"));
                            Assert.That(loggingMessages[4].Contains("abandoning"));
                        }
        }
Exemplo n.º 3
0
        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));
                            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///     usage:  TitanicWorkerExample [-v]
        ///
        ///     implements a MDPWorker API usage
        /// </summary>
        static void Main(string[] args)
        {
            const string service_name = "echo";
            var          verbose      = args.Length == 1 && args[0] == "-v";
            var          exit         = false;

            // trapping Ctrl+C as exit signal!
            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                exit     = true;
            };
            var id = new[] { (byte)'W', (byte)'1' };

            Console.WriteLine("Starting the Titanic Worker - offering service {0}", service_name);
            Console.WriteLine("to exit - CTR-C\n");

            try
            {
                // create worker offering the service 'echo'
                using (var session = new MDPWorker("tcp://localhost:5555", service_name, id))
                {
                    // there is no inital reply
                    NetMQMessage reply = null;

                    while (!exit)
                    {
                        // send the reply and wait for a request
                        var request = session.Receive(reply);

                        if (verbose)
                        {
                            Console.WriteLine("[TITANIC WORKER] Received: {0}", request);
                        }

                        // was the worker interrupted
                        if (ReferenceEquals(request, null))
                        {
                            break;
                        }
                        // echo the request
                        reply = request;

                        if (verbose)
                        {
                            Console.WriteLine("[TITANIC WORKER] Reply: {0}", request);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("[TITANIC WORKER] ERROR:");
                Console.WriteLine("{0}", ex.Message);
                Console.WriteLine("{0}", ex.StackTrace);

                Console.WriteLine("exit - any key");
                Console.ReadKey();
            }
        }
Exemplo n.º 5
0
        public void Receive_BrokerDisconnectedWithLogging_ShouldReturnRequest()
        {
            const string hostAddress     = "tcp://localhost:5555";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var context = NetMQContext.Create())
                using (var broker = context.CreateRouterSocket())
                    using (var poller = new Poller())
                        using (var session = new MDPWorker(hostAddress, "test"))
                        {
                            broker.Bind(hostAddress);
                            // we need to pick up any message in order to avoid errors but don't answer
                            broker.ReceiveReady += (s, e) => e.Socket.ReceiveMultipartMessage();

                            poller.AddSocket(broker);
                            Task.Factory.StartNew(poller.PollTillCancelled);

                            // speed up the test
                            session.HeartbeatDelay = TimeSpan.FromMilliseconds(250);
                            session.ReconnectDelay = TimeSpan.FromMilliseconds(250);
                            // set the event handler to receive the logging messages
                            session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                            // initialise the worker - broker protocol
                            session.Receive(null);

                            poller.CancelAndJoin();
                            poller.RemoveSocket(broker);

                            Assert.That(loggingMessages.Count(m => m.Contains("retrying")), Is.EqualTo(3));
                            // 3 times retrying and 1 time initial connecting
                            Assert.That(loggingMessages.Count(m => m.Contains("localhost")), Is.EqualTo(4));
                            Assert.That(loggingMessages.Last().Contains("abandoning"));
                        }
        }
Exemplo n.º 6
0
        public void RunSynchronous_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
                        Task.Run(() => broker.RunSynchronous(cts.Token));
                        // start echo task
                        Task.Run(() => new EchoWorker().Run(workerSession), cts.Token);
                        // wait for everything to happen
                        Thread.Sleep(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"));
                    }
        }
Exemplo n.º 7
0
        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));
                                        }
        }
Exemplo n.º 8
0
        public void ctor_ValidParameter_ShouldReturnWorker()
        {
            var session = new MDPWorker("tcp://127.0.0.1:5555", "test");

            Assert.That(session, Is.Not.Null);
            Assert.That(session.HeartbeatDelay, Is.EqualTo(TimeSpan.FromMilliseconds(2500)));
            Assert.That(session.ReconnectDelay, Is.EqualTo(TimeSpan.FromMilliseconds(2500)));
        }
Exemplo n.º 9
0
        public void Receive_RequestWithMDPVersionMismatch_ShouldThrowApplicationException()
        {
            const string hostAddress = "tcp://localhost:5555";

            // setup the counter socket for communication

            using (var context = NetMQContext.Create())
                using (var broker = context.CreateRouterSocket())
                    using (var poller = new Poller())
                        using (var session = new MDPWorker(hostAddress, "test"))
                        {
                            broker.Bind(hostAddress);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                var msg = e.Socket.ReceiveMultipartMessage();
                                // we expect to receive a 5 Frame message
                                // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                                if (msg.FrameCount != 5)
                                {
                                    Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                                }
                                // make sure the frames are as expected
                                Assert.That(msg[1], Is.EqualTo(NetMQFrame.Empty));
                                Assert.That(msg[2].ConvertToString(), Is.EqualTo("MDPW01"));
                                Assert.That(msg[3].BufferSize, Is.EqualTo(1));
                                Assert.That(msg[3].Buffer[0], Is.EqualTo((byte)MDPCommand.Ready));
                                Assert.That(msg[4].ConvertToString(), Is.EqualTo("test"));

                                // tell worker to stop gracefully
                                var reply = new NetMQMessage();
                                reply.Push(new[] { (byte)MDPCommand.Kill });
                                // push MDP Version
                                reply.Push("MDPW00");
                                // push separator
                                reply.Push(NetMQFrame.Empty);
                                // push worker address
                                reply.Push(msg[0]);
                                // send reply which is a request for the worker
                                e.Socket.SendMessage(reply);
                            };

                            poller.AddSocket(broker);
                            Task.Factory.StartNew(poller.PollTillCancelled);

                            try
                            {
                                session.Receive(null);
                            }
                            catch (ApplicationException ex)
                            {
                                Assert.That(ex.Message, Is.EqualTo("Invalid protocol header received!"));
                            }

                            poller.CancelAndJoin();
                            poller.RemoveSocket(broker);
                        }
        }
Exemplo n.º 10
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!]"));
                            }
                        }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Implements a MDPWorker API usage
        /// </summary>
        /// <remarks>
        /// Usage:  MDPWorkerExample [-v]
        /// </remarks>
        private static void Main(string[] args)
        {
            var verbose = args.Length == 1 && args[0] == "-v";

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

            Console.WriteLine("Starting the worker!");

            try
            {
                // create worker offering the service 'echo'
                using (var session = new MDPWorker("tcp://localhost:5555", "echo", id))
                {
                    session.HeartbeatDelay = TimeSpan.FromMilliseconds(10000);
                    // logging info to be displayed on screen
                    if (verbose)
                    {
                        session.LogInfoReady += (s, e) => Console.WriteLine("{0}", e.Info);
                    }

                    // there is no initial reply
                    NetMQMessage reply = null;

                    while (true)
                    {
                        // send the reply and wait for a request
                        var request = session.Receive(reply);

                        if (verbose)
                        {
                            Console.WriteLine("Received: {0}", request);
                        }

                        // was the worker interrupted
                        if (ReferenceEquals(request, null))
                        {
                            break;
                        }
                        // echo the request
                        reply = request;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR:");
                Console.WriteLine("{0}", ex.Message);
                Console.WriteLine("{0}", ex.StackTrace);

                Console.WriteLine("exit - any key");
                Console.ReadKey();
            }
        }
Exemplo n.º 12
0
        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();
                                }
        }
Exemplo n.º 13
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();
                        }
        }
Exemplo n.º 14
0
        public void Run()
        {
            var t = new Task(() =>
            {
                var g = new MDPWorker("tcp://localhost:5555", parent.GetServiceName(bucketId),
                                      Encoding.ASCII.GetBytes(parent.GetServiceName(bucketId)));

                // logging info to be displayed on screen
                g.LogInfoReady += (s, e) => Console.WriteLine($"{e.Info}");

                // there is no initial reply
                NetMQMessage reply = null;

                bool exit = false;
                while (!exit)
                {
                    // send the reply and wait for a request
                    var request = g.Receive(reply);

                    Console.WriteLine($"Received a request");

                    // was the worker interrupted
                    if (ReferenceEquals(request, null))
                    {
                        break;
                    }
                    // echo the request
                    if (OnReceived != null)
                    {
                        reply = OnReceived(request);
                    }
                    else
                    {
                        throw new Exception("OnReceived handler is null - is handler set?");
                    }
                }
            });

            t.Start();
        }
Exemplo n.º 15
0
        public void Receive_REPLYtoREQUEST_ShouldSendCorrectReply()
        {
            const string hostAddress     = "tcp://localhost:5557";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var context = NetMQContext.Create())
                using (var broker = context.CreateRouterSocket())
                    using (var poller = new Poller())
                        using (var session = new MDPWorker(hostAddress, "test", new[] { (byte)'W', (byte)'1' }))
                        {
                            broker.Bind(hostAddress);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                var msg = e.Socket.ReceiveMultipartMessage();
                                if (msg[3].Buffer[0] == (byte)MDPCommand.Ready)
                                {
                                    // this is a READY message and we
                                    // send REQUEST message
                                    var request = new NetMQMessage();
                                    request.Push("echo test");                        // [request]
                                    request.Push(NetMQFrame.Empty);                   // [e][request]
                                    request.Push("C1");                               // [client adr][e][request]
                                    request.Push(new[] { (byte)MDPCommand.Request }); // [command][client adr][e][request]
                                    request.Push(msg[2]);                             // [header][command][client adr][e][request]
                                    request.Push(NetMQFrame.Empty);                   // [e][header][command][client adr][e][request]
                                    request.Push(msg[0]);                             // [worker adr][e][header][command][client adr][e][request]
                                    // send reply which is a request for the worker
                                    e.Socket.SendMessage(request);
                                }

                                if (msg[3].Buffer[0] == (byte)MDPCommand.Reply)
                                {
                                    // we expect to receive
                                    // [WORKER ADR][e]["MDPW01"][REPLY][CLIENT ADR][e][request == "echo test"]
                                    // make sure the frames are as expected
                                    Assert.That(msg[0].ConvertToString(), Is.EqualTo("W1"));
                                    Assert.That(msg[1], Is.EqualTo(NetMQFrame.Empty));
                                    Assert.That(msg[2].ConvertToString(), Is.EqualTo("MDPW01"));
                                    Assert.That(msg[3].BufferSize, Is.EqualTo(1));
                                    Assert.That(msg[3].Buffer[0], Is.EqualTo((byte)MDPCommand.Reply));
                                    Assert.That(msg[4].ConvertToString(), Is.EqualTo("C1"));
                                    Assert.That(msg[5], Is.EqualTo(NetMQFrame.Empty));
                                    Assert.That(msg[6].ConvertToString(), Is.EqualTo("echo test"));

                                    // tell worker to stop gracefully
                                    var reply = new NetMQMessage();
                                    reply.Push(new[] { (byte)MDPCommand.Kill });
                                    // push MDP Version
                                    reply.Push(msg[2]);
                                    // push separator
                                    reply.Push(NetMQFrame.Empty);
                                    // push worker address
                                    reply.Push(msg[0]);
                                    // send reply which is a request for the worker
                                    e.Socket.SendMessage(reply);
                                }
                            };

                            poller.AddSocket(broker);
                            Task.Factory.StartNew(poller.PollTillCancelled);

                            // set the event handler to receive the logging messages
                            session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                            // initialise the worker - broker protocol
                            // and get initial request
                            var workerRequest = session.Receive(null);
                            // just echo the request
                            session.Receive(workerRequest);

                            poller.CancelAndJoin();
                            poller.RemoveSocket(broker);

                            Assert.That(loggingMessages.Count, Is.EqualTo(8));
                            Assert.That(loggingMessages[0], Is.EqualTo("[WORKER] connected to broker at tcp://localhost:5557"));
                            Assert.That(loggingMessages[1].Contains("Ready"));
                            Assert.That(loggingMessages[2].Contains("[WORKER] received"));
                            Assert.That(loggingMessages[3].Contains("Request"));
                            Assert.That(loggingMessages[4].Contains("Reply"));
                            Assert.That(loggingMessages[6].Contains("Kill"));
                            Assert.That(loggingMessages[7].Contains("abandoning"));
                        }
        }
Exemplo n.º 16
0
        public void Receive_RequestWithWrongMDPComand_ShouldLogCorrectMessage()
        {
            const string hostAddress     = "tcp://localhost:5555";
            var          loggingMessages = new List <string> ();
            var          first           = true;

            // setup the counter socket for communication
            using (var context = NetMQContext.Create())
                using (var broker = context.CreateRouterSocket())
                    using (var poller = new Poller())
                        using (var session = new MDPWorker(hostAddress, "test", Encoding.ASCII.GetBytes("Worker"), 2))
                        {
                            broker.Bind(hostAddress);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                var msg = e.Socket.ReceiveMultipartMessage();
                                // we expect to receive a 5 Frame message
                                // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                                if (msg.FrameCount != 5)
                                {
                                    return; // it is a HEARTBEAT
                                }
                                // make sure the frames are as expected
                                Assert.That(msg[1], Is.EqualTo(NetMQFrame.Empty));
                                Assert.That(msg[2].ConvertToString(), Is.EqualTo("MDPW01"));
                                Assert.That(msg[3].BufferSize, Is.EqualTo(1));
                                Assert.That(msg[3].Buffer[0], Is.EqualTo((byte)MDPCommand.Ready));
                                Assert.That(msg[4].ConvertToString(), Is.EqualTo("test"));

                                // tell worker to stop gracefully
                                var reply = new NetMQMessage();
                                if (first)
                                {
                                    reply.Push(new[] { (byte)0xff });
                                    first = false;
                                }
                                else
                                {
                                    reply.Push(new[] { (byte)MDPCommand.Kill });
                                }
                                // push MDP Version
                                reply.Push("MDPW01");
                                // push separator
                                reply.Push(NetMQFrame.Empty);
                                // push worker address
                                reply.Push(msg[0]);
                                // send reply which is a request for the worker
                                e.Socket.SendMessage(reply);
                            };
                            // set the event handler to receive the logging messages
                            session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);

                            poller.AddSocket(broker);
                            Task.Factory.StartNew(poller.PollTillCancelled);

                            session.HeartbeatDelay = TimeSpan.FromMilliseconds(250);
                            session.ReconnectDelay = TimeSpan.FromMilliseconds(250);
                            // initialise the worker - broker protocol
                            session.Receive(null);

                            Assert.That(loggingMessages.Count(m => m.Contains("[WORKER ERROR] invalid command received")), Is.EqualTo(1));
                            Assert.That(loggingMessages.Count(m => m.Contains("abandoning")), Is.EqualTo(1));

                            poller.CancelAndJoin();
                            poller.RemoveSocket(broker);
                        }
        }