Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Changed tests to use helper.js

  • Loading branch information...
commit 5576a7cda4fcf3615d3368683e7fe75163da1fbb 1 parent 27a56c2
pascalopitz authored
42 tests/helper.js
... ... @@ -0,0 +1,42 @@
  1 +var bs = require('../lib/beanstalk_client');
  2 +var net = require('net');
  3 +
  4 +var port = process.env.PORT || 11333;
  5 +
  6 +var mock = process.env.BEANSTALKD !== '1';
  7 +var mock_server;
  8 +
  9 +var connection;
  10 +
  11 +module.exports = {
  12 + bind : function (fn, closeOnEnd) {
  13 +
  14 + if(mock) {
  15 + mock_server = net.createServer(function(conn) {
  16 + connection = conn;
  17 +
  18 + connection.on('data', function (data) {
  19 + fn.call(mock_server, connection, data);
  20 + });
  21 +
  22 + if(closeOnEnd === true) {
  23 + closeOnEnd = function () {
  24 + mock_server.close();
  25 + }
  26 + }
  27 +
  28 + if(closeOnEnd) {
  29 + connection.on('end', function () {
  30 + closeOnEnd.call(mock_server);
  31 + });
  32 + }
  33 + });
  34 +
  35 + mock_server.listen(port);
  36 + }
  37 +
  38 + },
  39 + getClient : function () {
  40 + return bs.Client('127.0.0.1:' + port);
  41 + }
  42 +}
28 tests/test_ignore.js
... ... @@ -1,25 +1,15 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing ignore');
5 2
6   -var port = 11333;
7   -
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == "ignore default\r\n") {
12   - conn.write('WATCHING');
13   - }
14   - });
15   -
16   - conn.on('end', function() {
17   - mock_server.close();
18   - });
19   -});
20   -mock_server.listen(port);
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
  5 +
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == "ignore default\r\n") {
  8 + conn.write('WATCHING');
  9 + }
  10 +}, true);
  11 +var client = helper.getClient();
21 12
22   -var client = bs.Client('127.0.0.1:' + port);
23 13
24 14 var success = false;
25 15 var error = false;
29 tests/test_kick.js
... ... @@ -1,25 +1,14 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing kick');
5 2
6   -var port = 11333;
7   -
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == "kick 10\r\n") {
12   - conn.write('KICKED');
13   - }
14   - });
15   -
16   - conn.on('end', function() {
17   - mock_server.close();
18   - });
19   -});
20   -mock_server.listen(port);
21   -
22   -var client = bs.Client('127.0.0.1:' + port);
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
  5 +
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == "kick 10\r\n") {
  8 + conn.write('KICKED');
  9 + }
  10 +}, true);
  11 +var client = helper.getClient();
23 12
24 13 var success = false;
25 14 var error = false;
34 tests/test_list_tube_used.js
... ... @@ -1,29 +1,19 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing list_tube_used');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == "list-tube-used\r\n") {
12   - var response = 'USING';
13   - response += "\r\n";
14   - response += "tube"
15   - response += "\r\n";
16   - conn.write(response);
17   - }
18   - });
19   -
20   - conn.on('end', function() {
21   - mock_server.close();
22   - });
23   -});
24   -mock_server.listen(port);
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == "list-tube-used\r\n") {
  8 + var response = 'USING';
  9 + response += "\r\n";
  10 + response += "tube"
  11 + response += "\r\n";
  12 + conn.write(response);
  13 + }
  14 +}, true);
  15 +var client = helper.getClient();
25 16
26   -var client = bs.Client('127.0.0.1:' + port);
27 17 var success = false;
28 18 var error = false;
29 19
36 tests/test_list_tubes.js
... ... @@ -1,29 +1,19 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing list_tubes');
5 2
6   -var port = 11333;
7   -
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == "list-tubes\r\n") {
12   - var response = 'OK';
13   - response += "\r\n";
14   - response += "---\n- default\n - second\n"
15   - response += "\r\n";
16   - conn.write(response);
17   - }
18   - });
19   -
20   - conn.on('end', function() {
21   - mock_server.close();
22   - });
23   -});
24   -mock_server.listen(port);
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
  5 +
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == "list-tubes\r\n") {
  8 + var response = 'OK';
  9 + response += "\r\n";
  10 + response += "---\n- default\n - second\n"
  11 + response += "\r\n";
  12 + conn.write(response);
  13 + }
  14 +}, true);
  15 +var client = helper.getClient();
