-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.spec.js
130 lines (115 loc) · 3.76 KB
/
cli.spec.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
/* eslint-env mocha */
'use strict';
var Promise = require('bluebird');
var redis = require('redis');
var childProcess = Promise.promisifyAll(require('child_process'), {
multiArgs: true
});
var assert = require('chai').assert;
var pathUtil = require('path');
var fantastiq = require('..');
var REDIS_HOST = process.env.REDIS_HOST || '127.0.0.1';
var command = pathUtil.resolve(__dirname, '../bin/fantastiq.js');
describe('cli', function () {
this.timeout(10000);
var client = null;
var queue = null;
before(() => {
client = redis.createClient({
host: REDIS_HOST
});
queue = fantastiq.client(client);
});
after(function (done) {
queue.quit().then(function () {
return client.quit(done);
});
});
it('should add a job', function () {
return childProcess.execAsync(`${command} -r redis://${REDIS_HOST}:6379 add -j job-1`)
.spread(function (stdout) {
var id = stdout;
return queue.get(id);
})
.then(function (job) {
assert.ok(job);
assert.propertyVal(job, 'data', 'job-1');
assert.propertyVal(job, 'priority', 0);
});
});
it('should add a json job', function () {
return childProcess.execAsync(`${command} -r redis://${REDIS_HOST}:6379 add -j '{"hello": "world"}'`)
.spread(function (stdout) {
var id = stdout;
return queue.get(id);
})
.then(function (job) {
assert.ok(job);
assert.deepEqual(job.data, { hello: 'world' });
});
});
it('should add multiple jobs', function () {
return childProcess.execAsync(`${command} -r redis://${REDIS_HOST}:6379 add -j job-2 -j='-3' -j '{"job": 4}'`)
.spread(function (stdout) {
var ids = stdout.split(/\s/);
return queue.getN(ids);
})
.spread(function (job1, job2, job3) {
assert.ok(job1);
assert.ok(job2);
assert.ok(job3);
assert.propertyVal(job1, 'data', 'job-2');
assert.propertyVal(job2, 'data', -3);
assert.deepEqual(job3.data, { job: 4 });
});
});
it('should add jobs from stdin', function () {
return childProcess.execAsync(`printf "job-5\\n{\\"job\\": 6}\\njob-7" | ${command} -r redis://${REDIS_HOST}:6379 add`)
.spread(function (stdout) {
var ids = stdout.split(/\s/);
return queue.getN(ids);
})
.spread(function (job1, job2, job3) {
assert.ok(job1);
assert.ok(job2);
assert.ok(job3);
assert.propertyVal(job1, 'data', 'job-5');
assert.deepEqual(job2.data, { job: 6 });
assert.propertyVal(job3, 'data', 'job-7');
});
});
it('should use batch size', function () {
return childProcess.execAsync(`printf "a\\nb\\nc\\nd\\ne" | ${command} -r redis://${REDIS_HOST}:6379 add -b 2`)
.spread(function (stdout) {
var ids = stdout.split(/\s/);
return queue.getN(ids);
})
.then(function (jobs) {
assert.deepEqual(jobs.map(function (job) {
return job.data;
}), ['a', 'b', 'c', 'd', 'e']);
});
});
it('should add a job with priority', function () {
return childProcess.execAsync(`${command} -r redis://${REDIS_HOST}:6379 add -j job-8 -p 15`)
.spread(function (stdout) {
var id = stdout;
return queue.get(id);
})
.then(function (job) {
assert.ok(job);
assert.propertyVal(job, 'data', 'job-8');
assert.propertyVal(job, 'priority', 15);
});
});
it('should get a job', function () {
return queue.add('the-job')
.then(function (id) {
return childProcess.execAsync(`${command} -r redis://${REDIS_HOST}:6379 get ${id}`);
})
.spread(function (stdout) {
var job = JSON.parse(stdout);
assert.propertyVal(job, 'data', 'the-job');
});
});
});