cls.test.js
4.21 KB
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
"use strict";
/* jshint camelcase: false */
var chai = require('chai')
, sinon = require('sinon')
, expect = chai.expect
, Support = require(__dirname + '/support')
, Sequelize = Support.Sequelize
, Promise = Sequelize.Promise
, cls = require('continuation-local-storage');
chai.config.includeStack = true;
describe(Support.getTestDialectTeaser("Continuation local storage"), function () {
before(function () {
return Support.prepareTransactionTest(Support.createSequelizeInstance({
namespace: cls.createNamespace('sequelize')
})).bind(this).then(function (sequelize) {
this.sequelize = sequelize;
});
});
beforeEach(function () {
this.ns = cls.getNamespace('sequelize');
this.User = this.sequelize.define('user', {
name: Sequelize.STRING
});
return this.sequelize.sync({ force: true });
});
var autoCallback = function autoCallback(sequelize, cb) {
return sequelize.transaction(cb);
};
var thenCallback = function thenCallback(sequelize, cb) {
return sequelize.transaction().then(function (t) {
cb().then(function () {
t.commit();
});
});
};
[autoCallback, thenCallback].forEach(function (cb) {
describe(cb.name, function () {
describe('context', function () {
it('supports several concurrent transactions', function () {
var t1id, t2id, self = this;
return Promise.join(
cb(this.sequelize, function () {
t1id = self.ns.get('transaction').id;
return Promise.resolve();
}),
cb(this.sequelize, function () {
t2id = self.ns.get('transaction').id;
return Promise.resolve();
}),
function () {
expect(t1id).not.to.equal(t2id);
}
);
});
it('supports nested promise chains', function () {
var self = this;
return cb(this.sequelize, function () {
var tid = self.ns.get('transaction').id;
return self.User.findAll().then(function () {
expect(self.ns.get('transaction').id).to.be.ok;
expect(self.ns.get('transaction').id).to.equal(tid);
});
});
});
it('does not leak variables to the outer scope', function () {
// This is a little tricky. We want to check the values in the outer scope, when the transaction has been successfully set up, but before it has been comitted.
// We can't just call another function from inside that transaction, since that would transfer the context to that function - exactly what we are trying to prevent;
var self = this
, transactionSetup = false
, transactionEnded = false;
cb(this.sequelize, function () {
transactionSetup = true;
return Promise.delay(500).then(function () {
expect(self.ns.get('transaction')).to.be.ok;
transactionEnded = true;
});
});
// Wait for 400 ms - should be enough time to get everything set up
return Promise.delay(400).bind(this).then(function () {
expect(transactionSetup).to.be.ok;
expect(transactionEnded).not.to.be.ok;
expect(this.ns.get('transaction')).not.to.be.ok;
});
});
it('does not leak variables to the following promise chain', function () {
return cb(this.sequelize, function () {
return Promise.resolve();
}).bind(this).then(function () {
expect(this.ns.get('transaction')).not.to.be.ok;
});
});
});
describe('sequelize.query integration', function () {
it('automagically uses the transaction in all calls', function () {
var self = this;
return cb(this.sequelize, function () {
return self.User.create({ name: 'bob' }).then(function () {
return Promise.all([
expect(self.User.findAll({}, { transaction: null })).to.eventually.have.length(0),
expect(self.User.findAll({})).to.eventually.have.length(1)
]);
});
});
});
});
});
});
});