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

Commit

Permalink
feat(mock-server): add the ability to set a message handler
Browse files Browse the repository at this point in the history
 - modified tests to remove 3s from test run
 - synchronized all destroy calls to remove need for
   timeout-based guess
  • Loading branch information
mbroadst committed Sep 16, 2017
1 parent 05f83b0 commit 9a8b815
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 62 deletions.
30 changes: 24 additions & 6 deletions test/mock/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,21 @@ var Server = function(port, host, options) {
inherits(Server, EventEmitter);

Server.prototype.destroy = function() {
this.state = 'destroyed';
this.server.close();
var self = this;
if (self.state === 'destroyed') {
return Promise.resolve();
}

return new Promise(function(resolve, reject) {
self.sockets.forEach(function(x) {
x.destroy();
});

this.sockets.forEach(function(x) {
x.destroy();
self.server.close(function(err) {
if (err) return reject(err);
self.state = 'destroyed';
resolve();
});
});
};

Expand All @@ -61,7 +71,7 @@ Server.prototype.start = function() {
// Return start promise
return new Promise(function(resolve, reject) {
self.server.on('error', function(err) {
console.log("!!!!!!!!!!!!!!!!!!!! error reject")
console.log('!!!!!!!!!!!!!!!!!!!! error reject');
reject(err);
});

Expand Down Expand Up @@ -104,7 +114,11 @@ Server.prototype.start = function() {

self.on('message', function(message, connection) {
var request = new Request(self, connection, message);
self.messages.push(request);
if (self.messageHandler) {
self.messageHandler(request);
} else {
self.messages.push(request);
}
});

self.state = 'running';
Expand Down Expand Up @@ -133,6 +147,10 @@ Server.prototype.receive = function() {
});
};

Server.prototype.setMessageHandler = function(messageHandler) {
this.messageHandler = messageHandler;
};

var protocol = function(self, message) {
var index = 0;
self.isCompressed = false;
Expand Down
81 changes: 25 additions & 56 deletions test/tests/functional/rs_mocks/add_remove_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe('ReplSet Add Remove (mocks)', function() {
}
});

it('Successfully remove a secondary server from the set', {
it.only('Successfully remove a secondary server from the set', {
metadata: {
requires: {
generators: true,
Expand All @@ -258,7 +258,6 @@ describe('ReplSet Add Remove (mocks)', function() {
var firstSecondaryServer = null;
var secondSecondaryServer = null;
var arbiterServer = null;
var running = true;
var currentIsMasterIndex = 0;

// Default message fields
Expand Down Expand Up @@ -365,60 +364,32 @@ describe('ReplSet Add Remove (mocks)', function() {
secondSecondaryServer = yield mockupdb.createServer(32003, 'localhost');
arbiterServer = yield mockupdb.createServer(32002, 'localhost');

// Primary state machine
co(function*() {
while (running) {
var request = yield primaryServer.receive();
var doc = request.document;

if (doc.ismaster) {
request.reply(primary[currentIsMasterIndex]);
}
primaryServer.setMessageHandler(function(request) {
var doc = request.document;
if (doc.ismaster) {
request.reply(primary[currentIsMasterIndex]);
}
}).catch(function() {
// console.log(err.stack);
});

// First secondary state machine
co(function*() {
while (running) {
var request = yield firstSecondaryServer.receive();
var doc = request.document;

if (doc.ismaster) {
request.reply(firstSecondary[currentIsMasterIndex]);
}
firstSecondaryServer.setMessageHandler(function(request) {
var doc = request.document;
if (doc.ismaster) {
request.reply(firstSecondary[currentIsMasterIndex]);
}
}).catch(function() {
// console.log(err.stack);
});

// Second secondary state machine
co(function*() {
while (running) {
var request = yield secondSecondaryServer.receive();
var doc = request.document;

if (doc.ismaster) {
request.reply(secondSecondary[currentIsMasterIndex]);
}
secondSecondaryServer.setMessageHandler(function(request) {
var doc = request.document;
if (doc.ismaster) {
request.reply(secondSecondary[currentIsMasterIndex]);
}
}).catch(function() {
// console.log(err.stack);
});

// Arbiter state machine
co(function*() {
while (running) {
var request = yield arbiterServer.receive();
var doc = request.document;

if (doc.ismaster) {
request.reply(arbiter[currentIsMasterIndex]);
}
arbiterServer.setMessageHandler(function(request) {
var doc = request.document;
if (doc.ismaster) {
request.reply(arbiter[currentIsMasterIndex]);
}
}).catch(function() {
// console.log(err.stack);
});
});

Expand Down Expand Up @@ -471,18 +442,16 @@ describe('ReplSet Add Remove (mocks)', function() {
expect(server.s.replicaSetState.primary).to.not.be.null;
expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

primaryServer.destroy();
firstSecondaryServer.destroy();
secondSecondaryServer.destroy();
arbiterServer.destroy();
server.destroy();
running = false;

setTimeout(function() {
expect(Object.keys(Connection.connections())).to.have.length(0);
Promise.all([
server.destroy(),
primaryServer.destroy(),
firstSecondaryServer.destroy(),
secondSecondaryServer.destroy(),
arbiterServer.destroy()
]).then(function() {
Connection.disableConnectionAccounting();
done();
}, 2000);
});
}
});

Expand Down

0 comments on commit 9a8b815

Please sign in to comment.