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

Add listening method to net + tests #4743

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/http.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,11 @@ parameter is 511 (not 512).
This function is asynchronous. The last parameter `callback` will be added as
a listener for the `'listening'` event. See also [`net.Server.listen(port)`][].

### server.listening

A Boolean indicating whether or not the server is listening for
connections.

### server.maxHeadersCount

Limits maximum incoming headers count, equal to 1000 by default. If set to 0 -
Expand Down
5 changes: 5 additions & 0 deletions doc/api/net.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ would be to wait a second and then try again. This can be done with

(Note: All sockets in Node.js set `SO_REUSEADDR` already)

### server.listening

A Boolean indicating whether or not the server is listening for
connections.

### server.maxConnections

Set this property to reject connections when the server's connection count gets
Expand Down
8 changes: 8 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,14 @@ Server.prototype.listen = function() {
return self;
};

Object.defineProperty(Server.prototype, 'listening', {
get: function() {
return !!this._handle;
},
configurable: true,
enumerable: true
});

Server.prototype.address = function() {
if (this._handle && this._handle.getsockname) {
var out = {};
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-http-listening.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer();

assert.strictEqual(server.listening, false);

server.listen(common.PORT, common.mustCall(() => {
assert.strictEqual(server.listening, true);

server.close(common.mustCall(() => {
assert.strictEqual(server.listening, false);
}));
}));
16 changes: 16 additions & 0 deletions test/parallel/test-net-listening.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
Copy link
Contributor

Choose a reason for hiding this comment

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

All of my comments from the other test apply here as well :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, all those changes will be on my next commit, as soon as I match it with the contribution guidelines.

const common = require('../common');
const assert = require('assert');
const net = require('net');

const server = net.createServer();

assert.strictEqual(server.listening, false);

server.listen(common.PORT, common.mustCall(() => {
assert.strictEqual(server.listening, true);

server.close(common.mustCall(() => {
assert.strictEqual(server.listening, false);
}));
}));