25 16
26   -var client = bs.Client('127.0.0.1:' + port);
27 17
28 18 var success = false;
29 19 var error = false;
41 tests/test_list_tubes_watched.js
... ... @@ -1,33 +1,24 @@
  1 +console.log('testing list_tubes_watched');
  2 +
1 3 var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
  4 +var helper = require('./helper');
3 5
4   -console.log('testing list_tubes_watched');
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == "watch default\r\n") {
  8 + conn.write("WATCHING\r\n");
  9 + }
5 10
6   -var port = 11333;
7   -
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == "watch default\r\n") {
12   - conn.write("WATCHING\r\n");
13   - }
14   -
15   - if(String(data) == "list-tubes-watched\r\n") {
16   - var response = 'OK';
17   - response += "\r\n";
18   - response += "---\n- default\n - second\n"
19   - response += "\r\n";
20   - conn.write(response);
21   - }
22   - });
  11 + if(String(data) == "list-tubes-watched\r\n") {
  12 + var response = 'OK';
  13 + response += "\r\n";
  14 + response += "---\n- default\n - second\n"
  15 + response += "\r\n";
  16 + conn.write(response);
  17 + }
  18 +}, true);
  19 +var client = helper.getClient();
23 20
24   - conn.on('end', function() {
25   - mock_server.close();
26   - });
27   -});
28   -mock_server.listen(port);
29 21
30   -var client = bs.Client('127.0.0.1:' + port);
31 22 var success = false;
32 23 var error = false;
33 24
38 tests/test_multiline_put.js
... ... @@ -1,30 +1,24 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing multiline put, peek, delete');
5 2
6   -var port = 11333;
7   -
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data).indexOf('put') > -1) {
12   - conn.write("INSERTED 10\r\n");
13   - }
14   -
15   - if(String(data) == 'peek 10\r\n') {
16   - conn.write("FOUND 10 13\r\ntest\r\nhere\r\n\r\n");
17   - }
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
18 5
19   - if(String(data) == "delete 10\r\n") {
20   - conn.write("DELETED\r\n");
21   - mock_server.close();
22   - }
23   - });
  6 +helper.bind(function(conn, data) {
  7 + if(String(data).indexOf('put') > -1) {
  8 + conn.write("INSERTED 10\r\n");
  9 + }
  10 +
  11 + if(String(data) == 'peek 10\r\n') {
  12 + conn.write("FOUND 10 13\r\ntest\r\nhere\r\n\r\n");
  13 + }
  14 +
  15 + if(String(data) == "delete 10\r\n") {
  16 + conn.write("DELETED\r\n");
  17 + this.close();
  18 + }
24 19 });
25   -mock_server.listen(port);
  20 +var client = helper.getClient();
26 21
27   -var client = bs.Client('127.0.0.1:' + port);
28 22 var success = false;
29 23 var error = false;
30 24
66 tests/test_peek_buried.js
... ... @@ -1,49 +1,39 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing use, put, watch, reserve, bury, peek_buried');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == 'use burytest\r\n') {
12   - conn.write("USING\r\n");
13   - }
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == 'use burytest\r\n') {
  8 + conn.write("USING\r\n");
  9 + }
14 10
15   - if(String(data).indexOf('put') > -1) {
16   - conn.write("INSERTED 9\r\n");
17   - }
18   -
19   - if(String(data) == 'watch burytest\r\n') {
20   - conn.write("WATCHING\r\n");
21   - }
22   -
23   - if(String(data) == 'reserve\r\n') {
24   - conn.write("RESERVED 9 8\r\nburytest\r\n");
25   - }
  11 + if(String(data).indexOf('put') > -1) {
  12 + conn.write("INSERTED 9\r\n");
  13 + }
  14 +
  15 + if(String(data) == 'watch burytest\r\n') {
  16 + conn.write("WATCHING\r\n");
  17 + }
  18 +
  19 + if(String(data) == 'reserve\r\n') {
  20 + conn.write("RESERVED 9 8\r\nburytest\r\n");
  21 + }
