public static async Task <EchoClient_Rfc_862.EchoResult> RunTcpAsync()
        {
            // Hint: the using here means that the server is automatically closed
            // after the code is done. If you don't do that, the server stays open.
            using (var server = new EchoServer_Rfc_862())
            {
                await server.StartAsync();

                var client = new EchoClient_Rfc_862();
                // You only get the echo'd result from TCP with the final CloseAsync()
                // But for UDP  you get the returned packet.
                var partialResult = await client.WriteAsync(
                    new Windows.Networking.HostName("localhost"),
                    server.Options.Service,
                    EchoClient_Rfc_862.ProtocolType.Tcp,
                    "Hello, echo server!");

                // TODO: remove magic value?
                // Wait for the echo to have worked. The server is right here, so the
                // echo should be almost instantaneous.
                await Task.Delay(100);

                var completeResult = await client.CloseAsync(); // Close is essential for TCP.

                return(completeResult);
            }
        }
示例#2
0
        public async Task Test_Bad_Service(EchoClient_Rfc_862.ProtocolType protocol)
        {
            var host          = new HostName("localhost");
            var serverOptions = new EchoServer_Rfc_862.ServerOptions()
            {
                LoggingLevel = EchoServer_Rfc_862.ServerOptions.Verbosity.None
            };
            var clientOptions = new EchoClient_Rfc_862.ClientOptions()
            {
                LoggingLevel = EchoClient_Rfc_862.ClientOptions.Verbosity.None
            };

            using (var server = new EchoServer_Rfc_862(serverOptions))
            {
                bool serverOk = await server.StartAsync();

                if (!serverOk)
                {
                    Infrastructure.Error($"unable to start server");
                    return;
                }

                var client = new EchoClient_Rfc_862(clientOptions);
                var result = await client.WriteAsync(host, "79", protocol, "WontBeEchoed");

                Infrastructure.IfTrueError(result.Succeeded != EchoClient_Rfc_862.EchoResult.State.Failed, "result.Succeeded ");
                Infrastructure.IfTrueError(!String.IsNullOrEmpty(result.Value), "result.Value is not null");
                // ConnectionRefused is for TCP
                // ConnectionResetByPeer is for UDP
                Infrastructure.IfTrueError(result.Error != SocketErrorStatus.ConnectionRefused && result.Error != SocketErrorStatus.ConnectionResetByPeer, $"result.Error is wrong ({result.Error})");
            }
        }
        private void OnStartServers(object sender, RoutedEventArgs e)
        {
            var options = new EchoServer_Rfc_862.ServerOptions()
            {
                Service = uiService.Text,
            };

            Server           = new EchoServer_Rfc_862(options);
            Server.LogEvent += Server_LogEvent;
            ServerTask       = Server.StartAsync();
        }
示例#4
0
        public async Task Test_Echo_Good_Path_Udp()
        {
            using (var server = new EchoServer_Rfc_862())
            {
                var client = new EchoClient_Rfc_862();
                await server.StartAsync();

                var result1 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Udp, "ABC");

                Infrastructure.IfTrueError(result1.Succeeded != EchoClient_Rfc_862.EchoResult.State.Succeeded, "Close TCP status should be succeeded");
                Infrastructure.IfTrueError(result1.Value != "ABC", $"Should have gotten back abcDEF123456, not {result1.Value}");

                var result2 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Udp, "123");

                Infrastructure.IfTrueError(result2.Succeeded != EchoClient_Rfc_862.EchoResult.State.Succeeded, "Close TCP status should be succeeded");
                Infrastructure.IfTrueError(result2.Value != "123", $"Should have gotten back abcDEF123456, not {result2.Value}");
            }
        }
        public static async Task <EchoClient_Rfc_862.EchoResult> RunUdpAsync()
        {
            // Hint: the using here means that the server is automatically closed
            // after the code is done. If you don't do that, the server stays open.
            using (var server = new EchoServer_Rfc_862())
            {
                await server.StartAsync();

                var client = new EchoClient_Rfc_862();
                var result = await client.WriteAsync(
                    new Windows.Networking.HostName("localhost"),
                    server.Options.Service,
                    EchoClient_Rfc_862.ProtocolType.Udp,
                    "Hello, echo server!");

                await client.CloseAsync(); // Close is really needed for TCP.

                return(result);
            }
        }
