CImrie / mongomem

In-memory MongoDB Server. Ideal for testing.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

get-port not working

OmgImAlexis opened this issue · comments

When using this if I have mongodb running on port 27017 it still tries binding to that port instead of trying another open port.

For example when using ava with MongoDBServer.debug = true;.

➜  api.wvvw.me git:(master) ✗ yarn ava -- test/user.spec.js  --verbose
yarn ava v0.24.6
$ "/Users/xo/code/api.wvvw.me/node_modules/.bin/ava" test/user.spec.js --verbose

Mon, 26 Jun 2017 01:23:39 GMT mongodb-prebuilt-MongodHelper mongod stdout: 2017-06-26T10:53:39.106+0930 I CONTROL  [initandlisten] MongoDB starting : pid=72725 port=27017 dbpath=/var/folders/0s/cb83l2551dzdr09pym0ggckm0000gn/T/mongomem-72722HmQIjtY53Xsf 64-bit host=Bella.tardis
2017-06-26T10:53:39.107+0930 I CONTROL  [initandlisten] db version v3.5.5-13-g00ee4f5

Mon, 26 Jun 2017 01:23:39 GMT mongodb-prebuilt-MongodHelper mongod stdout: 2017-06-26T10:53:39.107+0930 I CONTROL  [initandlisten] git version: 00ee4f5156348477b9dd3f71b747104794f766c0
2017-06-26T10:53:39.107+0930 I CONTROL  [initandlisten] allocator: system
2017-06-26T10:53:39.107+0930 I CONTROL  [initandlisten] modules: none
2017-06-26T10:53:39.107+0930 I CONTROL  [initandlisten] build environment:
2017-06-26T10:53:39.107+0930 I CONTROL  [initandlisten]     distarch: x86_64
2017-06-26T10:53:39.107+0930 I CONTROL  [initandlisten]     target_arch: x86_64
2017-06-26T10:53:39.107+0930 I CONTROL  [initandlisten] options: { net: { port: 27017 }, security: { authorization: "disabled" }, storage: { dbPath: "/var/folders/0s/cb83l2551dzdr09pym0ggckm0000gn/T/mongomem-72722HmQIjtY53Xsf", engine: "ephemeralForTest" } }
2017-06-26T10:53:39.108+0930 E NETWORK  [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2017-06-26T10:53:39.108+0930 E NETWORK  [initandlisten]   addr already in use
2017-06-26T10:53:39.108+0930 E NETWORK  [initandlisten] Failed to set up sockets during startup.
2017-06-26T10:53:39.108+0930 E STORAGE  [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets
2017-06-26T10:53:39.108+0930 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-06-26T10:53:39.108+0930 I NETWORK  [initandlisten] shutdown: going to flush diaglog...

Mon, 26 Jun 2017 01:23:39 GMT mongodb-prebuilt-MongodHelper mongod stdout: 2017-06-26T10:53:39.113+0930 I CONTROL  [initandlisten] now exiting

Mon, 26 Jun 2017 01:23:39 GMT mongodb-prebuilt-MongodHelper mongod stdout: 2017-06-26T10:53:39.113+0930 I CONTROL  [initandlisten] shutting down with code:48

  ✖ start server Rejected promise returned by test
Mon, 26 Jun 2017 01:23:39 GMT mongodb-prebuilt-MongodHelper mongod close: 48

  1 test failed [10:53:39]

  start server

  Rejected promise returned by test

  Rejection reason:

    "Mongod shutting down"


error Command failed with exit code 1.

Get-port is working. There need additional checks, cause yarn runs tests in parallel. When you obtain free port mongod needs some time for startup, at this time starts another mongod process on same port which throws error.

You may try https://github.com/nodkz/mongodb-memory-server i spent quite huge amount of time to make it stable working with parallel testing.

The first time it runs it tries port 27017 which was taken before the tests even started. That's what's making me think get-port isn't doing what it should or at least the way to port is passed to mongodb isn't setup correctly.

Hey! Thanks for reporting this.

If it's trying port 27017 and MongoDB is constantly running before the tests then the issue is likely with get-port not detecting the port in use.
If you don't have a MongoDB server running locally and you are only booting it up using this package
(e.g. via await MongoDBServer.start() ) then @nodkz may have a point; get-port may not be detecting it due to startup times of the MongoDB in-memory server.

This was an early issue for me when developing the package but wasn't an issue later on in its use and development, so I believed it was just a temporary issue at the time.
I will try to find some time to diagnose and fix this if that's the case.

If you find out exactly where this is occurring feel free to submit a PR or let me know and I can fix.

Looks like get-port can't detect MongoDB and/or docker.

Added this to x.js.

const getPort = require('get-port');
getPort(27017).then(console.log);

Then ran this and even though mongo is using 27017 getPort returns 27017.

docker run -p 27017:27017 mongodb
node x.js