26 22
27   - if(String(data) == 'bury 9 10\r\n') {
28   - conn.write("BURIED\r\n");
29   - }
  23 + if(String(data) == 'bury 9 10\r\n') {
  24 + conn.write("BURIED\r\n");
  25 + }
30 26
31   - if(String(data) == 'peek-buried\r\n') {
32   - conn.write("FOUND 9 8\r\nburytest\r\n");
33   - }
  27 + if(String(data) == 'peek-buried\r\n') {
  28 + conn.write("FOUND 9 8\r\nburytest\r\n");
  29 + }
34 30
35   - if(String(data) == "delete 9\r\n") {
36   - conn.write("DELETED\r\n");
37   - }
38   - });
39   -
40   - conn.on('end', function() {
41   - mock_server.close();
42   - });
43   -});
44   -mock_server.listen(port);
  31 + if(String(data) == "delete 9\r\n") {
  32 + conn.write("DELETED\r\n");
  33 + }
  34 +}, true);
  35 +var client = helper.getClient();
45 36
46   -var client = bs.Client('127.0.0.1:' + port);
47 37
48 38 var success = false;
49 39 var error = false;
26 tests/test_peek_delayed.js
... ... @@ -1,25 +1,15 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing peek_delayed');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == 'peek-delayed\r\n') {
12   - conn.write("OK\r\n");
13   - }
14   - });
15   -
16   - conn.on('end', function(){
17   - mock_server.close();
18   - })
19   -});
20   -mock_server.listen(port);
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == 'peek-delayed\r\n') {
  8 + conn.write("OK\r\n");
  9 + }
  10 +}, true);
  11 +var client = helper.getClient();
21 12
22   -var client = bs.Client('127.0.0.1:' + port);
23 13
24 14 var success = false;
25 15 var error = false;
38 tests/test_peek_ready.js
... ... @@ -1,33 +1,23 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing put, peek_ready, delete');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data).indexOf('put') > -1) {
12   - conn.write("INSERTED 10\r\n");
13   - }
  6 +helper.bind(function(conn, data) {
  7 + if(String(data).indexOf('put') > -1) {
  8 + conn.write("INSERTED 10\r\n");
  9 + }
14 10
15   - if(String(data) == 'peek-ready\r\n') {
16   - conn.write("FOUND 10 7\r\ntest\r\n");
17   - }
  11 + if(String(data) == 'peek-ready\r\n') {
  12 + conn.write("FOUND 10 7\r\ntest\r\n");
  13 + }
18 14
19   - if(String(data) == 'delete 10\r\n') {
20   - conn.write("DELETED\r\n");
21   - }
22   - });
23   -
24   - conn.on('end', function(){
25   - mock_server.close();
26   - })
27   -});
28   -mock_server.listen(port);
  15 + if(String(data) == 'delete 10\r\n') {
  16 + conn.write("DELETED\r\n");
  17 + }
  18 +}, true);
  19 +var client = helper.getClient();
29 20
30   -var client = bs.Client('127.0.0.1:' + port);
31 21
32 22 var success = false;
33 23 var error = false;
35 tests/test_put_in_forloop.js
... ... @@ -1,29 +1,22 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing put in forloop');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9 6 var cnt = 0;
10 7
11   -var mock_server = net.createServer(function(conn) {
12   - conn.on('data', function(data) {
13   - if(String(data).indexOf('put') > -1) {
14   - cnt += 1;
15   - conn.write("INSERTED "+cnt+"\r\n");
16   - }
17   -
18   - if(String(data) == "use default\r\n") {
19   - conn.write('USING default\r\n');
20   - mock_server.close();
21   - }
22   - });
23   -});
24   -mock_server.listen(port);
25   -
26   -var client = bs.Client('127.0.0.1:' + port);
  8 +helper.bind(function(conn, data) {
  9 + if(String(data).indexOf('put') > -1) {
  10 + cnt += 1;
  11 + conn.write("INSERTED "+cnt+"\r\n");
  12 + }
  13 +
  14 + if(String(data) == "use default\r\n") {
  15 + conn.write('USING default\r\n');
  16 + this.close();
  17 + }
  18 +}, false);
  19 +var client = helper.getClient();
