davidfowl/MultiProtocolAspNetCore

Is it testable with WebApplicationFactory?

corentinaltepe opened this issue · 3 comments

Hi, thanks a lot for this!

I was wondering if it's possible testing the TCP server using WebApplicationFactory? I've configured my unit tests as follow, but it looks like the TCP server is never started during a unit test.

        private HttpClient _client;
        private WebApplicationFactory<Startup> _startupFactory;

        [SetUp]
        public void Setup()
        {
            _startupFactory = new WebApplicationFactory<Startup>()
                .WithWebHostBuilder(builder =>
                {
                    builder
                        .ConfigureAppConfiguration((context, conf) =>
                        {
                            // Custom config...
                        })
                        .UseKestrel()
                        .ConfigureKestrel((ctx, options) =>
                        {
                            // HTTP server
                            options.ListenLocalhost(5000);

                            // TCP server
                            options.ListenLocalhost(8007, builder =>
                            {
                                builder.UseConnectionHandler<MyConnectionHandler>();
                            });
                        });
                });
            _client = _startupFactory.CreateClient();
        }

        [TearDown]
        public void Teardown()
        {
            _client.Dispose();
            _startupFactory.Dispose();
        }

        [Test]
        public async Task ClientTcp()
        {
            using var client = new TcpClient();
            await client.ConnectAsync("localhost", 8007).ConfigureAwait(false);
            client.Close();
        }

Here's the error message:
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException : No connection could be made because the target machine actively refused it. [::ffff:127.0.0.1]:8007

@corentinaltepe did you ever solve this? Experiencing this currently.

Hi @juliankock.
No I didn't solve this. As far as I understand it, WebApplicationFactory is not the right abstraction for that.