-
Notifications
You must be signed in to change notification settings - Fork 3
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
wait for tcp port available (win32) #2
base: windows
Are you sure you want to change the base?
Changes from 1 commit
ed21f8d
35cb5c8
0d9dd39
c7cf6c7
34bc5e6
5c589fd
fbd8076
5b75dcd
d1bf330
5cf785d
a58db73
425fcde
7865e33
cccc7ed
9aef07f
bc67f2d
1f0f1af
3ef6fc5
3493b4b
e99fd6e
030cdbf
8663e04
08d5c04
18c0dd2
8b1b9f7
293263c
47797fe
c850246
b8d7ef2
2d204f4
b05e0d8
9140764
45f29f3
e526c69
8ab9dd8
cf15c04
575e5d7
4fbb40b
054e412
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 |
---|---|---|
|
@@ -64,12 +64,14 @@ class Server extends EventEmitter { | |
* @param {object} [options] - Optional settings | ||
* @param {boolean} [options.stdout=false] - Forward server output from STDOUT to STDOUT | ||
* @param {boolean} [options.stderr=true] - Forward server output from STDERR to STDERR | ||
* @param {number} [options.win32Timeout=30000] - (Windows only) Max time to wait for server ready, in mS | ||
* @returns {Promise} | ||
*/ | ||
launch (cmd, args, options = {}) { | ||
if (typeof this._process !== 'undefined') throw new Error('Server already launched'); | ||
const stdout = typeof options.stdout !== 'undefined' ? options.stdout : false; | ||
const stderr = typeof options.stderr !== 'undefined' ? options.stderr : true; | ||
const win32Timeout = typeof options.win32Timeout !== 'undefined' ? options.win32Timeout : 30000; | ||
|
||
const proc = (this._process = spawn(cmd, args, { stdio: ['pipe', 'pipe', 'pipe', this._fd] })); | ||
this._srv.close(); | ||
|
@@ -90,7 +92,23 @@ class Server extends EventEmitter { | |
}); | ||
|
||
// Should be resolved by the "spawn" event, but that is only supported in Node 15+ | ||
return Promise.resolve; | ||
if (process.platform !== 'win32') return Promise.resolve; | ||
return new Promise((resolve) => { | ||
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. There is no reject handler. What's supposed to happen when it times out? 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 caller will do exactly the same (trying to connect), and will fail because the port is not ready. 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. A promise does not resolve on its own. |
||
const retryTime = 60; | ||
dmanto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const now = new Date(); | ||
dmanto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const timeToStop = new Date(now.getTime() + win32Timeout); | ||
const port = this.port; | ||
(function loop () { | ||
const connection = net.connect(port, resolve); | ||
connection.on('error', err => { | ||
if (err.code === 'ECONNREFUSED') { | ||
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. And if it's something else? 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. should resolve the promise. But it actually doesn't, my mistake, see my answer about the logic a few minutes ago 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. It should reject the promise, not resolve. 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 only thing the loop does is to wait enough time to get the server in a defined (either right or wrong) state. So if there is an error different from ECONNREFUSED there is no need to stay waiting, there is no argue on that. But I think there is no added value in reject the promise with an error either. This is because you will try to connect by yourself after the launch of the server, and you will be sucesfull or not, and get the right error in any case. Anyway the promise could be rejected when there is an error detected, the change itself is trivial (although the specific tests are not that easy) but I cannot think of any use case in which this can be beneficial for the user of the module. |
||
if (new Date() < timeToStop) { | ||
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. And if it times out we just do nothing? 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. yes the caller will take care, I mean will recive the exact same error 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. How does the error reach the caller? 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 logic is like:
so Ooops, I need to resolve at the end of the anonymous function (or reject, but that will be handled much better by the caller, when it tries to connect) |
||
setTimeout(loop, retryTime); | ||
} | ||
} | ||
}); | ||
})(); | ||
}); | ||
} | ||
|
||
/** | ||
|
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.
As far as i can see there is no reason at all for this to be Windows specific.
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.
It will have no effect on other platforms, at least how is it written.
If you mean to wait for connection ready at the TCP port in all cases, it could be 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.
The way it is done now makes little sense. Since the module doesn't become more portable, it just allows someone to use it in a Windows specific way. Instead of enabling one test to run on all platforms.