27 20
28 21 client.use('default').onSuccess(function(data) {
29 22 var completed = 0;
38 tests/test_put_peek_delete.js
... ... @@ -1,33 +1,23 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing put, peek, delete');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data).indexOf('put') > -1) {
12   - conn.write("INSERTED 10\r\n");
13   - }
  6 +helper.bind(function(conn, data) {
  7 + if(String(data).indexOf('put') > -1) {
  8 + conn.write("INSERTED 10\r\n");
  9 + }
14 10
15   - if(String(data) == 'peek 10\r\n') {
16   - conn.write("FOUND 10 7\r\ntest\r\n");
17   - }
  11 + if(String(data) == 'peek 10\r\n') {
  12 + conn.write("FOUND 10 7\r\ntest\r\n");
  13 + }
18 14
19   - if(String(data) == 'delete 10\r\n') {
20   - conn.write("DELETED\r\n");
21   - }
22   - });
23   -
24   - conn.on('end', function(){
25   - mock_server.close();
26   - })
27   -});
28   -mock_server.listen(port);
  15 + if(String(data) == 'delete 10\r\n') {
  16 + conn.write("DELETED\r\n");
  17 + }
  18 +}, true);
  19 +var client = helper.getClient();
29 20
30   -var client = bs.Client('127.0.0.1:' + port);
31 21
32 22 var success = false;
33 23 var error = false;
72 tests/test_put_stats_job_delete.js
... ... @@ -1,50 +1,40 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing put stats_job');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data).indexOf('put') > -1) {
12   - conn.write("INSERTED 3\r\n");
13   - }
  6 +helper.bind(function(conn, data) {
  7 + if(String(data).indexOf('put') > -1) {
  8 + conn.write("INSERTED 3\r\n");
  9 + }
14 10
15   - if(String(data) == 'stats-job 3\r\n') {
16   - response = "";
17   - response += "OK 141\r\n";
18   - response += "---\n";
19   - response += "id: 3\n";
20   - response += "tube: default\n";
21   - response += "state: ready\n";
22   - response += "pri: 10\n";
23   - response += "age: 0\n";
24   - response += "delay: 0\n";
25   - response += "ttr: 100000\n";
26   - response += "time-left: 0\n";
27   - response += "reserves: 0\n";
28   - response += "timeouts: 0\n";
29   - response += "releases: 0\n";
30   - response += "buries: 0\n";
31   - response += "kicks: 0\n";
32   - response += "\r\n";
33   - conn.write(response);
34   - }
  11 + if(String(data) == 'stats-job 3\r\n') {
  12 + response = "";
  13 + response += "OK 141\r\n";
  14 + response += "---\n";
  15 + response += "id: 3\n";
  16 + response += "tube: default\n";
  17 + response += "state: ready\n";
  18 + response += "pri: 10\n";
  19 + response += "age: 0\n";
  20 + response += "delay: 0\n";
  21 + response += "ttr: 100000\n";
  22 + response += "time-left: 0\n";
  23 + response += "reserves: 0\n";
  24 + response += "timeouts: 0\n";
  25 + response += "releases: 0\n";
  26 + response += "buries: 0\n";
  27 + response += "kicks: 0\n";
  28 + response += "\r\n";
  29 + conn.write(response);
  30 + }
35 31
36   - if(String(data) == 'delete 3\r\n') {
37   - conn.write("DELETED\r\n");
38   - }
39   - });
40   -
41   - conn.on('end', function(){
42   - mock_server.close();
43   - })
44   -});
45   -mock_server.listen(port);
  32 + if(String(data) == 'delete 3\r\n') {
  33 + conn.write("DELETED\r\n");
  34 + }
  35 +}, true);
  36 +var client = helper.getClient();
