forked from cyx/redic.js
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
151 lines (126 loc) · 3.19 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
const test = require('tape-promise')(require('tape'));
const Disq = require('./index');
const mock = require('./mock');
const NODES = [ '127.0.0.1:7711', '127.0.0.1:7712', '127.0.0.1:7713' ];
const CYCLE = 5;
const OPTIONS = { cycle: CYCLE };
const disque = new Disq({ nodes: NODES });
test('ping', function(t) {
return disque.call('ping')
.then(function(reply) {
t.equal(reply, 'PONG');
});
});
test('info', function(t) {
return disque.info()
.then(function(reply) {
t.equal(reply.loading, '0');
});
});
test('unknown command', function(t) {
return disque.call('foo')
.then(t.fail)
.catch(function(error) {
t.assert(error, 'throws');
});
});
test('addjob', function(t) {
return disque.addjob('q1', 'j1')
.then(function(reply) {
t.assert(reply.startsWith('D-'));
});
});
test('getjob', function(t) {
return disque.addjob('q1', 'j1')
.then(function() {
return disque.getjob([ 'q1' ]);
})
.then(function(jobs) {
t.equal(jobs.length, 1);
t.equal(jobs[0].queue, 'q1');
t.assert(jobs[0].id.startsWith('D-'));
t.equal(jobs[0].body, 'j1');
});
});
test('addjob with options', function(t) {
let args;
const disque = new Disq({ nodes: [ '127.0.0.1:7714' ] });
const server = mock({
addjob() {
args = Array.prototype.slice.call(arguments);
return 'D-123';
}
}).listen(7714);
return disque.addjob('q1', 'j1', { timeout: 1000, ttl: 60, async: true })
.then(function() {
t.deepEqual(args, [ 'q1', 'j1', '1000', 'ttl', '60', 'async' ]);
})
.then(function() {
disque.end();
server.close();
});
});
test('getjob with options', function(t) {
let args;
const disque = new Disq({ nodes: [ '127.0.0.1:7714' ] });
const server = mock({
getjob() {
args = Array.prototype.slice.call(arguments);
return [[ 'q1', 'D-123', 'j1' ]];
}
}).listen(7714);
return disque.getjob('q1', { count: 1, nohang: true })
.then(function() {
t.deepEqual(args, [ 'count', '1', 'nohang', 'from', 'q1' ]);
})
.then(function() {
disque.end();
server.close();
});
});
test('authentication', function(t) {
let authenticated = false;
const disque = new Disq({ nodes: [ '127.0.0.1:7714' ], auth: 's3cr3t' });
const server = mock({
ping() {
if (authenticated)
return 'LEGIT';
else
return 'MEH';
},
auth(password) {
authenticated = (password === 's3cr3t');
return 'OK';
}
}).listen(7714);
return disque.call('ping')
.then(function(reply) {
t.equal(reply, 'LEGIT');
})
.then(function() {
disque.end();
server.close();
});
});
test('reconfigure with Promise', function(t) {
let times = 0;
const disque = new Disq(function() {
const port = 7711 + times;
times++;
return Promise.resolve({ nodes: [ `127.0.0.1:${port}` ] });
});
return disque.info()
.then(function(info) {
t.equal(info.tcp_port, '7711');
disque.end();
return disque.info();
})
.then(function(info) {
t.equal(info.tcp_port, '7712');
disque.end();
});
});
test.onFinish(function() {
disque.end();
});