/
bootstrap.js
58 lines (52 loc) · 1.58 KB
/
bootstrap.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
/**
* Bootstrap
* (sails.config.bootstrap)
*
* Perform test operations and match update events against rules
*/
var OPERATION_INTERVAL = 200;
module.exports.bootstrap = function(cb) {
// Complete sails lift immediately
cb();
var installed = [];
var opCount = 0;
var liveAll = Foo.liveFind({});
liveAll.on('update', function(diff, data) {
// Conditional to match valid diff/data states
if(
// Initial state: empty diff with empty added array
(data.length === 0 && diff.added.length === 0)
// First stage: adding sample items
|| (diff.added && diff.added[0].message * 1 === diff.added[0].id)
// Second stage: One item removed
|| (opCount === 7 && data.length === 5)
) {
process.stdout.write('.');
} else {
console.log('failed!'.red, diff);
process.exitCode = 1;
process.kill(process.pid, 'SIGINT');
}
opCount++;
});
// Perform operations at regular interval
var operationInterval = setInterval(function() {
if(installed.length < 6) {
// First stage: insert sample data
Foo.create({ message: installed.length + 1 }).exec(function(error, result) {
if(error) throw error;
installed.push(result);
});
} else if(opCount === 7) {
// Second stage: remove an item
Foo.destroy({ id: 3 }).exec(function(error) {
if(error) throw error;
});
} else {
// Third stage: still running? passed
console.log('passed'.green);
clearInterval(operationInterval);
process.kill(process.pid, 'SIGINT');
}
}, OPERATION_INTERVAL);
};