46 37
47   -var client = bs.Client('127.0.0.1:' + port);
48 38
49 39 var success = false;
50 40 var error = false;
30 tests/test_quit.js
... ... @@ -1,26 +1,20 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing quit');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == "use default\r\n") {
12   - conn.write('USING default\r\n');
13   - }
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == "use default\r\n") {
  8 + conn.write('USING default\r\n');
  9 + }
14 10
15   - if(String(data) == "quit\r\n") {
16   - conn.destroy();
17   - mock_server.close();
18   - }
19   - });
20   -});
21   -mock_server.listen(port);
  11 + if(String(data) == "quit\r\n") {
  12 + conn.destroy();
  13 + this.close();
  14 + }
  15 +}, false);
  16 +var client = helper.getClient();
22 17
23   -var client = bs.Client('127.0.0.1:' + port);
24 18
25 19 var success = false;
26 20 var closed = false;
63 tests/test_reserve.js
... ... @@ -1,49 +1,42 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing watch, reserve, use, put');
5 2
6 3 var port = 11333;
7 4
8   -var net = require('net');
9 5 var disconnected = 0;
10 6
11   -var mock_server = net.createServer(function(conn) {
12   -
13   - conn.on('data', function(data) {
14   - if(String(data) == 'use reservetest\r\n') {
15   - conn.write("USING\r\n");
16   - }
  7 +var assert = require('assert');
  8 +var helper = require('./helper');
17 9
18   - if(String(data).indexOf('put') > -1) {
19   - conn.write("INSERTED 9\r\n");
20   - }
21   -
22   - if(String(data) == 'watch reservetest\r\n') {
23   - conn.write("WATCHING\r\n");
24   - }
25   -
26   - if(String(data) == 'reserve\r\n') {
27   - conn.write("RESERVED 9 4\r\ntest\r\n");
28   - }
  10 +helper.bind(function(conn, data) {
  11 + if(String(data) == 'use reservetest\r\n') {
  12 + conn.write("USING\r\n");
  13 + }
29 14
30   - if(String(data) == "delete 9\r\n") {
31   - conn.write("DELETED\r\n");
32   - }
33   - });
  15 + if(String(data).indexOf('put') > -1) {
  16 + conn.write("INSERTED 9\r\n");
  17 + }
34 18
35   - conn.on('end', function() {
36   - disconnected = disconnected + 1;
37   -
38   - if(disconnected == 2) {
39   - mock_server.close();
40   - }
41   - });
  19 + if(String(data) == 'watch reservetest\r\n') {
  20 + conn.write("WATCHING\r\n");
  21 + }
  22 +
  23 + if(String(data) == 'reserve\r\n') {
  24 + conn.write("RESERVED 9 4\r\ntest\r\n");
  25 + }
  26 +
  27 + if(String(data) == "delete 9\r\n") {
  28 + conn.write("DELETED\r\n");
  29 + }
  30 +}, function() {
  31 + disconnected = disconnected + 1;
  32 +
  33 + if(disconnected == 2) {
  34 + this.close();
  35 + }
42 36 });
43   -mock_server.listen(port);
44 37
45   -var client = bs.Client('127.0.0.1:' + port);
46   -var client2 = bs.Client('127.0.0.1:' + port);
  38 +var client = helper.getClient();
  39 +var client2 = helper.getClient();
47 40
48 41 var success = false;
49 42 var error = false;
28 tests/test_reserve_with_timeout.js
... ... @@ -1,27 +1,15 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing reserve_with_timeout');
5 2
6   -var port = 11333;
7   -
8   -var net = require('net');
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
9 5
10   -var mock_server = net.createServer(function(conn) {
11   -
12   - conn.on('data', function(data) {
13   - if(String(data) == 'reserve-with-timeout 2\r\n') {
14   - conn.write("RESERVED 9 4\r\ntest\r\n");
15   - }
16   - });
17   -
18   - conn.on('end', function() {
19   - mock_server.close();
20   - });
21   -});
22   -mock_server.listen(port);
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == 'reserve-with-timeout 2\r\n') {
  8 + conn.write("RESERVED 9 4\r\ntest\r\n");
  9 + }
  10 +}, true);
  11 +var client = helper.getClient();
