public virtual void A15_ResponseReceiverReconnects_AfterStopListening() { IDuplexOutputChannel aDuplexOutputChannel = MessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexInputChannel aDuplexInputChannel = MessagingSystem.CreateDuplexInputChannel(ChannelId); AutoResetEvent aConnectionsCompletedEvent = new AutoResetEvent(false); List <string> anOpenConnections = new List <string>(); aDuplexInputChannel.ResponseReceiverConnected += (x, y) => { lock (anOpenConnections) { anOpenConnections.Add(y.ResponseReceiverId); aConnectionsCompletedEvent.Set(); } }; try { aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); // Wait until the client is connected. aConnectionsCompletedEvent.WaitOne(); // Stop listenig. aDuplexInputChannel.StopListening(); // Give some time to stop. Thread.Sleep(700); // Start listening again. aDuplexInputChannel.StartListening(); // The duplex output channel will try to connect again, therefore wait until connected. aConnectionsCompletedEvent.WaitOne(); Assert.IsTrue(aDuplexOutputChannel.IsConnected); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } Assert.AreEqual(2, anOpenConnections.Count); // Both connections should be same. Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, anOpenConnections[0]); Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, anOpenConnections[1]); }
public void AttachDuplexInputChannel(IDuplexInputChannel duplexInputChannel) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myDuplexInputChannels)) { Attach(duplexInputChannel); try { duplexInputChannel.StartListening(); } catch (Exception err) { // Try to clean after the failure try { DetachDuplexInputChannel(duplexInputChannel.ChannelId); } catch { } string aMessage = TracedObject + "failed to start listening for '" + duplexInputChannel.ChannelId + "'."; EneterTrace.Error(aMessage, err); throw; } } } }
public void ClientReceiveTimeout() { IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory() { ReceiveTimeout = TimeSpan.FromMilliseconds(1000) }; IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("ws://127.0.0.1:8046/"); IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("ws://127.0.0.1:8046/"); try { ManualResetEvent aConnectionClosed = new ManualResetEvent(false); anOutputChannel.ConnectionClosed += (x, y) => { EneterTrace.Info("Connection closed."); aConnectionClosed.Set(); }; anInputChannel.StartListening(); anOutputChannel.OpenConnection(); EneterTrace.Info("Connection opened."); // According to set receive timeout the client should get disconnected within 1 second. //aConnectionClosed.WaitOne(); Assert.IsTrue(aConnectionClosed.WaitOne(3000)); } finally { anOutputChannel.CloseConnection(); anInputChannel.StopListening(); } }
public void PortAvailability() { IDuplexInputChannel anInputChannel1 = MessagingSystemFactory.CreateDuplexInputChannel("tcp://[::1]:8044/"); IDuplexInputChannel anInputChannel2 = MessagingSystemFactory.CreateDuplexInputChannel("tcp://127.0.0.1:8044/"); try { anInputChannel1.StartListening(); anInputChannel2.StartListening(); Console.WriteLine("Available IP addresses:"); string[] anAvailableIpAddresses = TcpMessagingSystemFactory.GetAvailableIpAddresses(); foreach (string anIpAddress in anAvailableIpAddresses) { Console.WriteLine(anIpAddress); } bool aResult = TcpMessagingSystemFactory.IsPortAvailableForTcpListening("tcp://[::1]:8044/"); Assert.IsFalse(aResult); aResult = TcpMessagingSystemFactory.IsPortAvailableForTcpListening("tcp://0.0.0.0:8044/"); Assert.IsTrue(aResult); } finally { anInputChannel1.StopListening(); anInputChannel2.StopListening(); } }
public void BroadcastFromClientToAllServices() { UdpMessagingSystemFactory anOutputChannelMessaging = new UdpMessagingSystemFactory(new EasyProtocolFormatter()) { UnicastCommunication = false, AllowSendingBroadcasts = true }; UdpMessagingSystemFactory anInputChannelMessaging = new UdpMessagingSystemFactory(new EasyProtocolFormatter()) { UnicastCommunication = false, ReuseAddress = true }; ManualResetEvent aMessage1Received = new ManualResetEvent(false); string aReceivedMessage1 = null; IDuplexInputChannel anInputChannel1 = anInputChannelMessaging.CreateDuplexInputChannel("udp://127.0.0.1:8095/"); anInputChannel1.MessageReceived += (x, y) => { aReceivedMessage1 = (string)y.Message; aMessage1Received.Set(); }; ManualResetEvent aMessage2Received = new ManualResetEvent(false); string aReceivedMessage2 = null; IDuplexInputChannel anInputChannel2 = anInputChannelMessaging.CreateDuplexInputChannel("udp://127.0.0.1:8095/"); anInputChannel2.MessageReceived += (x, y) => { aReceivedMessage2 = (string)y.Message; aMessage2Received.Set(); }; IDuplexOutputChannel anOutputChannel = anOutputChannelMessaging.CreateDuplexOutputChannel("udp://255.255.255.255:8095/", "udp://127.0.0.1"); try { anInputChannel1.StartListening(); anInputChannel2.StartListening(); anOutputChannel.OpenConnection(); anOutputChannel.SendMessage("Hello"); aMessage1Received.WaitIfNotDebugging(1000); aMessage2Received.WaitIfNotDebugging(1000); } finally { anOutputChannel.CloseConnection(); anInputChannel1.StopListening(); anInputChannel2.StopListening(); } Assert.AreEqual("Hello", aReceivedMessage1); Assert.AreEqual("Hello", aReceivedMessage2); }
public void MulticastFromServiceToClients() { UdpMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(new EasyProtocolFormatter()) { UnicastCommunication = false, ReuseAddress = true, MulticastGroupToReceive = "234.1.2.3", MulticastLoopback = true }; ManualResetEvent aMessage1Received = new ManualResetEvent(false); string aReceivedMessage1 = null; IDuplexOutputChannel anOutputChannel1 = aMessaging.CreateDuplexOutputChannel("udp://127.0.0.1:8090/", "udp://127.0.0.1:8092/"); anOutputChannel1.ResponseMessageReceived += (x, y) => { aReceivedMessage1 = (string)y.Message; aMessage1Received.Set(); }; ManualResetEvent aMessage2Received = new ManualResetEvent(false); string aReceivedMessage2 = null; IDuplexOutputChannel anOutputChannel2 = aMessaging.CreateDuplexOutputChannel("udp://127.0.0.1:8090/", "udp://127.0.0.1:8092/"); anOutputChannel2.ResponseMessageReceived += (x, y) => { aReceivedMessage2 = (string)y.Message; aMessage2Received.Set(); }; IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("udp://127.0.0.1:8090/"); try { anInputChannel.StartListening(); anOutputChannel1.OpenConnection(); anOutputChannel2.OpenConnection(); anInputChannel.SendResponseMessage("udp://234.1.2.3:8092/", "Hello"); aMessage1Received.WaitIfNotDebugging(1000); aMessage2Received.WaitIfNotDebugging(1000); } finally { anInputChannel.StopListening(); anOutputChannel1.CloseConnection(); anOutputChannel2.CloseConnection(); } Assert.AreEqual("Hello", aReceivedMessage1); Assert.AreEqual("Hello", aReceivedMessage2); }
public void StartWorker4504Server() { // Create TCP based messaging. IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); Worker4504InputChannel = aMessaging.CreateDuplexInputChannel("tcp://127.0.0.1:4504"); Worker4504InputChannel.MessageReceived += Worker4504InputChannel_MessageReceived; Worker4504InputChannel.ResponseReceiverConnected += ClientConnected; Worker4504InputChannel.ResponseReceiverDisconnected += ClientDisconnected; //Worker4504InputChannel.ResponseReceiverConnected += Worker4504InputChannel_ResponseReceiverConnected; //Worker4504InputChannel.ResponseReceiverDisconnected += Worker4504InputChannel_ResponseReceiverDisconnected; Worker4504InputChannel.StartListening(); Logger.Info("Worker server 4504 started"); }
public void A14_ResponseReceiverReconnects_AfterDisconnect() { // Duplex output channel without queue - it will not try to reconnect. IDuplexOutputChannel aDuplexOutputChannel = MessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexInputChannel aDuplexInputChannel = MessagingSystem.CreateDuplexInputChannel(ChannelId); AutoResetEvent aConnectionsCompletedEvent = new AutoResetEvent(false); List <string> anOpenConnections = new List <string>(); aDuplexInputChannel.ResponseReceiverConnected += (x, y) => { lock (anOpenConnections) { anOpenConnections.Add(y.ResponseReceiverId); aConnectionsCompletedEvent.Set(); } }; try { aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); // Wait until the connection is open. aConnectionsCompletedEvent.WaitOne(); // Disconnect the response receiver. aDuplexInputChannel.DisconnectResponseReceiver(aDuplexOutputChannel.ResponseReceiverId); // The duplex output channel will try to connect again, therefore wait until connected. aConnectionsCompletedEvent.WaitOne(); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } Assert.AreEqual(2, anOpenConnections.Count); // Both connections should be same. Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, anOpenConnections[0]); Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, anOpenConnections[1]); }
public void MaxAmountOfConnections() { IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory() { MaxAmountOfConnections = 2 }; IDuplexOutputChannel anOutputChannel1 = aMessaging.CreateDuplexOutputChannel("udp://127.0.0.1:8049/"); IDuplexOutputChannel anOutputChannel2 = aMessaging.CreateDuplexOutputChannel("udp://127.0.0.1:8049/"); IDuplexOutputChannel anOutputChannel3 = aMessaging.CreateDuplexOutputChannel("udp://127.0.0.1:8049/"); IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("udp://127.0.0.1:8049/"); try { ManualResetEvent aConnectionClosed = new ManualResetEvent(false); anOutputChannel3.ConnectionClosed += (x, y) => { EneterTrace.Info("Connection closed."); aConnectionClosed.Set(); }; anInputChannel.StartListening(); anOutputChannel1.OpenConnection(); anOutputChannel2.OpenConnection(); anOutputChannel3.OpenConnection(); if (!aConnectionClosed.WaitOne(1000)) { Assert.Fail("Third connection was not closed."); } Assert.IsTrue(anOutputChannel1.IsConnected); Assert.IsTrue(anOutputChannel2.IsConnected); Assert.IsFalse(anOutputChannel3.IsConnected); } finally { anOutputChannel1.CloseConnection(); anOutputChannel2.CloseConnection(); anOutputChannel3.CloseConnection(); anInputChannel.StopListening(); } }
public void B01_Pinging_StopListening() { IDuplexInputChannel aDuplexInputChannel = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId); IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId); AutoResetEvent aDisconnectedEvent = new AutoResetEvent(false); bool aDisconnectedFlag = false; aDuplexOutputChannel.ConnectionClosed += (x, y) => { aDisconnectedFlag = true; aDisconnectedEvent.Set(); }; try { // Start listening. aDuplexInputChannel.StartListening(); // Start pinging and wait 5 seconds. aDuplexOutputChannel.OpenConnection(); Thread.Sleep(5000); Assert.IsFalse(aDisconnectedFlag); // Stop listener, therefore the ping response will not come and the channel should indicate the disconnection. aDuplexInputChannel.StopListening(); aDisconnectedEvent.WaitOne(); Assert.IsTrue(aDisconnectedFlag); Assert.IsFalse(aDuplexOutputChannel.IsConnected); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } }
public void B04_Pinging_CloseConnection() { IDuplexInputChannel aDuplexInputChannel = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId); IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId); AutoResetEvent aConnectionClosedEvent = new AutoResetEvent(false); string aClosedResponseReceiverId = ""; aDuplexInputChannel.ResponseReceiverDisconnected += (x, y) => { aClosedResponseReceiverId = y.ResponseReceiverId; aConnectionClosedEvent.Set(); }; try { // Start listening. aDuplexInputChannel.StartListening(); // Allow some time for pinging. aDuplexOutputChannel.OpenConnection(); Thread.Sleep(2000); Assert.AreEqual("", aClosedResponseReceiverId); // Close connection. Therefore the duplex input channel will not get any pings anymore. aDuplexOutputChannel.CloseConnection(); aConnectionClosedEvent.WaitOne(); Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, aClosedResponseReceiverId); Assert.IsFalse(aDuplexOutputChannel.IsConnected); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } }
public void B02_Pinging_DisconnectResponseReceiver() { IDuplexInputChannel aDuplexInputChannel = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId); IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId); AutoResetEvent aDisconnectedEvent = new AutoResetEvent(false); bool aDisconnectedFlag = false; aDuplexOutputChannel.ConnectionClosed += (x, y) => { aDisconnectedFlag = true; aDisconnectedEvent.Set(); }; try { // Start listening. aDuplexInputChannel.StartListening(); // Allow some time for pinging. aDuplexOutputChannel.OpenConnection(); Thread.Sleep(2000); Assert.IsFalse(aDisconnectedFlag); // Disconnect the duplex output channel. aDuplexInputChannel.DisconnectResponseReceiver(aDuplexOutputChannel.ResponseReceiverId); aDisconnectedEvent.WaitOne(); Assert.IsTrue(aDisconnectedFlag); Assert.IsFalse(aDuplexOutputChannel.IsConnected); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } }
public virtual void ConnectionNotGranted() { IDuplexInputChannel anInputChannel = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId); IDuplexOutputChannel anOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId); try { myConnectionNotGranted = true; anInputChannel.StartListening(); // Client opens the connection. Assert.Throws <InvalidOperationException>(() => anOutputChannel.OpenConnection()); } finally { myConnectionNotGranted = false; anOutputChannel.CloseConnection(); anInputChannel.StopListening(); } }
public virtual void AuthenticationTimeout() { IDuplexInputChannel anInputChannel = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId); IDuplexOutputChannel anOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId); try { myAuthenticationSleep = TimeSpan.FromMilliseconds(3000); anInputChannel.StartListening(); // Client opens the connection. Assert.Throws <TimeoutException>(() => anOutputChannel.OpenConnection()); } finally { myAuthenticationSleep = TimeSpan.FromMilliseconds(0); anOutputChannel.CloseConnection(); anInputChannel.StopListening(); } }
public void TestPortAvailability() { IDuplexInputChannel anInputChannel1 = MessagingSystemFactory.CreateDuplexInputChannel("udp://[::1]:8045/"); IDuplexInputChannel anInputChannel2 = MessagingSystemFactory.CreateDuplexInputChannel("udp://127.0.0.1:8045/"); try { anInputChannel1.StartListening(); anInputChannel2.StartListening(); bool aResult = UdpMessagingSystemFactory.IsPortAvailableForUdpListening("tcp://[::1]:8045/"); Assert.IsFalse(aResult); aResult = UdpMessagingSystemFactory.IsPortAvailableForUdpListening("tcp://0.0.0.0:8046/"); Assert.IsTrue(aResult); } finally { anInputChannel1.StopListening(); anInputChannel2.StopListening(); } }
public void B03_Pinging_NoResponseForPing() { // Create duplex input channel which will not send ping messages. IDuplexInputChannel aDuplexInputChannel = UnderlyingMessaging.CreateDuplexInputChannel(ChannelId); IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId); AutoResetEvent aDisconnectedEvent = new AutoResetEvent(false); bool aDisconnectedFlag = false; aDuplexOutputChannel.ConnectionClosed += (x, y) => { aDisconnectedFlag = true; aDisconnectedEvent.Set(); }; try { // Start listening. aDuplexInputChannel.StartListening(); // Allow some time for pinging. aDuplexOutputChannel.OpenConnection(); Assert.IsTrue(aDuplexOutputChannel.IsConnected); Thread.Sleep(2000); aDisconnectedEvent.WaitOne(); Assert.IsTrue(aDisconnectedFlag); Assert.IsFalse(aDuplexOutputChannel.IsConnected); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } }
public virtual void AuthenticationCancelledByClient() { IDuplexInputChannel anInputChannel = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId); IDuplexOutputChannel anOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId); try { myClientCancelAuthentication = true; anInputChannel.StartListening(); Exception anException = null; try { // Client opens the connection. anOutputChannel.OpenConnection(); } catch (Exception err) { anException = err; } Assert.IsInstanceOf <InvalidOperationException>(anException); // Check that the AuthenticationCancelled calleback was called. Assert.IsTrue(myAuthenticationCancelled); } finally { myClientCancelAuthentication = false; myAuthenticationCancelled = false; anOutputChannel.CloseConnection(); anInputChannel.StopListening(); } }
public virtual void AttachDuplexInputChannel(IDuplexInputChannel duplexInputChannel) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myDuplexInputChannelContextManipulatorLock)) { Attach(duplexInputChannel); try { duplexInputChannel.StartListening(); } catch (Exception err) { duplexInputChannel.ResponseReceiverDisconnected -= OnDuplexInputChannelResponseReceiverDisconnected; duplexInputChannel.MessageReceived -= OnMessageReceived; myDuplexInputChannelContexts.RemoveWhere(x => x.AttachedDuplexInputChannel.ChannelId == duplexInputChannel.ChannelId); EneterTrace.Error(TracedObject + "failed to attach the duplex input channel '" + duplexInputChannel.ChannelId + "'.", err); throw; } } } }
public void A16_RequestResponse_100_ConstantlyInterrupted() { IDuplexOutputChannel aDuplexOutputChannel = MessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexInputChannel aDuplexInputChannel = MessagingSystem.CreateDuplexInputChannel(ChannelId); IDuplexInputChannel anUnderlyingDuplexInputChannel = aDuplexInputChannel.GetField <IDuplexInputChannel>("myInputChannel"); Assert.NotNull(anUnderlyingDuplexInputChannel); AutoResetEvent anAllMessagesProcessedEvent = new AutoResetEvent(false); // Received messages. List <int> aReceivedMessages = new List <int>(); aDuplexInputChannel.MessageReceived += (x, y) => { lock (aReceivedMessages) { string aReceivedMessage = y.Message as string; EneterTrace.Info("Received message: " + aReceivedMessage); int k = int.Parse(aReceivedMessage); aReceivedMessages.Add(k); k += 1000; EneterTrace.Info("Sent response message: " + k.ToString()); aDuplexInputChannel.SendResponseMessage(y.ResponseReceiverId, k.ToString()); } }; aDuplexOutputChannel.ConnectionClosed += (x, y) => { EneterTrace.Info("ConnectionClosed invoked in duplex output channel"); // The buffered duplex output channel exceeded the max offline time. anAllMessagesProcessedEvent.Set(); }; // Received response messages. List <int> aReceivedResponseMessages = new List <int>(); aDuplexOutputChannel.ResponseMessageReceived += (x, y) => { lock (aReceivedResponseMessages) { string aReceivedMessage = y.Message as string; EneterTrace.Info("Received response message: " + aReceivedMessage); int k = int.Parse(aReceivedMessage); aReceivedResponseMessages.Add(k); if (aReceivedResponseMessages.Count == 100) { anAllMessagesProcessedEvent.Set(); } } }; try { bool aTestFinishedFlag = false; aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); Thread anInteruptingThread = new Thread(() => { for (int i = 0; i < 100 && !aTestFinishedFlag; ++i) { anUnderlyingDuplexInputChannel.DisconnectResponseReceiver(aDuplexOutputChannel.ResponseReceiverId); Thread.Sleep(ConnectionInterruptionFrequency); } }); // Start constant disconnecting. anInteruptingThread.Start(); for (int i = 0; i < 100; ++i) { aDuplexOutputChannel.SendMessage(i.ToString()); } // Wait until all messages are processed. //anAllMessagesProcessedEvent.WaitOne(); Assert.IsTrue(anAllMessagesProcessedEvent.WaitOne(20000), "The timeout occured."); aTestFinishedFlag = true; anInteruptingThread.Join(); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } aReceivedMessages.Sort(); Assert.AreEqual(100, aReceivedMessages.Count); for (int i = 0; i < 100; ++i) { Assert.AreEqual(i, aReceivedMessages[i]); } aReceivedResponseMessages.Sort(); Assert.AreEqual(100, aReceivedResponseMessages.Count); for (int i = 0; i < 100; ++i) { Assert.AreEqual(i + 1000, aReceivedResponseMessages[i]); } }
public void A01_SimpleRequestResponse() { IDuplexOutputChannel aDuplexOutputChannel = MessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexInputChannel aDuplexInputChannel = MessagingSystem.CreateDuplexInputChannel(ChannelId); // Received messages. List <int> aReceivedMessages = new List <int>(); aDuplexInputChannel.MessageReceived += (x, y) => { //EneterTrace.Info("Message Received"); lock (aReceivedMessages) { string aReceivedMessage = y.Message as string; int k = int.Parse(aReceivedMessage); aReceivedMessages.Add(k); k += 1000; aDuplexInputChannel.SendResponseMessage(y.ResponseReceiverId, k.ToString()); } }; // Received response messages. List <int> aReceivedResponseMessages = new List <int>(); AutoResetEvent anAllMessagesProcessedEvent = new AutoResetEvent(false); aDuplexOutputChannel.ResponseMessageReceived += (x, y) => { //EneterTrace.Info("Response Received"); lock (aReceivedResponseMessages) { string aReceivedMessage = y.Message as string; int k = int.Parse(aReceivedMessage); aReceivedResponseMessages.Add(k); if (k == 1019) { anAllMessagesProcessedEvent.Set(); } } }; try { aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); for (int i = 0; i < 20; ++i) { aDuplexOutputChannel.SendMessage(i.ToString()); } // Wait untill all messages are processed. anAllMessagesProcessedEvent.WaitOne(); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } aReceivedMessages.Sort(); Assert.AreEqual(20, aReceivedMessages.Count); for (int i = 0; i < 20; ++i) { Assert.AreEqual(i, aReceivedMessages[i]); } aReceivedResponseMessages.Sort(); Assert.AreEqual(20, aReceivedResponseMessages.Count); for (int i = 0; i < 20; ++i) { Assert.AreEqual(i + 1000, aReceivedResponseMessages[i]); } }
public void B01_InactivityTimeout() { // Set the polling frequency slower // than inactivity timeout in the duplex input channel. // Therefore the timeout should occur before the polling - this is how the // inactivity is simulated in this test. IMessagingSystemFactory aMessagingSystem = new HttpMessagingSystemFactory(3000, 2000); IDuplexOutputChannel anOutputChannel1 = aMessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexOutputChannel anOutputChannel2 = aMessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexInputChannel anInputChannel = aMessagingSystem.CreateDuplexInputChannel(ChannelId); AutoResetEvent aConncetionEvent = new AutoResetEvent(false); AutoResetEvent aDisconncetionEvent = new AutoResetEvent(false); List <TConnectionEvent> aConnections = new List <TConnectionEvent>(); anInputChannel.ResponseReceiverConnected += (x, y) => { aConnections.Add(new TConnectionEvent(DateTime.Now, y.ResponseReceiverId)); aConncetionEvent.Set(); }; List <TConnectionEvent> aDisconnections = new List <TConnectionEvent>(); anInputChannel.ResponseReceiverDisconnected += (x, y) => { aDisconnections.Add(new TConnectionEvent(DateTime.Now, y.ResponseReceiverId)); aDisconncetionEvent.Set(); }; try { anInputChannel.StartListening(); Assert.IsTrue(anInputChannel.IsListening); // Create the 1st connection. anOutputChannel1.OpenConnection(); Assert.IsTrue(anOutputChannel1.IsConnected); aConncetionEvent.WaitOne(); Assert.AreEqual(1, aConnections.Count); Assert.IsFalse(string.IsNullOrEmpty(aConnections[0].ReceiverId)); Thread.Sleep(1000); // Create the 2nd connection. anOutputChannel2.OpenConnection(); Assert.IsTrue(anOutputChannel2.IsConnected); aConncetionEvent.WaitOne(); Assert.AreEqual(2, aConnections.Count); Assert.IsFalse(string.IsNullOrEmpty(aConnections[1].ReceiverId)); // Wait for the 1st disconnection aDisconncetionEvent.WaitOne(); Assert.AreEqual(1, aDisconnections.Count); Assert.AreEqual(aConnections[0].ReceiverId, aDisconnections[0].ReceiverId); Assert.IsTrue(aDisconnections[0].Time - aConnections[0].Time > TimeSpan.FromMilliseconds(1900)); // Wait for the 2nd disconnection aDisconncetionEvent.WaitOne(); Assert.AreEqual(2, aDisconnections.Count); Assert.AreEqual(aConnections[1].ReceiverId, aDisconnections[1].ReceiverId); Assert.IsTrue(aDisconnections[1].Time - aConnections[1].Time > TimeSpan.FromMilliseconds(1900)); } finally { anOutputChannel1.CloseConnection(); anOutputChannel2.CloseConnection(); anInputChannel.StopListening(); } }
public void A01_Reconnect_DisconnectReceiver() { IDuplexInputChannel aDuplexInputChannel = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId); IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId); Reconnecter aReconnecter = new Reconnecter(aDuplexOutputChannel, TimeSpan.FromMilliseconds(300), -1); AutoResetEvent aReconnectEvent = new AutoResetEvent(false); DuplexChannelEventArgs aReconnectEventArgs = null; aReconnecter.ConnectionOpened += (x, y) => { aReconnectEventArgs = y; aReconnectEvent.Set(); }; AutoResetEvent aDisconnectEvent = new AutoResetEvent(false); DuplexChannelEventArgs aDisconnectEventArgs = null; aReconnecter.ConnectionClosed += (x, y) => { aDisconnectEventArgs = y; aDisconnectEvent.Set(); }; AutoResetEvent aConnectionOpenedEvent = new AutoResetEvent(false); aDuplexOutputChannel.ConnectionOpened += (x, y) => { aConnectionOpenedEvent.Set(); }; AutoResetEvent aConnectionClosedEvent = new AutoResetEvent(false); aDuplexOutputChannel.ConnectionClosed += (x, y) => { aConnectionClosedEvent.Set(); }; try { aReconnecter.EnableReconnecting(); // Start listening. aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); Thread.Sleep(2000); Assert.IsTrue(aDuplexOutputChannel.IsConnected); aConnectionOpenedEvent.WaitOne(); // Disconnect the duplexoutput channel. aDuplexInputChannel.DisconnectResponseReceiver(aDuplexOutputChannel.ResponseReceiverId); // Wait until the disconnect is notified. aDisconnectEvent.WaitOne(); aConnectionClosedEvent.WaitOne(); Assert.AreEqual(aDuplexOutputChannel.ChannelId, aDisconnectEventArgs.ChannelId); Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, aDisconnectEventArgs.ResponseReceiverId); // Wait until the reconnecter opens connection again. aReconnectEvent.WaitOne(); Assert.AreEqual(aDuplexOutputChannel.ChannelId, aReconnectEventArgs.ChannelId); Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, aReconnectEventArgs.ResponseReceiverId); Assert.IsTrue(aDuplexOutputChannel.IsConnected); aConnectionOpenedEvent.WaitOne(); } finally { aReconnecter.DisableReconnecting(); aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } }
public void A02_Reconnect_StopListening() { MonitoredMessagingFactory aConnectionMonitor = new MonitoredMessagingFactory(MessagingSystemFactory); IDuplexInputChannel aDuplexInputChannel = aConnectionMonitor.CreateDuplexInputChannel(ChannelId); IDuplexOutputChannel aDuplexOutputChannel = aConnectionMonitor.CreateDuplexOutputChannel(ChannelId); // Max 5 attempts to try to reconnect. Reconnecter aReconnecter = new Reconnecter(aDuplexOutputChannel, TimeSpan.FromMilliseconds(300), 5); AutoResetEvent aReconnectEvent = new AutoResetEvent(false); DuplexChannelEventArgs aReconnectEventArgs = null; aReconnecter.ConnectionOpened += (x, y) => { aReconnectEventArgs = y; aReconnectEvent.Set(); }; AutoResetEvent aDisconnectEvent = new AutoResetEvent(false); DuplexChannelEventArgs aDisconnectEventArgs = null; aReconnecter.ConnectionClosed += (x, y) => { aDisconnectEventArgs = y; aDisconnectEvent.Set(); }; AutoResetEvent aReconnecFailedEvent = new AutoResetEvent(false); DuplexChannelEventArgs aReconnectingFailedEventArgs = null; aReconnecter.ReconnectingFailed += (x, y) => { aReconnectingFailedEventArgs = y; aReconnecFailedEvent.Set(); }; try { aReconnecter.EnableReconnecting(); // Start listening. aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); Thread.Sleep(2000); Assert.IsTrue(aDuplexOutputChannel.IsConnected); Assert.IsNull(aReconnectingFailedEventArgs); Assert.IsNull(aDisconnectEventArgs); Assert.IsNull(aReconnectEventArgs); // Stop listening. // Note: The Reconnecter will try 5 times to reopen the connection, then it will notify, the reconnecion failed. aDuplexInputChannel.StopListening(); // Wait until the disconnect is notified. aDisconnectEvent.WaitOne(); Assert.AreEqual(aDuplexOutputChannel.ChannelId, aDisconnectEventArgs.ChannelId); Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, aDisconnectEventArgs.ResponseReceiverId); Assert.IsFalse(aDuplexOutputChannel.IsConnected); Assert.IsNull(aReconnectEventArgs); // Wait until the reconnect fails after 5 failed reconnects. aReconnecFailedEvent.WaitOne(); Assert.AreEqual(aDuplexOutputChannel.ChannelId, aReconnectingFailedEventArgs.ChannelId); Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, aReconnectingFailedEventArgs.ResponseReceiverId); Assert.IsFalse(aDuplexOutputChannel.IsConnected); Assert.IsNull(aReconnectEventArgs); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); aReconnecter.DisableReconnecting(); } }