-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
common.js
132 lines (116 loc) · 3.58 KB
/
common.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
'use strict';
const mock = require('./mongodb-mock/index');
const BSON = require('../mongodb');
const { LEGACY_HELLO_COMMAND } = require('../mongodb');
const { isHello } = require('../mongodb');
class ReplSetFixture {
constructor() {
this.electionIds = [new BSON.ObjectId(), new BSON.ObjectId()];
}
uri(dbName) {
return `mongodb://${this.primaryServer.uri()},${this.firstSecondaryServer.uri()},${this.secondSecondaryServer.uri()}/${
dbName || 'test'
}?replicaSet=rs`;
}
setup(options) {
options = options || {};
const hello = options[LEGACY_HELLO_COMMAND] ? options[LEGACY_HELLO_COMMAND] : mock.HELLO;
return Promise.all([
mock.createServer(),
mock.createServer(),
mock.createServer(),
mock.createServer()
]).then(servers => {
this.servers = servers;
this.primaryServer = servers[0];
this.firstSecondaryServer = servers[1];
this.secondSecondaryServer = servers[2];
this.arbiterServer = servers[3];
this.defaultFields = Object.assign({}, hello, {
__nodejs_mock_server__: true,
setName: 'rs',
setVersion: 1,
electionId: this.electionIds[0],
hosts: this.servers.map(server => server.uri()),
arbiters: [this.arbiterServer.uri()]
});
if (!options.doNotInitStates) this.defineReplSetStates();
if (!options.doNotInitHandlers) this.configureMessageHandlers();
});
}
defineReplSetStates() {
this.primaryStates = [
Object.assign({}, this.defaultFields, {
[LEGACY_HELLO_COMMAND]: true,
secondary: false,
me: this.primaryServer.uri(),
primary: this.primaryServer.uri(),
tags: { loc: 'ny' }
})
];
this.firstSecondaryStates = [
Object.assign({}, this.defaultFields, {
[LEGACY_HELLO_COMMAND]: false,
secondary: true,
me: this.firstSecondaryServer.uri(),
primary: this.primaryServer.uri(),
tags: { loc: 'sf' }
})
];
this.secondSecondaryStates = [
Object.assign({}, this.defaultFields, {
[LEGACY_HELLO_COMMAND]: false,
secondary: true,
me: this.secondSecondaryServer.uri(),
primary: this.primaryServer.uri(),
tags: { loc: 'la' }
})
];
this.arbiterStates = [
Object.assign({}, this.defaultFields, {
[LEGACY_HELLO_COMMAND]: false,
secondary: false,
arbiterOnly: true,
me: this.arbiterServer.uri(),
primary: this.primaryServer.uri()
})
];
}
configureMessageHandlers() {
this.primaryServer.setMessageHandler(request => {
var doc = request.document;
if (isHello(doc)) {
request.reply(this.primaryStates[0]);
}
});
this.firstSecondaryServer.setMessageHandler(request => {
var doc = request.document;
if (isHello(doc)) {
request.reply(this.firstSecondaryStates[0]);
}
});
this.arbiterServer.setMessageHandler(request => {
var doc = request.document;
if (isHello(doc)) {
request.reply(this.arbiterStates[0]);
}
});
}
}
/**
* Creates a cluster time for use in unit testing cluster time gossiping and
* causal consistency.
*
* @param {number} time the logical time
* @returns a cluster time according to the driver sessions specification
*/
function genClusterTime(time) {
return {
clusterTime: new BSON.Timestamp(BSON.Long.fromNumber(time, true)),
signature: { hash: new BSON.Binary(Buffer.from('test', 'utf8')), keyId: new BSON.Long(1) }
};
}
module.exports = {
ReplSetFixture: ReplSetFixture,
genClusterTime: genClusterTime
};