-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.js
93 lines (86 loc) · 2.55 KB
/
tests.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
/*eslint-env mocha */
/**
* These tests can only succeed if you have MongoDb running.
* I haven't found a way yet, how to test this plugin entirely by itself.
*/
// Modules
var expect = require('expect.js');
var faker = require('faker');
var async = require('async');
var _ = require('lodash');
// Test subject
var MySchema = require('./mySchema');
describe('mongoose-lock-release', function () {
var lockTime = 100000;
var mySchema;
before(function (done) {
new MySchema({
order: {
id: faker.random.uuid()
},
user: {
email: faker.internet.email()
}
}).save(function(err, _mySchema) {
mySchema = _mySchema;
done(err);
});
});
it('should lock it if it is not locked yet', function (done) {
async.waterfall([
function lock(cb) {
mySchema.lock(lockTime, cb);
},
function check(mySchema, cb) {
expect(mySchema).to.be.ok();
expect(mySchema.locked > new Date()).to.be(true);
cb();
}
], done);
});
it('should refuse to lock if it is already locked', function (done) {
mySchema.lock(lockTime, function(err, mySchema) {
expect(err).to.not.be.ok();
expect(mySchema).to.not.be.ok();
done();
});
});
it('should release the lock', function (done) {
mySchema.release(function(err, _mySchema) {
expect(err).to.not.be.ok();
expect(_mySchema).to.be.ok();
mySchema = _mySchema;
done();
});
});
it('should fail one lock if two are set concurrently', function (done) {
async.parallel([
function lock(cb) {
mySchema.lock(lockTime, cb);
},
function lock(cb) {
mySchema.lock(lockTime, cb);
},
], function (err, mySchemas) {
if (err) {
return done(err);
}
var countLocks = _.filter(mySchemas, function(i) { return i && i.locked; });
expect(countLocks.length).to.be(1);
done();
});
});
it('should be lockable if lock has expired', function (done) {
async.series([
release,
lock,
lock, //lock it again
], done);
function release(cb) {
mySchema.release(cb);
}
function lock(cb) {
mySchema.lock(1, cb);
}
});
});