hibri / HttpMock

A library for creating Http servers on the fly in tests and stubbing responses
MIT License
128 stars 44 forks source link

Creating/Disposing the same server #45

Closed neoistheone123 closed 8 years ago

neoistheone123 commented 8 years ago

I have a test suite which creates/disposes a server multiple times. Server.IsAvailable() returns true, but the HTTP request hangs until timeout. When I ran the tests individually, they pass.

hibri commented 8 years ago

Hi @ykeysergit, Can you reproduce it ? and send me the test suite ?

Thanks.

neoistheone123 commented 8 years ago

Yes. Consistently. The test suite is like...

public void Test1(){ var server = fixture.createServer() try{ } finally{ server.Dispose(); } }

public void Test2(){ var server = fixture.createServer() try{

} finally{ server.Dispose(); } }

Seems like there's a thread in the framework which is still running.

hibri commented 8 years ago

What is behind fixture.createServer ?

A correct usage example is here https://github.com/hibri/HttpMock/blob/master/src/HttpMock.Integration.Tests/HttpFactoryTests.cs and here

https://github.com/hibri/HttpMock/blob/master/src/HttpMock.Integration.Tests/MultipleTestsUsingTheSameStubServerWithDifferentHttpMethods.cs#L8

I suggest sharing the mock server between tests, to avoid the cost of destroying and recreating the port that the server listens on. This can be an expensive operation. Dispose might not guarantee the port is released for use in time for the next test.

You can use .WithNewContext to clear the stubs that were setup in a previous tests. Create the server in a TestFixtureSetup (before all tests), and call .WithNewContext in a SetUp method before each test.

Hope this helps :)

neoistheone123 commented 8 years ago

I did this so I can use AssertWasCall with different expectations. Otherwise, the success of one test case may propogate to the next. I'll try WithNewContext.

createServer: IHttpServer server = HttpMockRepository.At(hostUrl); stubX(server); return server;

FYI, I'm using XUnit.

neoistheone123 commented 8 years ago

I think it may be due to something due to how I created the server. Stand by.

neoistheone123 commented 8 years ago

Ok. The root cause is on my end. Thanks for your input!