Exemple #1
0
        public async Task EchoServerAsync(string address, int timeoutSeconds, string[] messages)
        {
            var maxClients   = 1;
            var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSeconds)).Token;
            var options      = new WebSocketListenerOptions
            {
                Logger = new TestLogger(this.logger)
                {
                    IsDebugEnabled = System.Diagnostics.Debugger.IsAttached
                }
            };

            options.Standards.RegisterRfc6455(rfc6455 =>
            {
                rfc6455.MessageExtensions.Add(new WebSocketDeflateExtension());
            });
            options.Transports.RegisterNamedPipes();

            var listenEndPoints = new[] { new Uri(address) };
            var server          = new EchoServer(listenEndPoints, options);

            this.logger.Debug("[TEST] Starting echo server.");
            await server.StartAsync().ConfigureAwait(false);

            var messageSender = new MessageSender(listenEndPoints[0], options);

            this.logger.Debug("[TEST] Connecting clients.");
            await messageSender.ConnectAsync(maxClients, cancellation).ConfigureAwait(false);

            this.logger.Debug($"[TEST] {messageSender.ConnectedClients} Client connected.");
            this.logger.Debug($"[TEST] Sending {maxClients * messages.Length} messages.");
            var sendTask = messageSender.SendMessagesAsync(messages, cancellation);

            while (sendTask.IsCompleted == false)
            {
                await Task.Delay(1000);

                this.logger.Debug($"[TEST] Server: r={server.ReceivedMessages}, s={server.SentMessages}, e={server.Errors}. " +
                                  $"Clients: r={messageSender.MessagesReceived}, s={messageSender.MessagesSent}, e={messageSender.Errors}.");
            }
            var processedMessages = await sendTask.ConfigureAwait(false);

            Assert.Equal(messages.Length, processedMessages);

            this.logger.Debug("[TEST] Stopping echo server.");
            await server.StopAsync().ConfigureAwait(false);

            this.logger.Debug("[TEST] Echo server stopped.");
            this.logger.Debug("[TEST] Disconnecting clients.");
            await messageSender.CloseAsync().ConfigureAwait(false);

            this.logger.Debug("[TEST] Disposing server.");
            server.Dispose();

            this.logger.Debug("[TEST] Waiting for send/receive completion.");
        }
Exemple #2
0
        //[InlineData("tcp://127.0.0.1:10102/", 30, 1000)]
        //[InlineData("tcp://127.0.0.1:10103/", 40, 10000)]
        public async Task EchoServerMassClientsAsync(string address, int timeoutSeconds, int maxClients)
        {
            var messages     = new string[] { new string('a', 126), new string('a', 127), new string('a', 128), new string('a', ushort.MaxValue - 1), new string('a', ushort.MaxValue), new string('a', ushort.MaxValue + 2) };
            var startTime    = DateTime.UtcNow;
            var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSeconds)).Token;
            var options      = new WebSocketListenerOptions
            {
                NegotiationQueueCapacity = maxClients,
                PingTimeout = TimeSpan.FromSeconds(30),
                Logger      = new TestLogger(this.logger)
                {
                    IsDebugEnabled = System.Diagnostics.Debugger.IsAttached
                }
            };

            options.Standards.RegisterRfc6455();
            options.Transports.ConfigureTcp(tcp =>
            {
                tcp.IsAsync        = true;
                tcp.NoDelay        = false;
                tcp.BacklogSize    = maxClients;
                tcp.SendTimeout    = TimeSpan.FromSeconds(15);
                tcp.ReceiveTimeout = TimeSpan.FromSeconds(15);
            });
            options.Transports.Add(new NamedPipeTransport());
            var listenEndPoints = new[] { new Uri(address) };
            var server          = new EchoServer(listenEndPoints, options);

            this.logger.Debug("[TEST] Starting echo server.");
            await server.StartAsync().ConfigureAwait(false);

            var messageSender = new MessageSender(listenEndPoints[0], options);

            this.logger.Debug("[TEST] Connecting clients.");
            await messageSender.ConnectAsync(maxClients, cancellation).ConfigureAwait(false);

            this.logger.Debug($"[TEST] {messageSender.ConnectedClients} Client connected.");
            this.logger.Debug($"[TEST] Sending {maxClients * messages.Length} messages.");
            var sendTask = messageSender.SendMessagesAsync(messages, cancellation);

            while (sendTask.IsCompleted == false && cancellation.IsCancellationRequested == false)
            {
                await Task.Delay(1000);

                this.logger.Debug($"[TEST] T:{timeoutSeconds - (DateTime.UtcNow - startTime).TotalSeconds:F0} " +
                                  $"Server: r={server.ReceivedMessages}, s={server.SentMessages}, e={server.Errors}. " +
                                  $"Clients: r={messageSender.MessagesReceived}, s={messageSender.MessagesSent}, e={messageSender.Errors}.");
            }

            var errorMessages = new SortedDictionary <string, int>(StringComparer.OrdinalIgnoreCase);

            server.PushErrorMessagesTo(errorMessages);
            messageSender.PushErrorMessagesTo(errorMessages);

            if (errorMessages.Count > 0)
            {
                this.logger.Debug("Errors:");
                foreach (var kv in errorMessages)
                {
                    this.logger.Debug($"[TEST] [x{kv.Value}] {kv.Key}");
                }
            }


            if (cancellation.IsCancellationRequested)
            {
                throw new TimeoutException();
            }

            await sendTask.ConfigureAwait(false);

            this.logger.Debug("[TEST] Stopping echo server.");
            await server.StopAsync().ConfigureAwait(false);

            this.logger.Debug("[TEST] Echo server stopped.");
            this.logger.Debug("[TEST] Disconnecting clients.");
            await messageSender.CloseAsync().ConfigureAwait(false);

            this.logger.Debug("[TEST] Disposing server.");
            server.Dispose();

            this.logger.Debug("[TEST] Waiting for send/receive completion.");
        }