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")); } }
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)); } }
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")); } }
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)); } }
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!]")); } } }
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(); } }
public void Bind_ReBind_ShouldLogSuccess() { using (var sut = new MDPBroker("No_Valid_Endpoint")) { var info = string.Empty; sut.LogInfoReady += (s, e) => { info = e.Info; }; sut.Bind("tcp://localhost:5555"); Assert.That(info, Is.EqualTo("[BROKER] MDP Broker/0.3 is active at tcp://localhost:5555")); } }
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(); } }
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(); } }
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(); } }