예제 #1
0
        public async Task Connect_Success(bool clientFirst)
        {
            await using ConnectionFactory factory = new MemoryConnectionFactory();
            await using ConnectionListener listener = await factory.ListenAsync();

            using var semaphore = new SemaphoreSlim(0);

            await RunClientServer(async () =>
            {
                if (!clientFirst)
                {
                    bool success = await semaphore.WaitAsync(10_000);
                    Assert.True(success);
                }

                ValueTask<Connection> task = factory.ConnectAsync(listener.EndPoint!);
                if (clientFirst) semaphore.Release();

                await using Connection connection = await task;
            },
            async () =>
            {
                if (clientFirst)
                {
                    bool success = await semaphore.WaitAsync(10_000);
                    Assert.True(success);
                }

                ValueTask<Connection?> task = listener.AcceptConnectionAsync();
                if (!clientFirst) semaphore.Release();

                await using Connection? connection = await task;
                Assert.NotNull(connection);
            });
        }
예제 #2
0
        public async Task Listener_DisposeCancelsAccept_Success()
        {
            await using ConnectionFactory factory = new MemoryConnectionFactory();
            await using ConnectionListener listener = await factory.ListenAsync();

            ValueTask<Connection?> acceptTask = listener.AcceptConnectionAsync();

            await listener.DisposeAsync();

            Connection? connection = await acceptTask;
            Assert.Null(connection);
        }
예제 #3
0
        public async Task Listener_DisposeCancelsConnect_Success()
        {
            await using ConnectionFactory factory   = new MemoryConnectionFactory();
            await using ConnectionListener listener = await factory.ListenAsync();

            ValueTask <Connection> connectTask = factory.ConnectAsync(listener.EndPoint !);

            await listener.DisposeAsync();

            SocketException ex = await Assert.ThrowsAsync <SocketException>(async() =>
            {
                await using Connection connection = await connectTask;
            }).ConfigureAwait(false);

            Assert.Equal(SocketError.ConnectionRefused, ex.SocketErrorCode);
        }
예제 #4
0
        internal virtual ConnectionFactory CreateConnectionFactory()
        {
            ConnectionFactory factory = new MemoryConnectionFactory();

            if (UseSsl)
            {
                factory = new SslConnectionFactory(factory);
            }
            if (Trickle)
            {
                factory = new TricklingConnectionFactory(factory)
                {
                    ForceAsync = TrickleForceAsync
                }
            }
            ;
            return(factory);
        }
예제 #5
0
        static async Task Main(string[] args)
        {
            Environment.SetEnvironmentVariable("DOTNET_SYSTEM_THREADING_POOLASYNCVALUETASKS", "1");

            await using ConnectionFactory connectionFactory = new MemoryConnectionFactory();

            await using ConnectionListener listener = await connectionFactory.ListenAsync();

            await using SimpleHttp1Server server = new(listener, triggerBytes, responseBytes);

            await using Connection connection = await connectionFactory.ConnectAsync(listener.EndPoint !);

            await using HttpConnection httpConnection = new Http1Connection(connection, HttpPrimitiveVersion.Version11);

            if (!Debugger.IsAttached)
            {
                Console.WriteLine("Press any key to continue, once profiler is attached...");
                Console.ReadKey();
            }

            for (int i = 0; i < 1000000; ++i)
            {
                await using ValueHttpRequest request = (await httpConnection.CreateNewRequestAsync(HttpPrimitiveVersion.Version11, HttpVersionPolicy.RequestVersionExact))
                                                       ?? throw new Exception("HttpConnection failed to return a request");

                request.ConfigureRequest(contentLength: 0, hasTrailingHeaders: false);
                request.WriteRequest(HttpRequest.GetMethod, authority, pathAndQuery);

                request.WriteHeader(preparedRequestHeaders);

                foreach ((byte[] name, byte[] value) in dynamicRequestHeaders)
                {
                    request.WriteHeader(name, value);
                }

                await request.CompleteRequestAsync();

                while (await request.ReadAsync() != HttpReadType.EndOfStream)
                {
                    // do nothing, just draining.
                }
            }
        }
 public MemoryTransport()
 {
     ConnectionListenerFactory = new MemoryConnectionListenerFactory(this);
     ConnectionFactory         = new MemoryConnectionFactory(this);
 }