Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock Server Isn't Closing Before The Next One #138

Closed
ebebbington opened this issue Feb 20, 2020 · 8 comments
Closed

Mock Server Isn't Closing Before The Next One #138

ebebbington opened this issue Feb 20, 2020 · 8 comments
Labels
Bug Something isn't working as expected HELP WANTED This item is seeking help from anyone

Comments

@ebebbington
Copy link
Member

Describe the bug
A test doesn't seem to close the mock server, so it fails when running the next test as the address is in use, see below for the error:

S D:\Development\environments\deno-drash> deno test --allow-env --allow-net .\tests\unit\http\middleware_test.ts
Compile file:///D:/Development/environments/deno-drash/.deno.test.ts
running 12 tests
middleware.ts
OK     - (0.00ms)

Deno server started at localhost:1557.

OK     server/resource: missing CSRF token (24.00ms)

Deno server started at localhost:1557.

error: Uncaught AddrInUse: Only one usage of each socket address (protocol/network address/port) is normally permitted. (os error 10048)
► $deno$/dispatch_json.ts:40:11
    at DenoError ($deno$/errors.ts:20:5)
    at unwrapResponse ($deno$/dispatch_json.ts:40:11)
    at sendSync ($deno$/dispatch_json.ts:67:10)
    at listen ($deno$/net.ts:170:15)
    at serve (server.ts:492:20)
    at run (server.ts:418:24)
    at middleware_test.ts:49:10
    at runTests ($deno$/testing.ts:157:22)

It passes the first test, and fails the second, which helps confirm the server hasn't actually closed yet

To Reproduce
Steps to reproduce the behavior:

  1. Cd into the root directory
  2. Run the following command:
    deno test --allow-env --allow-net .\tests\unit\http\middleware_test.ts
    3.. See error

Expected behavior
Tests should be able to run after one another and the mock server should be closed before running the next text (which is also creating a mock server)

Suggested Solution(s)
Couldn't say, maybe there's a race condition or server.deno_server.close(); isn't working as expected

Desktop (please complete the following information):

  • OS: [Windows 10]
  • Deno:
deno 0.33.0
v8 8.1.108
typescript 3.7.2

Additional context
Disocvered this problem whilst developing the solution to issue #115 types for all members when I tried testing. I tested on a branch without any of my changes and the error still showed

@crookse
Copy link
Member

crookse commented Feb 20, 2020

Possibly a race condition. I just ran the tests and didn't see the error. However, I have seen this error before. I "fixed" it by changing the hostnames and ports ;P. Might be worth looking into running a single server instance for a test suite, but I like the fact that different server instances with different configs can be created for each test in a test suite.

@crookse crookse added the Bug Something isn't working as expected label Feb 20, 2020
@ebebbington
Copy link
Member Author

The worst kind of bugs... is there a method on test to run only a single test? Like in Chai, you can do describe.only('this is the only block that will be tested', function () { ... })

@ebebbington
Copy link
Member Author

FYI should note the race condition seems to be between the tests 'server/resource: wrong CSRF token' and 'server/resource: user is not an admin', as seen below:

PS D:\Development\environments\deno-drash> deno test --allow-env --allow-net .\tests\unit\http\middleware_test.ts
Compile file:///D:/Development/environments/deno-drash/tests/unit/http/middleware_test.ts
running 12 tests
middleware.ts
OK     - (0.00ms)

Deno server started at 0.0.0.0:1557.

OK     server/resource: missing CSRF token (344.00ms)

Deno server started at localhost:1557.

OK     server/resource: wrong CSRF token (20.00ms)

Deno server started at localhost:1557.

error: Uncaught AddrInUse: Only one usage of each socket address (protocol/network address/port) is normally permitted. (os error 10048)
► $deno$/dispatch_json.ts:40:11
    at DenoError ($deno$/errors.ts:20:5)
    at unwrapResponse ($deno$/dispatch_json.ts:40:11)
    at sendSync ($deno$/dispatch_json.ts:67:10)
    at listen ($deno$/net.ts:170:15)
    at serve (server.ts:492:20)
    at run (server.ts:418:24)
    at middleware_test.ts:80:10
    at runTests ($deno$/testing.ts:157:22)

@player-4
Copy link
Contributor

we could look into using await. maybe the testing module runs tests in parallel. idk. i'll investigate.

@crookse
Copy link
Member

crookse commented Feb 21, 2020

This is all kinds of messed up now with deno test in deno v0.34.0. I just tried running the tests using deno test it in your branch, Jack. We need to fix the tests before we release your #141 PR. I can work with you on that, but not until this evening.

@crookse crookse added the HELP WANTED This item is seeking help from anyone label Feb 21, 2020
@ebebbington
Copy link
Member Author

Possibly a race condition. I just ran the tests and didn't see the error. However, I have seen this error before. I "fixed" it by changing the hostnames and ports ;P. Might be worth looking into running a single server instance for a test suite, but I like the fact that different server instances with different configs can be created for each test in a test suite.

Only way i've managed to fix tests is using this approach, by having each server running on different ports. I've seen Deno so the same in some examples but I haven't found a current file that does this (yet). Also think the problem with having a single test server instance is responses - I know in server_test.ts, the tests fail if the GetUrl and GetBody resources are used at the same time, as a few tests expect different results when getting the same path e.g. one test uses GetUrlQueryParam and another uses GetHeaderParam (i can't remember if it was these exact ones)

@ebebbington
Copy link
Member Author

@crookse Isn't this 'technicallly' solved now? In the sense Drash's test don't have this error anymore due to the restructure of the tests? Just wondering if it could be closed now

@crookse
Copy link
Member

crookse commented Apr 16, 2020

@ebebbington , that seems to be the case. Closing this. Thanks!

@crookse crookse closed this as completed Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working as expected HELP WANTED This item is seeking help from anyone
Development

No branches or pull requests

3 participants