connection-manager.test.js
3.43 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
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Support = require('../../support');
const dialect = Support.getTestDialect();
if (dialect.match(/^mssql/)) {
describe('[MSSQL Specific] Query Queue', () => {
it('should work with handleDisconnects', () => {
const sequelize = Support.createSequelizeInstance({ pool: { min: 1, max: 1, idle: 5000 } });
const cm = sequelize.connectionManager;
let conn;
return sequelize
.sync()
.then(() => cm.getConnection())
.then(connection => {
// Save current connection
conn = connection;
// simulate a unexpected end
// connection removed from pool by MSSQL Conn Manager
conn.unwrap().emit('error', {code: 'ECONNRESET'});
})
.then(() => cm.getConnection())
.then(connection => {
expect(conn).to.not.be.equal(connection);
expect(cm.validate(conn)).to.not.be.ok;
return cm.releaseConnection(connection);
});
});
it('should handle double disconnect', () => {
const sequelize = Support.createSequelizeInstance({ pool: { min: 1, max: 1, idle: 5000 } });
const cm = sequelize.connectionManager;
let count = 0;
let conn = null;
return sequelize
.sync()
.then(() => cm.getConnection())
.then(connection => {
conn = connection;
const unwrapConn = conn.unwrap();
unwrapConn.on('end', () => {
count++;
});
return cm.disconnect(conn);
})
.then(() => cm.disconnect(conn))
.then(() => {
expect(count).to.be.eql(1);
});
});
it('should not throw when non pooled connection is unexpectedly closed', () => {
const sequelize = Support.createSequelizeInstance({ pool: { min: 1, max: 1, idle: 5000 } });
const cm = sequelize.connectionManager;
let conn;
return sequelize
.sync()
.then(() => cm.getConnection())
.then(connection => {
conn = connection;
// remove from pool
return cm.pool.destroy(connection);
})
.then(() => {
// unexpected disconnect
const unwrapConn = conn.unwrap();
unwrapConn.emit('error', {
code: 'ESOCKET'
});
});
});
describe('Errors', () => {
it('ECONNREFUSED', () => {
const sequelize = Support.createSequelizeInstance({ host: '127.0.0.1', port: 34237 });
return expect(sequelize.connectionManager.getConnection()).to.have.been.rejectedWith(sequelize.ConnectionRefusedError);
});
it('ENOTFOUND', () => {
const sequelize = Support.createSequelizeInstance({ host: 'http://wowow.example.com' });
return expect(sequelize.connectionManager.getConnection()).to.have.been.rejectedWith(sequelize.HostNotFoundError);
});
it('EHOSTUNREACH', () => {
const sequelize = Support.createSequelizeInstance({ host: '255.255.255.255' });
return expect(sequelize.connectionManager.getConnection()).to.have.been.rejectedWith(sequelize.HostNotReachableError);
});
it('ER_ACCESS_DENIED_ERROR | ELOGIN', () => {
const sequelize = new Support.Sequelize('localhost', 'was', 'ddsd', Support.sequelize.options);
return expect(sequelize.connectionManager.getConnection()).to.have.been.rejectedWith(sequelize.AccessDeniedError);
});
});
});
}