示例#6
0
        public async Task Test_Echo_Good_Path_Tcp()
        {
            using (var server = new EchoServer_Rfc_862())
            {
                var client = new EchoClient_Rfc_862();
                await server.StartAsync();

                var result1 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Tcp, "ABCdef");

                Infrastructure.IfTrueError(result1.Succeeded != EchoClient_Rfc_862.EchoResult.State.InProgress, "TCP status should be in progress");

                var result2 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Tcp, "123456");

                Infrastructure.IfTrueError(result2.Succeeded != EchoClient_Rfc_862.EchoResult.State.InProgress, "TCP status should be in progress");

                await Task.Delay(100); //TODO: artificial delay to get the results.

                var result3 = await client.CloseAsync();

                Infrastructure.IfTrueError(result3.Succeeded != EchoClient_Rfc_862.EchoResult.State.Succeeded, "Close TCP status should be succeeded");
                Infrastructure.IfTrueError(result3.Value != "ABCdef123456", $"Should have gotten back abcDEF123456, not {result3.Value}");
            }
        }
示例#7
0
        /// <summary>
        /// System test, not a unit tests. Creates a server, pumps a bunch of requests at it,
        /// and verifies that everything worked.
        /// </summary>
        /// <returns></returns>
        public async Task Test_Stress(EchoClient_Rfc_862.ProtocolType protocol)
        {
            string pname = protocol.ToString();

            const int NLOOP  = 4;
            const int NBUNCH = 200;

            const double ALLOWED_TIME       = 20.0;                       // Normally we expect everything to be fast. No so much for a stress test!
            const int    ALLOWED_CONN_RESET = (NLOOP * NBUNCH) * 5 / 100; // Allow 5% conn reset

            int NBytesWrite   = 0;
            int NConnReset    = 0;
            var host          = new HostName("localhost");
            var serverOptions = new EchoServer_Rfc_862.ServerOptions()
            {
                LoggingLevel = EchoServer_Rfc_862.ServerOptions.Verbosity.None,
            };
            var clientOptions = new EchoClient_Rfc_862.ClientOptions()
            {
                LoggingLevel = EchoClient_Rfc_862.ClientOptions.Verbosity.None,
            };

            using (var server = new EchoServer_Rfc_862(serverOptions))
            {
                bool serverOk = await server.StartAsync();

                if (!serverOk)
                {
                    Infrastructure.Error($"Client: Stress: {pname} unable to start server");
                    return;
                }

                var start = DateTimeOffset.UtcNow;
                for (int bigLoop = 0; bigLoop < NLOOP; bigLoop++)
                {
                    if (bigLoop % 100 == 0 && bigLoop > 0)
                    {
                        Infrastructure.Log($"Client: Stress: {pname} starting loop {bigLoop} of {NLOOP}");
                    }
                    var allClients = new EchoClient_Rfc_862[NBUNCH];

                    var allWriteTasks = new Task <EchoClient_Rfc_862.EchoResult> [NBUNCH];
                    for (int i = 0; i < NBUNCH; i++)
                    {
                        var send = $"ABC-Loop {bigLoop} Item {i}";
                        NBytesWrite     += send.Length;
                        allClients[i]    = new EchoClient_Rfc_862(clientOptions);
                        allWriteTasks[i] = allClients[i].WriteAsync(host, serverOptions.Service, protocol, send);
                    }
                    await Task.WhenAll(allWriteTasks);

                    // TCP has to wait for the close to happen.
                    Task <EchoClient_Rfc_862.EchoResult>[] allCloseTasks = null;
                    if (protocol == EchoClient_Rfc_862.ProtocolType.Tcp)
                    {
                        allCloseTasks = new Task <EchoClient_Rfc_862.EchoResult> [NBUNCH];
                        for (int i = 0; i < NBUNCH; i++)
                        {
                            allCloseTasks[i] = allClients[i].CloseAsync();
                        }
                        await Task.WhenAll(allCloseTasks);
                    }

                    for (int i = 0; i < NBUNCH; i++)
                    {
                        var result = protocol == EchoClient_Rfc_862.ProtocolType.Tcp
                            ? allCloseTasks[i].Result
                            : allWriteTasks[i].Result;

                        var didSucceed = result.Succeeded == EchoClient_Rfc_862.EchoResult.State.Succeeded;
                        if (!didSucceed && result.Error == SocketErrorStatus.ConnectionResetByPeer)
                        {
                            // Connection reset by peer is an error only if we get a bunch of them.
                            NConnReset++;
                            Infrastructure.IfTrueError(NConnReset > ALLOWED_CONN_RESET, $"Too many connection resets {NConnReset}");
                        }
                        else if (!Infrastructure.IfTrueError(!didSucceed, $"!result.Succeeded with error {result.Error.ToString()} for {pname}"))
                        {
                            Infrastructure.IfTrueError(result.Value == null, "result.Value is null");
                            if (result.Value != null && (result.Value.Length < 10 || result.Value.Length > 60))
                            {
                                ;
                            }
                            Infrastructure.IfTrueError(result.Value != null && (result.Value.Length < 10 || result.Value.Length > 60), $"result.Value is weird size ({result.Value.Length}) for {pname}");
                        }
                        Infrastructure.IfTrueError(result.TimeInSeconds < -1, $"result.TimeInSeconds too small {result.TimeInSeconds} for {pname}");
                        Infrastructure.IfTrueError(result.TimeInSeconds > ALLOWED_TIME, $"result.TimeInSeconds too large {result.TimeInSeconds} for {pname}");
                    }

                    await Task.Delay(300); //NOTE: pause just to make the test runs easier to figure out
                }
                // timing data
                var    delta       = DateTimeOffset.UtcNow.Subtract(start).TotalSeconds;
                double timePerCall = delta / (double)(NLOOP * NBUNCH);
                Infrastructure.Log($"Stress test average time: {NLOOP} {NBUNCH} {timePerCall} for {pname}");


                // How's the server doing?
                const int ExpectedCount = NLOOP * NBUNCH;
                Infrastructure.IfTrueError(server.Stats.NConnections != ExpectedCount, $"Server got {server.Stats.NConnections} connections but expected {ExpectedCount} for {pname}");
                Infrastructure.IfTrueError(server.Stats.NResponses != ExpectedCount, $"Server sent {server.Stats.NResponses} responses but expected {ExpectedCount} for {pname}");

                // Why is the expected not equal to the NBytesSent? Because TCP has a weird timing thing:
                // the server has to close down reading from the client relatively quickly and can't waste
                // time for the client to send bytes that probably won't come.
                var expectedMinBytes = protocol == EchoClient_Rfc_862.ProtocolType.Udp ? NBytesWrite : NBytesWrite / 4;
                Infrastructure.IfTrueError(server.Stats.NBytesRead > NBytesWrite, $"Server got {server.Stats.NBytesRead} bytes but expected {NBytesWrite} for {pname}");
                Infrastructure.IfTrueError(server.Stats.NBytesRead < expectedMinBytes, $"Server got {server.Stats.NBytesRead} bytes but expected {NBytesWrite} with a minimum value of {expectedMinBytes} for {pname}");
                Infrastructure.IfTrueError(server.Stats.NExceptions != 0, $"Server got {server.Stats.NExceptions} exceptions but expected {0} for {pname}");
            }
        }