replication.test.js
2.16 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
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Support = require(__dirname + '/support');
const DataTypes = require(__dirname + '/../../lib/data-types');
const dialect = Support.getTestDialect();
const sinon = require('sinon');
describe(Support.getTestDialectTeaser('Replication'), function() {
if (dialect === 'sqlite') return;
let sandbox;
let readSpy, writeSpy;
beforeEach(() => {
sandbox = sinon.sandbox.create();
this.sequelize = Support.getSequelizeInstance(null, null, null, {
replication: {
write: Support.getConnectionOptions(),
read: [Support.getConnectionOptions()]
}
});
expect(this.sequelize.connectionManager.pool.write).to.be.ok;
expect(this.sequelize.connectionManager.pool.read).to.be.ok;
this.User = this.sequelize.define('User', {
firstName: {
type: DataTypes.STRING,
field: 'first_name'
}
});
return this.User.sync({force: true})
.then(() => {
readSpy = sandbox.spy(this.sequelize.connectionManager.pool.read, 'acquire');
writeSpy = sandbox.spy(this.sequelize.connectionManager.pool.write, 'acquire');
});
});
afterEach(() => {
sandbox.restore();
});
function expectReadCalls() {
chai.expect(readSpy.callCount).least(1);
chai.expect(writeSpy.notCalled).eql(true);
}
function expectWriteCalls() {
chai.expect(writeSpy.callCount).least(1);
chai.expect(readSpy.notCalled).eql(true);
}
it('should be able to make a write', () => {
return this.User.create({
firstName: Math.random().toString()
})
.then(expectWriteCalls);
});
it('should be able to make a read', () => {
return this.User.findAll()
.then(expectReadCalls);
});
it('should run read-only transactions on the replica', () => {
return this.sequelize.transaction({readOnly: true}, transaction => {
return this.User.findAll({transaction});
})
.then(expectReadCalls);
});
it('should run non-read-only transactions on the primary', () => {
return this.sequelize.transaction(transaction => {
return this.User.findAll({transaction});
})
.then(expectWriteCalls);
});
});