23 12
24   -var client = bs.Client('127.0.0.1:' + port);
25 13
26 14 var success = false;
27 15 var error = false;
122 tests/test_stats.js
... ... @@ -1,73 +1,63 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing stats');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == 'stats\r\n') {
12   - var response = "";
13   - response += "OK 813\r\n";
14   - response += "---\n";
15   - response += "current-jobs-urgent: 0\n";
16   - response += "current-jobs-ready: 0\n";
17   - response += "current-jobs-reserved: 0\n";
18   - response += "current-jobs-delayed: 0\n";
19   - response += "current-jobs-buried: 0\n";
20   - response += "cmd-put: 10\n";
21   - response += "cmd-peek: 2\n";
22   - response += "cmd-peek-ready: 0\n";
23   - response += "cmd-peek-delayed: 0\n";
24   - response += "cmd-peek-buried: 1\n";
25   - response += "cmd-reserve: 2\n";
26   - response += "cmd-reserve-with-timeout: 0\n";
27   - response += "cmd-delete: 10\n";
28   - response += "cmd-release: 0\n";
29   - response += "cmd-use: 2\n";
30   - response += "cmd-watch: 2\n";
31   - response += "cmd-ignore: 0\n";
32   - response += "cmd-bury: 1\n";
33   - response += "cmd-kick: 0\n";
34   - response += "cmd-touch: 0\n";
35   - response += "cmd-stats: 1\n";
36   - response += "cmd-stats-job: 6\n";
37   - response += "cmd-stats-tube: 0\n";
38   - response += "cmd-list-tubes: 15\n";
39   - response += "cmd-list-tube-used: 0\n";
40   - response += "cmd-list-tubes-watched: 0\n";
41   - response += "cmd-pause-tube: 0\n";
42   - response += "job-timeouts: 0\n";
43   - response += "total-jobs: 10\n";
44   - response += "max-job-size: 65535\n";
45   - response += "current-tubes: 1\n";
46   - response += "current-connections: 1\n";
47   - response += "current-producers: 0\n";
48   - response += "current-workers: 0\n";
49   - response += "current-waiting: 0\n";
50   - response += "total-connections: 27\n";
51   - response += "pid: 2759\n";
52   - response += "version: 1.4.6\n";
53   - response += "rusage-utime: 0.000000\n";
54   - response += "rusage-stime: 0.028001\n";
55   - response += "uptime: 19109\n";
56   - response += "binlog-oldest-index: 0\n";
57   - response += "binlog-current-index: 0\n";
58   - response += "binlog-max-size: 10485760\n";
59   - response += "\r\n";
60   - conn.write(response);
61   - }
62   - });
63   -
64   - conn.on('end', function(){
65   - mock_server.close();
66   - })
67   -});
68   -mock_server.listen(port);
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == 'stats\r\n') {
  8 + var response = "";
  9 + response += "OK 813\r\n";
  10 + response += "---\n";
  11 + response += "current-jobs-urgent: 0\n";
  12 + response += "current-jobs-ready: 0\n";
  13 + response += "current-jobs-reserved: 0\n";
  14 + response += "current-jobs-delayed: 0\n";
  15 + response += "current-jobs-buried: 0\n";
  16 + response += "cmd-put: 10\n";
  17 + response += "cmd-peek: 2\n";
  18 + response += "cmd-peek-ready: 0\n";
  19 + response += "cmd-peek-delayed: 0\n";
  20 + response += "cmd-peek-buried: 1\n";
  21 + response += "cmd-reserve: 2\n";
  22 + response += "cmd-reserve-with-timeout: 0\n";
  23 + response += "cmd-delete: 10\n";
  24 + response += "cmd-release: 0\n";
  25 + response += "cmd-use: 2\n";
  26 + response += "cmd-watch: 2\n";
  27 + response += "cmd-ignore: 0\n";
  28 + response += "cmd-bury: 1\n";
  29 + response += "cmd-kick: 0\n";
  30 + response += "cmd-touch: 0\n";
  31 + response += "cmd-stats: 1\n";
  32 + response += "cmd-stats-job: 6\n";
  33 + response += "cmd-stats-tube: 0\n";
  34 + response += "cmd-list-tubes: 15\n";
  35 + response += "cmd-list-tube-used: 0\n";
  36 + response += "cmd-list-tubes-watched: 0\n";
  37 + response += "cmd-pause-tube: 0\n";
  38 + response += "job-timeouts: 0\n";
  39 + response += "total-jobs: 10\n";
  40 + response += "max-job-size: 65535\n";
  41 + response += "current-tubes: 1\n";
  42 + response += "current-connections: 1\n";
  43 + response += "current-producers: 0\n";
  44 + response += "current-workers: 0\n";
  45 + response += "current-waiting: 0\n";
  46 + response += "total-connections: 27\n";
  47 + response += "pid: 2759\n";
  48 + response += "version: 1.4.6\n";
  49 + response += "rusage-utime: 0.000000\n";
  50 + response += "rusage-stime: 0.028001\n";
  51 + response += "uptime: 19109\n";
  52 + response += "binlog-oldest-index: 0\n";
  53 + response += "binlog-current-index: 0\n";
  54 + response += "binlog-max-size: 10485760\n";
  55 + response += "\r\n";
  56 + conn.write(response);
  57 + }
  58 +}, true);
  59 +var client = helper.getClient();
