/// <summary> /// Tests LocalListen with projection. /// </summary> private void TestLocalListenProjection(object topic) { var grid3GotMessage = false; var grid3Listener = new MessageListener <string>((id, x) => { grid3GotMessage = true; return(true); }); _grid3.GetMessaging().LocalListen(grid3Listener, topic); var clusterMessaging = _grid1.GetCluster().ForNodes(_grid1.GetCluster().GetLocalNode(), _grid2.GetCluster().GetLocalNode()).GetMessaging(); var clusterListener = MessagingTestHelper.GetListener(); clusterMessaging.LocalListen(clusterListener, topic); CheckSend(msg: clusterMessaging, topic: topic); Assert.IsFalse(grid3GotMessage, "Grid3 should not get messages"); CheckSend(grid: _grid2, msg: clusterMessaging, topic: topic); Assert.IsFalse(grid3GotMessage, "Grid3 should not get messages"); clusterMessaging.StopLocalListen(clusterListener, topic); _grid3.GetMessaging().StopLocalListen(grid3Listener, topic); }
/// <summary> /// Tests RemoteListen. /// </summary> private void TestRemoteListen(object topic, bool async = false) { var messaging = _grid1.GetMessaging(); var listener = MessagingTestHelper.GetListener("first"); var listenId = async ? messaging.RemoteListenAsync(listener, topic).Result : messaging.RemoteListen(listener, topic); // Test sending CheckSend(topic, msg: messaging, remoteListen: true); // Test different topic CheckNoMessage(NextId()); // Test multiple subscriptions for the same filter var listener2 = MessagingTestHelper.GetListener("second"); var listenId2 = async ? messaging.RemoteListenAsync(listener2, topic).Result : messaging.RemoteListen(listener2, topic); CheckSend(topic, msg: messaging, remoteListen: true, repeatMultiplier: 2); // expect twice the messages if (async) { messaging.StopRemoteListenAsync(listenId2).Wait(); } else { messaging.StopRemoteListen(listenId2); } // Wait for all to unsubscribe: StopRemoteListen (both sync and async) does not remove remote listeners // upon exit. Remote listeners are removed with disco messages after some delay - // see TestStopRemoteListenRemovesAllCallbacksUponExit. TestUtils.AssertHandleRegistryHasItems( (int)MessagingTestHelper.SleepTimeout.TotalMilliseconds, 1, _grid1, _grid2, _grid3); CheckSend(topic, msg: messaging, remoteListen: true); // back to normal after unsubscription // Test message type mismatch var ex = Assert.Throws <IgniteException>(() => messaging.Send(1.1, topic)); Assert.AreEqual("Unable to cast object of type 'System.Double' to type 'System.String'.", ex.Message); // Test end listen if (async) { messaging.StopRemoteListenAsync(listenId).Wait(); } else { messaging.StopRemoteListen(listenId); } CheckNoMessage(topic); }
/// <summary> /// Checks that no message has arrived. /// </summary> private void CheckNoMessage(object topic, IIgnite grid = null) { // this will result in an exception in case of a message MessagingTestHelper.ClearReceived(0); (grid ?? _grid1).GetMessaging().SendAll(NextMessage(), topic); Thread.Sleep(MessagingTestHelper.SleepTimeout); MessagingTestHelper.AssertFailures(); }
public void TestRemoteListenMultithreaded() { const int threadCnt = 20; const int runSeconds = 20; var messaging = _grid1.GetMessaging(); var senders = Task.Factory.StartNew(() => TestUtils.RunMultiThreaded(() => { MessagingTestHelper.ClearReceived(int.MaxValue); messaging.Send(NextMessage()); Thread.Sleep(50); }, threadCnt, runSeconds)); var sharedListener = MessagingTestHelper.GetListener(); for (int i = 0; i < 100; i++) { messaging.RemoteListen(sharedListener); // add some listeners to be stopped by filter result } TestUtils.RunMultiThreaded(() => { // Check that listen/stop work concurrently messaging.StopRemoteListen(messaging.RemoteListen(sharedListener)); }, threadCnt, runSeconds / 2); MessagingTestHelper.ListenResult = false; messaging.Send(NextMessage()); // send a message to make filters return false Thread.Sleep(MessagingTestHelper.SleepTimeout); // wait for all to unsubscribe MessagingTestHelper.ListenResult = true; senders.Wait(); // wait for senders to stop MessagingTestHelper.ClearReceived(int.MaxValue); var lastMsg = NextMessage(); messaging.Send(lastMsg); Thread.Sleep(MessagingTestHelper.SleepTimeout); // Check that unsubscription worked properly var sharedResult = MessagingTestHelper.ReceivedMessages.ToArray(); if (sharedResult.Length != 0) { Assert.Fail("Unexpected messages ({0}): {1}; last sent message: {2}", sharedResult.Length, string.Join(",", sharedResult), lastMsg); } }
/// <summary> /// Tests RemoteListen with a projection. /// </summary> private void TestRemoteListenProjection(object topic) { var clusterMessaging = _grid1.GetCluster().ForNodes(_grid1.GetCluster().GetLocalNode(), _grid2.GetCluster().GetLocalNode()).GetMessaging(); var clusterListener = MessagingTestHelper.GetListener(); var listenId = clusterMessaging.RemoteListen(clusterListener, topic); CheckSend(msg: clusterMessaging, topic: topic, remoteListen: true); clusterMessaging.StopRemoteListen(listenId); CheckNoMessage(topic); }
/// <summary> /// Tests RemoteListen. /// </summary> public void TestRemoteListen(object topic, bool async = false) { var messaging = async ? _grid1.Message().WithAsync() : _grid1.Message(); var listener = MessagingTestHelper.GetListener(); var listenId = messaging.RemoteListen(listener, topic); if (async) { listenId = messaging.GetFuture <Guid>().Get(); } // Test sending CheckSend(topic, msg: messaging, remoteListen: true); // Test different topic CheckNoMessage(NextId()); // Test multiple subscriptions for the same filter var listenId2 = messaging.RemoteListen(listener, topic); if (async) { listenId2 = messaging.GetFuture <Guid>().Get(); } CheckSend(topic, msg: messaging, remoteListen: true, repeatMultiplier: 2); // expect twice the messages messaging.StopRemoteListen(listenId2); if (async) { messaging.GetFuture().Get(); } CheckSend(topic, msg: messaging, remoteListen: true); // back to normal after unsubscription // Test message type mismatch var ex = Assert.Throws <IgniteException>(() => messaging.Send(1.1, topic)); Assert.AreEqual("Unable to cast object of type 'System.Double' to type 'System.String'.", ex.Message); // Test end listen messaging.StopRemoteListen(listenId); if (async) { messaging.GetFuture().Get(); } CheckNoMessage(topic); }
public void TearDown() { try { TestUtils.AssertHandleRegistryIsEmpty(1000, _grid1, _grid2, _grid3); MessagingTestHelper.AssertFailures(); } finally { // Stop all grids between tests to drop any hanging messages Ignition.StopAll(true); } }
public void TestStopRemoteListenRemovesAllCallbacksUponExit() { const string topic = "topic"; var messaging = _grid1.GetMessaging(); var listenId = messaging.RemoteListen(MessagingTestHelper.GetListener("first"), topic); TestUtils.AssertHandleRegistryHasItems(-1, 1, _grid1, _grid2, _grid3); messaging.Send(1, topic); messaging.StopRemoteListen(listenId); TestUtils.AssertHandleRegistryHasItems(-1, 0, _grid1, _grid2, _grid3); }
/// <summary> /// Sends messages in various ways and verefies correct receival. /// </summary> /// <param name="topic">Topic.</param> /// <param name="grid">The grid to use.</param> /// <param name="msg">Messaging to use.</param> /// <param name="remoteListen">Whether to expect remote listeners.</param> /// <param name="single">When true, only check one message.</param> /// <param name="repeatMultiplier">Expected message count multiplier.</param> private void CheckSend(object topic = null, IIgnite grid = null, IMessaging msg = null, bool remoteListen = false, bool single = false, int repeatMultiplier = 1) { IClusterGroup cluster; if (msg != null) { cluster = msg.ClusterGroup; } else { grid = grid ?? _grid1; msg = grid.GetMessaging(); cluster = grid.GetCluster().ForLocal(); } // Messages will repeat due to multiple nodes listening var expectedRepeat = repeatMultiplier * (remoteListen ? cluster.GetNodes().Count : 1); var messages = Enumerable.Range(1, 10).Select(x => NextMessage()).OrderBy(x => x).ToList(); // Single message MessagingTestHelper.ClearReceived(expectedRepeat); msg.Send(messages[0], topic); MessagingTestHelper.VerifyReceive(cluster, messages.Take(1), m => m.ToList(), expectedRepeat); if (single) { return; } // Multiple messages (receive order is undefined) MessagingTestHelper.ClearReceived(messages.Count * expectedRepeat); msg.SendAll(messages, topic); MessagingTestHelper.VerifyReceive(cluster, messages, m => m.OrderBy(x => x), expectedRepeat); // Multiple messages, ordered MessagingTestHelper.ClearReceived(messages.Count * expectedRepeat); messages.ForEach(x => msg.SendOrdered(x, topic, MessagingTestHelper.MessageTimeout)); if (remoteListen) // in remote scenario messages get mixed up due to different timing on different nodes { MessagingTestHelper.VerifyReceive(cluster, messages, m => m.OrderBy(x => x), expectedRepeat); } else { MessagingTestHelper.VerifyReceive(cluster, messages, m => m.Reverse(), expectedRepeat); } }
private void TestLocalListen(object topic) { var messaging = _grid1.GetMessaging(); var listener = MessagingTestHelper.GetListener(); messaging.LocalListen(listener, topic); // Test sending CheckSend(topic); CheckSend(topic, _grid2); CheckSend(topic, _grid3); // Test different topic CheckNoMessage(NextId()); CheckNoMessage(NextId(), _grid2); // Test multiple subscriptions for the same filter messaging.LocalListen(listener, topic); messaging.LocalListen(listener, topic); CheckSend(topic, repeatMultiplier: 3); // expect all messages repeated 3 times messaging.StopLocalListen(listener, topic); CheckSend(topic, repeatMultiplier: 2); // expect all messages repeated 2 times messaging.StopLocalListen(listener, topic); CheckSend(topic); // back to 1 listener // Test message type mismatch var ex = Assert.Throws <IgniteException>(() => messaging.Send(1.1, topic)); Assert.AreEqual("Unable to cast object of type 'System.Double' to type 'System.String'.", ex.Message); // Test end listen MessagingTestHelper.ListenResult = false; CheckSend(topic, single: true); // we'll receive one more and then unsubscribe because of delegate result. CheckNoMessage(topic); // Start again MessagingTestHelper.ListenResult = true; messaging.LocalListen(listener, topic); CheckSend(topic); // Stop messaging.StopLocalListen(listener, topic); CheckNoMessage(topic); }
public void TestRemoteListenMultithreaded() { const int threadCnt = 20; const int runSeconds = 20; var messaging = _grid1.GetMessaging(); var senders = Task.Factory.StartNew(() => TestUtils.RunMultiThreaded(() => { MessagingTestHelper.ClearReceived(int.MaxValue); messaging.Send(NextMessage()); Thread.Sleep(50); }, threadCnt, runSeconds)); var sharedListener = MessagingTestHelper.GetListener(); for (int i = 0; i < 100; i++) { messaging.RemoteListen(sharedListener); // add some listeners to be stopped by filter result } TestUtils.RunMultiThreaded(() => { // Check that listen/stop work concurrently messaging.StopRemoteListen(messaging.RemoteListen(sharedListener)); }, threadCnt, runSeconds); MessagingTestHelper.ListenResult = false; messaging.Send(NextMessage()); // send a message to make filters return false Thread.Sleep(MessagingTestHelper.MessageTimeout); // wait for all to unsubscribe MessagingTestHelper.ListenResult = true; senders.Wait(); // wait for senders to stop var sharedResult = MessagingTestHelper.ReceivedMessages.Count; messaging.Send(NextMessage()); Thread.Sleep(MessagingTestHelper.MessageTimeout); // Check that unsubscription worked properly Assert.AreEqual(sharedResult, MessagingTestHelper.ReceivedMessages.Count); }