Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

feat(net): Add method to check if port is in LISTEN state #1043

Merged
merged 1 commit into from
Jan 30, 2018

Conversation

rosen-vladimirov
Copy link
Collaborator

Add method in net service to check if port is in LISTEN state. Add tests for it.
The method has different implementation for macOS, Linux and Windows - added tests for each of the implementations.

this.$errors.failWithoutHelp(`Unable to check for free ports on ${platform}. Supported platforms are: ${_.keys(platformData).join(", ")}`);
}

while (true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of while (true) you can set the condition while (new Date().getTime() < endTime) and delete the break; code below.

IMO it'd be a bit clearer than looking at what seems to be an endless loop at a first glance

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current flow executes at least one check, no matter of the passed timeout. Changing the while statement will change this behavior.
Also the current workflow checks if a timeout is passed and performs at least one check after that. Changing the while statement will change this behavior as well, as we have sleep at the end of the while. The sleep cannot be moved at the first line of the while as we may not have to wait for it.
So the desired workflow is:

  1. Check if the port is in LISTEN state.
  2. If yes - break. If not, check if timeout is passed and in case not - sleep for specified interval.
  3. Check if port is in LISTEN state.
  4. If yes - break. If not, check if timeout is passed and in case not - sleep for specified interval.
    ... Repeat until timeout is reached.

public async waitForPortToListen(port: number, timeout: number, interval?: number): Promise<boolean> {
interval = interval || Net.DEFAULT_INTERVAL;
const endTime = new Date().getTime() + timeout;
const platformData: IDictionary<any> = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can introduce an strongly typed interface here instead of any. Not a merge-stopper though

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to introduce an interface for an object that's created and used inside a single method.

* @param {number} inteval @optional The amount of time between each check.
* @returns {boolean} true in case port is in LISTEN state, false otherwise.
*/
waitForPortToListen(port: number, timeout: number, interval?: number): Promise<boolean>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can optionally introduce an interface here instead of accepting three different arguments

@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/fix-sim-debug-brk branch from 717b5f4 to c3e7d93 Compare January 30, 2018 11:48
Add method in net service to check if port is in LISTEN state. Add tests for it.
The method has different implementation for macOS, Linux and Windows - added tests for each of the implementations.
@rosen-vladimirov rosen-vladimirov force-pushed the vladimirov/fix-sim-debug-brk branch from c3e7d93 to a3beed4 Compare January 30, 2018 12:07
@rosen-vladimirov
Copy link
Collaborator Author

ping @Mitko-Kerezov , comments are fixed

Copy link
Contributor

@Mitko-Kerezov Mitko-Kerezov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay to me

@rosen-vladimirov rosen-vladimirov merged commit 623c2d8 into release Jan 30, 2018
@rosen-vladimirov rosen-vladimirov deleted the vladimirov/fix-sim-debug-brk branch January 30, 2018 14:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants