public void DisposeTest() { ManualResetEvent delivered = new ManualResetEvent(false); int deliveredMessagesCount = 0; var transport = new InMemoryTransport(); IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_TOPIC, (message, ack) => { delivered.WaitOne(); Interlocked.Increment(ref deliveredMessagesCount); }, null); messagingSession.Send(TEST_TOPIC, new BinaryMessage(), 0); Thread.Sleep(200); var task = Task.Factory.StartNew(transport.Dispose); Assert.That(task.Wait(200), Is.False, "transport was disposd before all message processing finished"); delivered.Set(); Assert.That(task.Wait(1000), Is.True, "transport was not disposd after all message processing finished"); }
public IDisposable Subscribe( IMessagingSession messagingSession, string destination, Action <BinaryMessage, Action <bool> > callback, string messageType, int priority) { if (m_IsDisposing) { throw new ObjectDisposedException("ProcessingGroup " + Name); } var taskFactory = m_SchedulingStrategy.GetTaskFactory(priority); var subscription = new SingleAssignmentDisposable(); subscription.Disposable = messagingSession.Subscribe( destination, (message, ack) => { Interlocked.Increment(ref m_TasksInProgress); taskFactory.StartNew(() => { Interlocked.Increment(ref m_ReceivedMessages); //if subscription is disposed unack message immediately if (subscription.IsDisposed) { ack(false); } else { callback(message, ack); Interlocked.Increment(ref m_ProcessedMessages); } Interlocked.Decrement(ref m_TasksInProgress); }, TaskCreationOptions.HideScheduler); }, messageType); return(subscription); }
public IDisposable Subscribe(string destination, Action <BinaryMessage, Action <bool> > callback, string messageType) { return(_messagingSession.Subscribe(destination, callback, messageType)); }