public async Task Client_Should_be_able_to_unsub_from_a_subject() { var subject = Context.GenerateSubject(); var nr1Received = new ConcurrentQueue <MsgOp>(); var nr2Received = new ConcurrentQueue <MsgOp>(); var nr3Received = new ConcurrentQueue <MsgOp>(); var subInfo1 = new SubscriptionInfo(subject); var subInfo2 = new SubscriptionInfo(subject); var subInfo3 = new SubscriptionInfo(subject); _sync = Sync.MaxThree(); await ConnectAllClients(); _client1.OpStream.OfType <MsgOp>().Subscribe(msg => { _client1.Unsub(subInfo1); nr1Received.Enqueue(msg); _sync.Release(msg); }); _client1.Sub(subInfo1); _client2.OpStream.OfType <MsgOp>().Subscribe(async msg => { await _client2.UnsubAsync(subInfo2); nr2Received.Enqueue(msg); _sync.Release(msg); }); _client2.Sub(subInfo2); _client3.OpStream.OfType <MsgOp>().Subscribe(msg => { nr3Received.Enqueue(msg); _sync.Release(msg); }); _client3.Sub(subInfo3); await Context.DelayAsync(); _client1.Pub(subject, "mess1"); _sync.WaitForAll(); _client3.Unsub(subInfo3); await Context.DelayAsync(); _client1.Pub(subject, "mess2"); await Context.DelayAsync(); _sync.InterceptedCount.Should().Be(3); nr1Received.Should().HaveCount(1); nr2Received.Should().HaveCount(1); nr3Received.Should().HaveCount(1); }
public async Task Client_Should_be_able_to_unsub_from_a_subject() { const string subject = "Test"; var nr1ReceiveCount = 0; var nr2ReceiveCount = 0; var nr3ReceiveCount = 0; var subInfo1 = new SubscriptionInfo(subject); var subInfo2 = new SubscriptionInfo(subject); var subInfo3 = new SubscriptionInfo(subject); _client1.OpStream.OfType <MsgOp>().Subscribe(msg => { _client1.Unsub(subInfo1); Interlocked.Increment(ref nr1ReceiveCount); ReleaseOne(); }); _client1.Sub(subInfo1); _client2.OpStream.OfType <MsgOp>().Subscribe(async msg => { await _client2.UnsubAsync(subInfo2); Interlocked.Increment(ref nr2ReceiveCount); ReleaseOne(); }); _client2.Sub(subInfo2); _client3.OpStream.OfType <MsgOp>().Subscribe(msg => { Interlocked.Increment(ref nr3ReceiveCount); ReleaseOne(); }); _client3.Sub(subInfo3); _client1.Pub(subject, "mess1"); WaitOne(); WaitOne(); WaitOne(); _client3.Unsub(subInfo3); await DelayAsync(); _client1.Pub(subject, "mess2"); await DelayAsync(); nr1ReceiveCount.Should().Be(1); nr2ReceiveCount.Should().Be(1); nr3ReceiveCount.Should().Be(1); }
/// <summary> /// Releases unmanaged and - optionally - managed resources. /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (disposing && !_disposed) { if (_client != null && _client.IsConnected) { if (_subscription != null) { _client.Unsub(_subscription); _subscription = null; } _client.Disconnect(); } _disposed = true; } }