Ejemplo n.º 1
0
        public void Bind_Call_ShouldLogSuccess()
        {
            var info = string.Empty;
            using (var sut = new MDPBroker("tcp://localhost:5555"))
            {
                sut.LogInfoReady += (s, e) => { info = e.Info; };

                sut.Bind();

                Assert.That(info, Is.EqualTo("[BROKER] MDP Broker/0.3 is active at tcp://localhost:5555"));
            }
        }
Ejemplo n.º 2
0
        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();
            }
        }
Ejemplo n.º 3
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(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"));
            }
        }
Ejemplo n.º 4
0
        public async void Run_ProcessMultipleRequestsWorkerStops_ShouldCorrectlyRouteReplies()
        {
            const string endPoint = "tcp://localhost:5555";
            var log = new List<string>();

            var idW01 = new[] { (byte)'W', (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 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, endPoint, longHeartbeatInterval), 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", endPoint));
                // 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();
            }
        }
Ejemplo n.º 5
0
        public async void Run_ProcessMultipleRequestsClientStopsAndReconnectsWithMultipleWorker_ShouldCorrectlyRouteReplies()
        {
            const string endPoint = "tcp://localhost:5555";
            var log = 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();
            }
        }
Ejemplo n.º 6
0
        public async void Run_ReceiveREPLYMessageFromThreeDifferentWorker_ShouldLogAndReturnCorrectReplies()
        {
            const string endPoint = "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 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(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(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));
            }
        }
Ejemplo n.º 7
0
        public async void Run_ReceiveREPLYMessageFromWorker_ShouldLogCorrectReply()
        {
            const string endPoint = "tcp://localhost:5555";
            var log = 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(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(7));
                Assert.That(log.Count(s => s.Contains("READY processed. Worker W1 added to service echo")), Is.EqualTo(1));
                Assert.That(log.Count(s => s.Contains("Received")), Is.EqualTo(3));
                Assert.That(log[4], Is.EqualTo("[BROKER] Dispatching request -> NetMQMessage[C1,,Helo World!]"));
                Assert.That(log[6],
                    Is.EqualTo("[BROKER] REPLY from W1 received and send to C1 -> NetMQMessage[MDPC01,echo,Helo World!]"));
            }
        }
Ejemplo n.º 8
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(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(7));
                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));
            }
        }