Skip to content

Commit

Permalink
BF #9
Browse files Browse the repository at this point in the history
  • Loading branch information
veny committed Mar 19, 2014
1 parent 91ddb03 commit 4144632
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
9 changes: 6 additions & 3 deletions lib/gearmanode/job-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ JobServer.prototype.connect = function (callback) {
this.socket.on('connect', function () {
self.socket.setKeepAlive(true);
self.connected = true;
self.emit('ConnectInternal140319214558');
self.clientOrWorker.emit('socketConnect', self.getUid()); // trigger event
JobServer.logger.log('debug', 'connection established, uid=%s', self.getUid());
callback();
Expand Down Expand Up @@ -233,9 +234,8 @@ JobServer.prototype.send = function (data, callback) {
// invoked after connection established
connectCb = function (connectErr) {
if (connectErr instanceof Error) { // if Error, the problem has been already propagated by this.socket.on('error', ...
self.removeAllListeners('ConnectInternal140319214558'); // remove function waiting for connection
if (callback instanceof Function) { callback(connectErr); }
} else {
sendCb();
}
};

Expand All @@ -249,7 +249,10 @@ JobServer.prototype.send = function (data, callback) {
sendCb();
} else {
JobServer.logger.log('debug', 'unconnected job server, uid=%s', this.getUid());
this.connect(connectCb);
if (events.EventEmitter.listenerCount(this, 'ConnectInternal140319214558') == 0) { // connect only if there are no already waiting 'send' function (see BF #9)
this.connect(connectCb);
}
this.once('ConnectInternal140319214558', sendCb);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/gearmanode/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@


exports.VERSION_HISTORY = [
['0.1.5', '2014-19-03', 'added SET_CLIENT_ID, integration with Travis CI'],
['0.1.4', '2014-28-02', 'added CAN_DO_TIMEOUT, BF #7'],
['0.1.5', '2014-20-03', 'added SET_CLIENT_ID; integration with Travis CI; BF #9'],
['0.1.4', '2014-28-02', 'added CAN_DO_TIMEOUT; BF #7'],
['0.1.3', '2014-20-02', 'BF #6'],
['0.1.2', '2013-27-11', 'added Worker#resetAbilities'],
['0.1.1', '2013-12-11', 'BF #4; added Job#reportWarning & Job#sendWorkData'],
Expand Down
13 changes: 11 additions & 2 deletions test/test-job-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ describe('JobServer', function() {
done();
})
})
it('BF9: should connect once if two `send` invoked on unconnected server', function() {
var w = gearmanode.worker();
sinon.spy(w.jobServers[0], 'send'); // proxies original method
sinon.spy(w.jobServers[0], 'connect');
w.setWorkerId('foo');
w.setWorkerId('bar');
w.jobServers[0].send.calledTwice.should.be.true;
w.jobServers[0].connect.calledOnce.should.be.true;
})
})


Expand Down Expand Up @@ -160,11 +169,11 @@ describe('JobServer', function() {
js.setOption('foo');
})
it('should emit server error on client/worker if option is unknown', function(done) {
js.emit = sinon.spy();
sinon.spy(js, 'emit'); // proxies original method
c.once('jobServerError', function(uid, code, msg) {
uid.should.equal(js.getUid());
code.toUpperCase().should.equal(protocol.CONSTANTS.UNKNOWN_OPTION);
js.emit.callCount.should.equal(0); // emit on Job Server after emit on Client/Worker
js.emit.callCount.should.equal(1); // internal event to signal successful connection
done();
});
js.setOption('foo');
Expand Down

0 comments on commit 4144632

Please sign in to comment.