-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
client.test.js
140 lines (127 loc) · 4.9 KB
/
client.test.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
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
130
131
132
133
134
135
136
137
138
139
140
'use strict';
const expect = require('chai').expect;
const mock = require('../../tools/mock');
const { ReplSetFixture } = require('../../tools/common');
const test = {};
describe('Sessions - client/unit', function () {
describe('Client', function () {
afterEach(() => mock.cleanup());
beforeEach(() => {
return mock.createServer().then(server => {
test.server = server;
});
});
it('should not throw a synchronous exception if sessions are not supported', {
metadata: { requires: { topology: 'single' } },
test() {
test.server.setMessageHandler(request => {
var doc = request.document;
if (doc.ismaster || doc.hello) {
request.reply(Object.assign({}, mock.DEFAULT_ISMASTER_36));
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
const client = this.configuration.newClient(`mongodb://${test.server.uri()}/test`);
return client.connect().then(() => {
expect(() => client.startSession()).to.not.throw(
'Current topology does not support sessions'
);
return client.close();
});
}
});
it('should throw an exception if sessions are not supported on some servers', {
metadata: { requires: { topology: 'single' } },
test() {
const replicaSetMock = new ReplSetFixture();
let testClient;
return replicaSetMock
.setup({ doNotInitHandlers: true })
.then(() => {
replicaSetMock.firstSecondaryServer.setMessageHandler(request => {
var doc = request.document;
if (doc.ismaster || doc.hello) {
const ismaster = replicaSetMock.firstSecondaryStates[0];
ismaster.logicalSessionTimeoutMinutes = 20;
request.reply(ismaster);
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
replicaSetMock.secondSecondaryServer.setMessageHandler(request => {
var doc = request.document;
if (doc.ismaster || doc.hello) {
const ismaster = replicaSetMock.secondSecondaryStates[0];
ismaster.logicalSessionTimeoutMinutes = 10;
request.reply(ismaster);
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
replicaSetMock.arbiterServer.setMessageHandler(request => {
var doc = request.document;
if (doc.ismaster || doc.hello) {
const ismaster = replicaSetMock.arbiterStates[0];
ismaster.logicalSessionTimeoutMinutes = 30;
request.reply(ismaster);
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
replicaSetMock.primaryServer.setMessageHandler(request => {
var doc = request.document;
if (doc.ismaster || doc.hello) {
const ismaster = replicaSetMock.primaryStates[0];
ismaster.logicalSessionTimeoutMinutes = null;
request.reply(ismaster);
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
return replicaSetMock.uri();
})
.then(uri => {
testClient = this.configuration.newClient(uri);
return testClient.connect();
})
.then(client => {
const session = client.startSession();
return client.db().collection('t').insertOne({ a: 1 }, { session });
})
.then(() => {
expect.fail('Expected an error to be thrown about not supporting sessions');
})
.catch(error => {
expect(error.message).to.equal('Current topology does not support sessions');
})
.finally(() => (testClient ? testClient.close() : null));
}
});
it('should return a client session when requested if the topology supports it', {
metadata: { requires: { topology: 'single' } },
test: function (done) {
test.server.setMessageHandler(request => {
var doc = request.document;
if (doc.ismaster || doc.hello) {
request.reply(
Object.assign({}, mock.DEFAULT_ISMASTER, {
logicalSessionTimeoutMinutes: 10
})
);
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
const client = this.configuration.newClient(`mongodb://${test.server.uri()}/test`);
client.connect(function (err, client) {
expect(err).to.not.exist;
let session = client.startSession();
expect(session).to.exist;
session.endSession({ skipCommand: true });
client.close(done);
});
}
});
});
});