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

Commit d80d956

Browse files
committed
refactor(mock-server): allow auto generation of port numbers
NODE-1088
1 parent 11865bf commit d80d956

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

test/mock/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ const DEFAULT_ISMASTER = {
5757
*/
5858
module.exports = {
5959
createServer: function(port, host, options) {
60+
port = port || 0;
61+
host = host || 'localhost';
6062
options = options || {};
63+
6164
let mockServer = new Server(port, host, options);
6265
mockServers.push(mockServer);
6366
return mockServer.start();

test/mock/lib/server.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,37 @@ var Server = function(port, host, options) {
3131

3232
// Create a bson instance
3333
this.bson = new BSON();
34+
3435
// Save the settings
3536
this.host = host;
3637
this.port = port;
38+
3739
// Create a server socket
3840
this.server = net.createServer();
41+
3942
// Responses
4043
this.messages = [];
44+
4145
// state
4246
this.state = 'stopped';
47+
4348
// Number of connections
4449
this.connections = 0;
50+
4551
// sockets
4652
this.sockets = [];
4753
};
4854

4955
inherits(Server, EventEmitter);
5056

57+
Server.prototype.uri = function() {
58+
return `${this.host}:${this.port}`;
59+
};
60+
61+
Server.prototype.address = function() {
62+
return { host: this.host, port: this.port };
63+
};
64+
5165
Server.prototype.destroy = function() {
5266
var self = this;
5367
if (self.state === 'destroyed') {
@@ -111,6 +125,10 @@ Server.prototype.start = function() {
111125
});
112126

113127
self.server.listen(self.port, self.host, function() {
128+
// update address information if necessary
129+
self.host = self.server.address().address;
130+
self.port = self.server.address().port;
131+
114132
resolve(self);
115133
});
116134

0 commit comments

Comments
 (0)