69 60
70   -var client = bs.Client('127.0.0.1:' + port);
71 61
72 62 var success = false;
73 63 var error = false;
26 tests/test_stats_job.js
... ... @@ -1,25 +1,15 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing stats_job not existing');
5 2
6   -var port = 11333;
7   -
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == "stats-job 111111111\r\n") {
12   - conn.write('NOT_FOUND\r\n');
13   - }
14   - });
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
15 5
16   - conn.on('end', function() {
17   - mock_server.close();
18   - });
19   -});
20   -mock_server.listen(port);
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == "stats-job 111111111\r\n") {
  8 + conn.write('NOT_FOUND\r\n');
  9 + }
  10 +}, true);
  11 +var client = helper.getClient();
21 12
22   -var client = bs.Client('127.0.0.1:' + port);
23 13
24 14 var success = false;
25 15 var error = false;
62 tests/test_stats_tube.js
... ... @@ -1,42 +1,32 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing stats_tube');
5 2
6   -var port = 11333;
7   -
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == 'stats-tube default\r\n') {
12   - var response = "";
13   - response += "OK 251\r\n";
14   - response += "---\n";
15   - response += "name: default\n";
16   - response += "current-jobs-urgent: 0\n";
17   - response += "current-jobs-ready: 0\n";
18   - response += "current-jobs-reserved: 0\n";
19   - response += "current-jobs-delayed: 0\n";
20   - response += "current-jobs-buried: 0\n";
21   - response += "total-jobs: 8\n";
22   - response += "current-using: 1\n";
23   - response += "current-watching: 1\n";
24   - response += "current-waiting: 0\n";
25   - response += "cmd-pause-tube: 0\n";
26   - response += "pause: 0\n";
27   - response += "pause-time-left: 0\n";
28   - response += "\r\n";
29   - conn.write(response);
30   - }
31   - });
32   -
33   - conn.on('end', function(){
34   - mock_server.close();
35   - })
36   -});
37   -mock_server.listen(port);
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
  5 +
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == 'stats-tube default\r\n') {
  8 + var response = "";
  9 + response += "OK 251\r\n";
  10 + response += "---\n";
  11 + response += "name: default\n";
  12 + response += "current-jobs-urgent: 0\n";
  13 + response += "current-jobs-ready: 0\n";
  14 + response += "current-jobs-reserved: 0\n";
  15 + response += "current-jobs-delayed: 0\n";
  16 + response += "current-jobs-buried: 0\n";
  17 + response += "total-jobs: 8\n";
  18 + response += "current-using: 1\n";
  19 + response += "current-watching: 1\n";
  20 + response += "current-waiting: 0\n";
  21 + response += "cmd-pause-tube: 0\n";
  22 + response += "pause: 0\n";
  23 + response += "pause-time-left: 0\n";
  24 + response += "\r\n";
  25 + conn.write(response);
  26 + }
  27 +}, true);
  28 +var client = helper.getClient();
