davidfowl / MultiProtocolAspNetCore

253 stars 32 forks source link

Is it testable with WebApplicationFactory? #7

Open corentinaltepe opened 4 years ago

corentinaltepe commented 4 years ago

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();
        }
corentinaltepe commented 4 years ago

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

juliankock commented 1 year ago

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

corentinaltepe commented 1 year ago

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