Permalink
Browse files

Implemented queue, for loop put test added

(docco is broken, cannot generate up to date docs atm)
  • Loading branch information...
1 parent d661330 commit a882eb508169c7f9af8d55345e26d9bdc4fc9829 Pascal Opitz committed Jul 4, 2012
Showing with 75 additions and 11 deletions.
  1. +29 −9 lib/beanstalk_client.js
  2. +2 −2 package.json
  3. +44 −0 tests/test_put_in_forloop.js
View
@@ -113,6 +113,9 @@ function BeanstalkClient() {
this.port = 11300;
this.conn;
this.default_priority = 10;
+
+ this.queue = [];
+ this.waitingForResponses = false;
};
util.inherits(BeanstalkClient, events.EventEmitter);
@@ -131,24 +134,41 @@ BeanstalkClient.prototype.Instance = function(server) {
// executes command
BeanstalkClient.prototype.command = function(obj) {
- var _self = this;
+ var _self = this;
var cmd = new BeanstalkCommand();
- cmd.addListener('data', cmd.responseHandler);
+
+ // makes sure that if there's another command queued, it gets done
+ cmd.addListener('command_done', function(data) {
+ if(_self.queue.length) {
+ var next = _self.queue.shift();
+ process.nextTick(function() {
+ _self.conn.write(next);
+ });
+ } else {
+ _self.waitingForResponse = false;
+ }
+ });
+
// handles data that comes back from the connection
var dataHandler = function(data) {
Debug.log('response:');
Debug.log(data);
- cmd.emit('data', data, obj);
+ cmd.responseHandler.call(cmd, data, obj);
};
// pushes commands to the server
var requestExec = function(data) {
- Debug.log('request:');
- Debug.log(data);
- process.nextTick(function() {
- _self.conn.write(data);
- });
+ if(!_self.waitingForResponse) {
+ _self.waitingForResponse = true;
+ Debug.log('request:');
+ Debug.log(data);
+ process.nextTick(function() {
+ _self.conn.write(data);
+ });
+ } else {
+ _self.queue.push(data);
+ }
};
if(!this.conn) {
@@ -177,7 +197,7 @@ BeanstalkClient.prototype.command = function(obj) {
Debug.log('connection closed');
});
} else {
- this.conn.removeAllListeners('data');
+ this.conn.removeAllListeners('data');
requestExec(obj.command);
}
View
@@ -2,8 +2,8 @@
"name": "nodestalker"
, "description": "A Beanstalk client for node.js"
, "keywords": ["beanstalkd", "queue"]
- , "version": "0.1.3"
- , "author": "Pascal Opitz <pascal@ilikecode.co.uk> (http://www.ilikecode.co.uk)"
+ , "version": "0.1.4"
+ , "author": "Pascal Opitz <contact@pascalopitz.com> (http://blog.pascalopitz.com)"
, "repository": {
"type": "git"
, "url": "git://github.com/pascalopitz/nodestalker.git"
@@ -0,0 +1,44 @@
+var assert = require('assert');
+var bs = require('../lib/beanstalk_client');
+
+console.log('testing put in forloop');
+
+var port = 11333;
+
+var net = require('net');
+var cnt = 0;
+
+var mock_server = net.createServer(function(conn) {
+ conn.on('data', function(data) {
+ if(String(data).indexOf('put') > -1) {
+ cnt += 1;
+ conn.write("INSERTED "+cnt+"\r\n");
+ }
+
+ if(String(data) == "use default\r\n") {
+ conn.write('USING default\r\n');
+ mock_server.close();
+ }
+ });
+});
+mock_server.listen(port);
+
+var client = bs.Client('127.0.0.1:' + port);
+
+client.use('default').onSuccess(function(data) {
+ var completed = 0;
+
+ for (var i=0;i<5;i++) {
+
+ client.put("foo", 100, 0).onSuccess(function(data) {
+ completed += 1;
+ assert.equal(completed, data);
+
+
+ if(completed === 5) {
+ client.disconnect();
+ }
+ });
+ }
+});
+

0 comments on commit a882eb5

Please sign in to comment.