예제 #1
0
파일: Program.cs 프로젝트: 80dB/AsyncNats
        static async Task Main(string[] args)
        {
            var options = new NatsDefaultOptions
            {
                Echo = true // Without echo this test does not work! On production you might want to keep it disabled
            };

            var connection = new NatsConnection(options);

            connection.ConnectionException   += (sender, exception) => Console.WriteLine($"ConnectionException : {exception}");
            connection.StatusChange          += (sender, status) => Console.WriteLine($"Connection status changed to {status}");
            connection.ConnectionInformation += (sender, information) => Console.WriteLine($"Connection information {JsonSerializer.Serialize(information)}");
            var cancellation = new CancellationTokenSource();

            var addListenerTask      = AddListenerAsync(connection, cancellation.Token);
            var multiplyListenerTask = MultiplyListenerAsync(connection, cancellation.Token);
            var senderTask           = SenderAsync(connection, cancellation.Token);
            await connection.ConnectAsync();

            Console.ReadKey();

            cancellation.Cancel();

            try
            {
                await addListenerTask;
            }
            catch (OperationCanceledException)
            {
            }

            try
            {
                await multiplyListenerTask;
            }
            catch (OperationCanceledException)
            {
            }

            try
            {
                await senderTask;
            }
            catch (OperationCanceledException)
            {
            }

            Console.ReadKey();

            await connection.DisposeAsync();

            Console.ReadKey();
        }
예제 #2
0
        static async Task Main(string[] args)
        {
            var options = new NatsDefaultOptions
            {
                Echo = true // Without echo this test does not work! On production you might want to keep it disabled
            };

            var connection = new NatsConnection(options);

            connection.ConnectionException   += (sender, exception) => Console.WriteLine($"ConnectionException : {exception}");
            connection.StatusChange          += (sender, status) => Console.WriteLine($"Connection status changed to {status}");
            connection.ConnectionInformation += (sender, information) => Console.WriteLine($"Connection information {JsonSerializer.Serialize(information)}");
            await connection.ConnectAsync();

            var cancellation = new CancellationTokenSource();
            var listener     = ListenerTask(connection, cancellation.Token);
            var sender       = SenderTask(connection, cancellation.Token);

            await sender;
            await listener;
        }
예제 #3
0
        static async Task Main(string[] args)
        {
            var options = new NatsDefaultOptions
            {
                Echo = true // Without echo this test does not work! On production you might want to keep it disabled
            };

            options.Serializer = new NatsAsciiSerializer();
            var connection = new NatsConnection(options);

            connection.ConnectionException   += (sender, exception) => Console.WriteLine($"ConnectionException : {exception}");
            connection.StatusChange          += (sender, status) => Console.WriteLine($"Connection status changed to {status}");
            connection.ConnectionInformation += (sender, information) => Console.WriteLine($"Connection information {JsonSerializer.Serialize(information)}");

            var cancellation = new CancellationTokenSource();

            await connection.ConnectAsync();

            var count = 0;

            while (connection.Status != NatsStatus.Connected)
            {
                await Task.Delay(50, cancellation.Token);

                count++;
                if (count > 100)
                {
                    Console.WriteLine("Could not connect to nats server");
                    await connection.DisposeAsync();

                    return;
                }
            }


            var readerTypedTask = Task.Run(() => ReaderText(connection, cancellation.Token));
            var writerTask      = Task.Run(() => WriterText(connection, cancellation.Token));

            Console.ReadKey();

            cancellation.Cancel();
            try
            {
                await readerTypedTask;
            }
            catch (OperationCanceledException)
            {
            }

            try
            {
                await writerTask;
            }
            catch (OperationCanceledException)
            {
            }

            Console.ReadKey();

            await connection.DisposeAsync();

            Console.ReadKey();
        }
