public async Task Given_subscribed_When_unsubscribing_sync_It_should_not_get_any_further_messages() { var subject = GenerateSubject(); var interceptCount = 0; var subscription = _client.Sub(subject, stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg => { Interlocked.Increment(ref interceptCount); ReleaseOne(); }))); await _client.PubAsync(subject, "Test1"); WaitOne(); await _client.PubAsync(subject, "Test2"); WaitOne(); await _client.UnsubAsync(subscription.SubscriptionInfo); await _client.PubAsync(subject, "Test3"); WaitOne(); interceptCount.Should().Be(2); }
public async Task Given_subscribed_When_unsubscribing_async_It_should_not_get_any_further_messages() { var subject = Context.GenerateSubject(); _sync = Sync.MaxOne(); _client = await Context.ConnectClientAsync(); var s = _client.Sub(subject, stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg => _sync.Release(msg)))); await Context.DelayAsync(); await _client.PubAsync(subject, "Test1"); _sync.WaitForAny(); await _client.PubAsync(subject, "Test2"); _sync.WaitForAny(); await _client.UnsubAsync(s.SubscriptionInfo); await _client.PubAsync(subject, "Test3"); await Context.DelayAsync(); _sync.InterceptedCount.Should().Be(2); }
/// <summary> /// Starts the subscription /// </summary> /// <summary> /// Starts the subscription /// </summary> public async Task Start() { if (_disposed) { throw new ObjectDisposedException($"{typeof(JsonRpcSubscription).FullName}@{this.GetHashCode()}"); } if (!_client.IsConnected) { _client.Connect(); } if (_subscription != null) { await _client.UnsubAsync(_subscription); _subscription = null; } _subscription = await _client.SubAsync(_sInfo, stream => stream.Subscribe(msg => { try { byte[] resp = OnMessage(msg.Payload); _client.Pub(msg.ReplyTo, resp); } catch (Exception ex) { _logger?.LogError(ex, "Error publishing NATS message"); } }, error => { _logger?.LogError(error, "Fatal error in NATS subscription handler"); })); }
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); }