Ejemplo n.º 1
0
        public async Task ConnectionCanReadAndWrite(ListenOptions listenOptions)
        {
            var serviceContext = new TestServiceContext();

            listenOptions.UseHttpServer(listenOptions.ConnectionAdapters, serviceContext, new DummyApplication(TestApp.EchoApp), HttpProtocols.Http1);

            var transportContext = new TestLibuvTransportContext
            {
                ConnectionDispatcher = new ConnectionDispatcher(serviceContext, listenOptions.Build())
            };

            var transport = new LibuvTransport(transportContext, listenOptions);

            await transport.BindAsync();

            using (var socket = TestConnection.CreateConnectedLoopbackSocket(listenOptions.IPEndPoint.Port))
            {
                var data = "Hello World";
                socket.Send(Encoding.ASCII.GetBytes($"POST / HTTP/1.0\r\nContent-Length: 11\r\n\r\n{data}"));
                var buffer = new byte[data.Length];
                var read   = 0;
                while (read < data.Length)
                {
                    read += socket.Receive(buffer, read, buffer.Length - read, SocketFlags.None);
                }
            }

            Assert.True(await serviceContext.ConnectionManager.CloseAllConnectionsAsync(new CancellationTokenSource(TestConstants.DefaultTimeout).Token));
            await transport.UnbindAsync();

            await transport.StopAsync();
        }
Ejemplo n.º 2
0
        public async Task OneToTenThreads(int threadCount)
        {
            var listenOptions   = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0));
            var serviceContext  = new TestServiceContext();
            var testApplication = new DummyApplication(context =>
            {
                return(context.Response.WriteAsync("Hello World"));
            });

            listenOptions.UseHttpServer(listenOptions.ConnectionAdapters, serviceContext, testApplication, HttpProtocols.Http1);

            var transportContext = new TestLibuvTransportContext
            {
                Options = new LibuvTransportOptions {
                    ThreadCount = threadCount
                }
            };

            await using var transport = new LibuvConnectionListener(transportContext, listenOptions.EndPoint);
            await transport.BindAsync();

            listenOptions.EndPoint = transport.EndPoint;

            var dispatcher = new ConnectionDispatcher(serviceContext, listenOptions.Build());
            var acceptTask = dispatcher.StartAcceptingConnections(transport);

            using (var client = new HttpClient())
            {
                // Send 20 requests just to make sure we don't get any failures
                var requestTasks = new List <Task <string> >();
                for (int i = 0; i < 20; i++)
                {
                    var requestTask = client.GetStringAsync($"http://127.0.0.1:{listenOptions.IPEndPoint.Port}/");
                    requestTasks.Add(requestTask);
                }

                foreach (var result in await Task.WhenAll(requestTasks))
                {
                    Assert.Equal("Hello World", result);
                }
            }

            await transport.UnbindAsync();

            await acceptTask;

            if (!await serviceContext.ConnectionManager.CloseAllConnectionsAsync(default))