예제 #4
0
파일: Program.cs 프로젝트: 80dB/AsyncNats
        static async Task Main(string[] args)
        {
            var options = new NatsDefaultOptions
            {
                Serializer     = new NatsMessagePackSerializer(),
                Echo           = true, // Without echo this test does not work! On production you might want to keep it disabled
                RequestTimeout = TimeSpan.FromSeconds(2),
            };

            var connection = new NatsConnection(options);

            connection.ConnectionException   += (sender, exception) => Console.WriteLine($"ConnectionException : {exception.Message}");
            connection.StatusChange          += (sender, status) => Console.WriteLine($"Connection status changed to {status}");
            connection.ConnectionInformation += (sender, information) => Console.WriteLine($"Connection information {JsonSerializer.Serialize(information)}");
            var cancellation = new CancellationTokenSource();

            var listenerTask = connection.StartContractServer <IContract>(new Server(), cancellation.Token, "IContract");
            await connection.ConnectAsync();

            var count = 0;

            while (connection.Status != NatsStatus.Connected)
            {
                await Task.Delay(50, cancellation.Token);

                count++;
                if (count <= 100)
                {
                    continue;
                }

                Console.WriteLine("Could not connect to nats server");
                await connection.DisposeAsync();

                return;
            }

            var client = connection.GenerateContractClient <IContract>("IContract");
            var result = await client.MultiplyAsync(10, 10);

            Console.WriteLine("Multiply Result: {0}", result);

            result = client.Add(10, 10);
            Console.WriteLine("Add Result: {0}", result);

            result = await client.RandomAsync();

            Console.WriteLine("RandomAsync Result: {0}", result);

            result = client.Random();
            Console.WriteLine("Random Result: {0}", result);

            await client.SayAsync("Hello Async World");

            client.Say("Hello Sync World");

            await client.FireAndForget(1, 2, 3);

            Console.WriteLine("After FireAndForget - Note that ThrowException will not execute until after FireAndForget finishes (server is single threaded)");

            Console.WriteLine("Request timeout test");
            try
            {
                await client.Timeout();

                Console.WriteLine("ERROR: The Timeout call should have timed out!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Expected exception: {0}", ex.InnerException?.Message ?? ex.Message);
            }

            try
            {
                client.ThrowException();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Expected exception: {0}", ex.Message);
            }

            try
            {
                await client.ThrowExceptionOnMethodWithReturn();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Expected exception: {0}", ex.Message);
            }

            Console.ReadKey();

            cancellation.Cancel();

            try
            {
                await listenerTask;
            }
            catch (OperationCanceledException)
            {
            }

            await connection.DisposeAsync();
        }
예제 #5
0
파일: Program.cs 프로젝트: 80dB/AsyncNats
        static async Task Main(string[] args)
        {
            var options = new NatsDefaultOptions
            {
                SenderQueueLength   = 5000,
                ReceiverQueueLength = 500,
            };

            await using var readerConnection        = new NatsConnection(options);
            readerConnection.ConnectionException   += (sender, exception) => Console.WriteLine($"ConnectionException : {exception}");
            readerConnection.StatusChange          += (sender, status) => Console.WriteLine($"Connection status changed to {status}");
            readerConnection.ConnectionInformation += (sender, information) => Console.WriteLine($"Connection information {JsonSerializer.Serialize(information)}");


            var cancellation = new CancellationTokenSource();
            await readerConnection.ConnectAsync();

            var count = 0;

            while (readerConnection.Status != NatsStatus.Connected)
            {
                await Task.Delay(50, cancellation.Token);

                count++;
                if (count > 100)
                {
                    Console.WriteLine("Could not connect to nats server");
                    await readerConnection.DisposeAsync();

                    return;
                }
            }


            var tasks = new[]
            {
                Task.Run(() => Reader(readerConnection, cancellation.Token), cancellation.Token)
            }.Concat(Enumerable.Range(0, 5).Select(_ => Task.Run(() => Writer(options, cancellation.Token), cancellation.Token))).ToArray();


            var frequency = (double)Stopwatch.Frequency;

            while (!Console.KeyAvailable)
            {
                Console.Write($"\rMessages processed {_timings.Count} ({_timings.Count / ((Stopwatch.GetTimestamp() - _started) / frequency):N2} messages/sec)  {readerConnection.ReceiverQueueSize}           ");
                await Task.Delay(1000, cancellation.Token);
            }

            cancellation.Cancel();

            try
            {
                Task.WaitAll(tasks);
            }
            catch
            { }


            Console.WriteLine($"Measured {_timings.Count} messages in {(_ended - _started) / frequency:N2} seconds ({_timings.Count / ((_ended - _started) / frequency):N2} messages/sec)");

            _timings.Sort();

            var ms = Stopwatch.Frequency / 1000d;

            Console.WriteLine("Latencies:");
            Console.WriteLine($"Slowest: {_timings[^1] / ms:N2}ms");