-
Notifications
You must be signed in to change notification settings - Fork 99
Add promise return for getSocket #179
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,8 @@ function createServers (callback) { | |
| function (next) { | ||
| const server = net.createServer(function () { }), | ||
| name = base === 0 ? 'test.sock' : 'test' + base + '.sock'; | ||
| let sock = path.join(socketDir, name); | ||
| const socket = path.join(socketDir, name); | ||
| let sock = socket; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just have 1 variable
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the platform is windows, we append the pipe stuff to the front of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good |
||
|
|
||
| // shamelessly stolen from foreverjs, | ||
| // https://github.com/foreverjs/forever/blob/6d143609dd3712a1cf1bc515d24ac6b9d32b2588/lib/forever/worker.js#L141-L154 | ||
|
|
@@ -46,22 +47,19 @@ function createServers (callback) { | |
|
|
||
| server.listen(sock, next); | ||
| base++; | ||
| servers.push(server); | ||
| servers.push([server, socket]); | ||
| }, callback); | ||
| } | ||
|
|
||
| function stopServers(callback, index) { | ||
| if (index < servers.length) { | ||
| servers[index].close(function (err) { | ||
| if (err) { | ||
| callback(err, false); | ||
| } else { | ||
| stopServers(callback, index + 1); | ||
| function stopServers(callback) { | ||
| _async.each(servers, function ([server, socket], next) { | ||
| server.close(function () { | ||
| if (process.platform === 'win32' && fs.existsSync(socket)) { | ||
| fs.unlinkSync(socket); | ||
| } | ||
| next(); | ||
| }); | ||
| } else { | ||
| callback(null, true); | ||
| } | ||
| }, callback); | ||
| } | ||
|
|
||
| function cleanup(callback) { | ||
|
|
@@ -74,7 +72,7 @@ function cleanup(callback) { | |
| if (fs.existsSync(badDir)) { | ||
| fs.rmdirSync(badDir); | ||
| } | ||
| stopServers(callback, 0); | ||
| stopServers(callback); | ||
| } | ||
|
|
||
| describe('portfinder', function () { | ||
|
|
@@ -88,60 +86,126 @@ describe('portfinder', function () { | |
|
|
||
| describe('with 5 existing servers', function () { | ||
| beforeAll(function (done) { | ||
| createServers(function () { | ||
| portfinder.getSocket({ | ||
| path: path.join(badDir, 'test.sock'), | ||
| }, function () { | ||
| done(); | ||
| }); | ||
|
Comment on lines
-92
to
-96
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling |
||
| }); | ||
| createServers(done); | ||
| }); | ||
|
|
||
| afterAll(function (done) { | ||
| stopServers(done); | ||
| }); | ||
|
|
||
| test('the getSocket() method should respond with the first free socket (test5.sock)', function (done) { | ||
| portfinder.getSocket({ | ||
| path: path.join(socketDir, 'test.sock'), | ||
| }, function (err, socket) { | ||
| expect(err).toBeNull(); | ||
| expect(socket).toEqual(path.join(socketDir, 'test5.sock')); | ||
| done(); | ||
| describe.each([ | ||
| ['getSocket()', false, portfinder.getSocket], | ||
| ['getSocket()', true, portfinder.getSocket], | ||
| ['getSocketPromise()', true, portfinder.getSocketPromise], | ||
| ])(`the %s method (promise: %p)`, function (name, isPromise, method) { | ||
| test('should respond with the first free socket (test5.sock)', function (done) { | ||
| if (isPromise) { | ||
| method({ | ||
| path: path.join(socketDir, 'test.sock') | ||
| }) | ||
| .then(function (socket) { | ||
| expect(socket).toEqual(path.join(socketDir, 'test5.sock')); | ||
| done(); | ||
| }) | ||
| .catch(function (err) { | ||
| done(err); | ||
| }); | ||
| } else { | ||
| method({ | ||
| path: path.join(socketDir, 'test.sock'), | ||
| }, function (err, socket) { | ||
| if (err) { | ||
| done(err); | ||
| return; | ||
| } | ||
| expect(err).toBeNull(); | ||
| expect(socket).toEqual(path.join(socketDir, 'test5.sock')); | ||
| done(); | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with no existing servers', function () { | ||
| describe('the getSocket() method', function () { | ||
| describe.each([ | ||
| ['getSocket()', false, portfinder.getSocket], | ||
| ['getSocket()', true, portfinder.getSocket], | ||
| ['getSocketPromise()', true, portfinder.getSocketPromise], | ||
| ])(`the %s method (promise: %p)`, function (name, isPromise, method) { | ||
| test("with a directory that doesn't exist should respond with the first free socket (test.sock)", function (done) { | ||
| portfinder.getSocket({ | ||
| path: path.join(badDir, 'test.sock'), | ||
| }, function (err, socket) { | ||
| expect(err).toBeNull(); | ||
| expect(socket).toEqual(path.join(badDir, 'test.sock')); | ||
| done(); | ||
| }); | ||
| if (isPromise) { | ||
| method({ | ||
| path: path.join(badDir, 'test.sock'), | ||
| }) | ||
| .then(function (socket) { | ||
| expect(socket).toEqual(path.join(badDir, 'test.sock')); | ||
| done(); | ||
| }) | ||
| .catch(function (err) { | ||
| done(err); | ||
| }); | ||
| } else { | ||
| method({ | ||
| path: path.join(badDir, 'test.sock'), | ||
| }, function (err, socket) { | ||
| if (err) { | ||
| done(err); | ||
| return; | ||
| } | ||
| expect(err).toBeNull(); | ||
| expect(socket).toEqual(path.join(badDir, 'test.sock')); | ||
| done(); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| test("with a nested directory that doesn't exist should respond with the first free socket (test.sock)", function (done) { | ||
| portfinder.getSocket({ | ||
| path: path.join(badDir, 'deeply', 'nested', 'test.sock'), | ||
| }, function (err, socket) { | ||
| expect(err).toBeNull(); | ||
| expect(socket).toEqual(path.join(badDir, 'deeply', 'nested', 'test.sock')); | ||
| done(); | ||
| }); | ||
| if (isPromise) { | ||
| method({ | ||
| path: path.join(badDir, 'deeply', 'nested', 'test.sock'), | ||
| }) | ||
| .then(function (socket) { | ||
| expect(socket).toEqual(path.join(badDir, 'deeply', 'nested', 'test.sock')); | ||
| done(); | ||
| }) | ||
| .catch(function (err) { | ||
| done(err); | ||
| }); | ||
| } else { | ||
| method({ | ||
| path: path.join(badDir, 'deeply', 'nested', 'test.sock'), | ||
| }, function (err, socket) { | ||
| expect(err).toBeNull(); | ||
| expect(socket).toEqual(path.join(badDir, 'deeply', 'nested', 'test.sock')); | ||
| done(); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| test('with a directory that exists should respond with the first free socket (test.sock)', function (done) { | ||
| portfinder.getSocket({ | ||
| path: path.join(socketDir, 'exists.sock'), | ||
| }, function (err, socket) { | ||
| expect(err).toBeNull(); | ||
| expect(socket).toEqual(path.join(socketDir, 'exists.sock')); | ||
| done(); | ||
| }); | ||
| // We don't use `test.sock` here due to some race condition on Windows in freeing the `test.sock` file | ||
| // when we close the servers. | ||
| test('with a directory that exists should respond with the first free socket (foo.sock)', function (done) { | ||
|
Comment on lines
+186
to
+188
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test was mentioning As such, I went with calling it |
||
| if (isPromise) { | ||
| method({ | ||
| path: path.join(socketDir, 'foo.sock'), | ||
| }) | ||
| .then(function (socket) { | ||
| expect(socket).toEqual(path.join(socketDir, 'foo.sock')); | ||
| done(); | ||
| }) | ||
| .catch(function (err) { | ||
| done(err); | ||
| }); | ||
| } else { | ||
| method({ | ||
| path: path.join(socketDir, 'foo.sock'), | ||
| }, function (err, socket) { | ||
| expect(err).toBeNull(); | ||
| expect(socket).toEqual(path.join(socketDir, 'foo.sock')); | ||
| done(); | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and the change to
internals.getPortabove are no-ops in terms of what they do (as the functions don't return anything), but I feel this is more "semantically correct" in that we don't expect this function to return anything if not a promise.