38 29
39   -var client = bs.Client('127.0.0.1:' + port);
40 30
41 31 var success = false;
42 32 var error = false;
25 tests/test_use.js
... ... @@ -1,22 +1,17 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing use');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
  5 +
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == "use default\r\n") {
  8 + conn.write('USING default\r\n');
  9 + this.close();
  10 + }
  11 +}, false);
  12 +var client = helper.getClient();
7 13
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == "use default\r\n") {
12   - conn.write('USING default\r\n');
13   - mock_server.close();
14   - }
15   - });
16   -});
17   -mock_server.listen(port);
18 14
19   -var client = bs.Client('127.0.0.1:' + port);
20 15 var success = false;
21 16 var error = false;
22 17
57 tests/test_utf8.js
... ... @@ -1,41 +1,32 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing utf8');
5 2
6   -var port = 11333;
7   -
8   -var net = require('net');
9 3 var cnt = 0;
10 4
11   -var mock_server = net.createServer(function(conn) {
12   - conn.on('data', function(data) {
13   -
14   -
15   - if(String(data).indexOf('put') > -1) {
16   - var input = data.toString().split('\r\n');
17   -
18   - assert.equal(input[1], "ééééé");
19   - assert.equal(Buffer.byteLength(input[1], 'utf8'), Buffer.byteLength("ééééé", 'utf8'));
20   -
21   - cnt += 1;
22   - conn.write("INSERTED "+cnt+"\r\n");
23   - }
24   -
25   - if(String(data) == "use default\r\n") {
26   - conn.write('USING default\r\n');
27   - mock_server.close();
28   - }
29   - });
30   -});
31   -mock_server.listen(port);
32   -
33   -var client = bs.Client('127.0.0.1:' + port);
  5 +var assert = require('assert');
  6 +var helper = require('./helper');
  7 +
  8 +helper.bind(function(conn, data) {
  9 + if(String(data).indexOf('put') > -1) {
  10 + var input = data.toString().split('\r\n');
  11 +
  12 + assert.equal(input[1], "ééééé");
  13 + assert.equal(Buffer.byteLength(input[1], 'utf8'), Buffer.byteLength("ééééé", 'utf8'));
  14 +
  15 + cnt += 1;
  16 + conn.write("INSERTED "+cnt+"\r\n");
  17 + }
  18 +
  19 + if(String(data) == "use default\r\n") {
  20 + conn.write('USING default\r\n');
  21 + this.close();
  22 + }
  23 +}, false);
  24 +var client = helper.getClient();
34 25
35 26 client.use('default').onSuccess(function(data) {
36   - client.put("ééééé", 100, 0).onSuccess(function(data) {
37   - assert.ok(!isNaN(data[0]));
38   - client.disconnect();
39   - });
  27 + client.put("ééééé", 100, 0).onSuccess(function(data) {
  28 + assert.ok(!isNaN(data[0]));
  29 + client.disconnect();
  30 + });
40 31 });
41 32
23 tests/test_watch.js
... ... @@ -1,22 +1,15 @@
1   -var assert = require('assert');
2   -var bs = require('../lib/beanstalk_client');
3   -
4 1 console.log('testing watch');
5 2
6   -var port = 11333;
  3 +var assert = require('assert');
  4 +var helper = require('./helper');
7 5
8   -var net = require('net');
9   -var mock_server = net.createServer(function(conn) {
10   - conn.on('data', function(data) {
11   - if(String(data) == "watch default\r\n") {
12   - conn.write('WATCHING 1\r\n');
13   - mock_server.close();
14   - }
15   - });
  6 +helper.bind(function(conn, data) {
  7 + if(String(data) == "watch default\r\n") {
  8 + conn.write('WATCHING 1\r\n');
  9 + this.close();
  10 + }
16 11 });
17   -mock_server.listen(port);
18   -
19   -var client = bs.Client('127.0.0.1:' + port);
  12 +var client = helper.getClient();
20 13
21 14 var success = false;
22 15 var error = false;

0 comments on commit 5576a7c

Please sign in to comment.
Something went wrong with that request. Please try again.