Exemplo n.º 1
0
        public async Task Given_subscribed_When_disconnectiong_and_connecting_again_It_should_resubscribe_and_get_messages()
        {
            var subject = Context.GenerateSubject();

            _sync   = Sync.MaxOne();
            _client = await Context.ConnectClientAsync();

            _client.Sub(subject, stream => stream.Subscribe(NatsObserver.Delegating <MsgOp>(msg => _sync.Release(msg))));

            await Context.DelayAsync();

            await _client.PubAsync(subject, "Test1");

            _sync.WaitForAny();
            _client.Disconnect();

            await _client.ConnectAsync();

            await _client.PubAsync(subject, "Test2");

            _sync.WaitForAny();

            _sync.InterceptedCount.Should().Be(2);
        }
Exemplo n.º 2
0
        public static async Task <TimeSpan> MyNatsClientRequestAsync(int n, ReadOnlyMemory <byte> payload, bool useTls)
        {
            Console.WriteLine($"MyNatsClient-RequestAsync: n={n} s={payload.Length}");

            const string subject = "casea";

            using var cts  = new CancellationTokenSource();
            using var sync = new AutoResetEvent(false);
            var tcs    = new TaskCompletionSource <TimeSpan>();
            var cnInfo = new ConnectionInfo("127.0.0.1");

            if (useTls)
            {
                cnInfo.ServerCertificateValidation = (_, __, ___) => true;
            }

            var responderTask = Task.Factory.StartNew(async() =>
            {
                using var client = new NatsClient(cnInfo);
                await client.ConnectAsync().ConfigureAwait(false);

                client.Sub(subject, messages => messages.SubscribeSafe(msg => { client.Pub(msg.ReplyTo, msg.Payload); }));

                sync.Set();

                await tcs.Task.ConfigureAwait(false);
            }, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

            var requesterTask = Task.Factory.StartNew(async() =>
            {
                using var client = new NatsClient(cnInfo);

                await client.ConnectAsync().ConfigureAwait(false);

                if (!sync.WaitOne(1000))
                {
                    throw new Exception("Responder does not seem to be started.");
                }

                for (var i = 0; i < 10; i++)
                {
                    await client.RequestAsync(subject, payload, cts.Token).ConfigureAwait(false);
                }

                var sw = Stopwatch.StartNew();

                for (var i = 0; i < n; i++)
                {
                    await client.RequestAsync(subject, payload, cts.Token).ConfigureAwait(false);
                }

                sw.Stop();
                tcs.SetResult(sw.Elapsed);
            }, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

            var elapsed = await tcs.Task.ConfigureAwait(false);

            cts.Cancel();

            await Task.WhenAll(responderTask, requesterTask).ConfigureAwait(false);

            return(elapsed);
        }