forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard.test.js
301 lines (253 loc) · 8.89 KB
/
shard.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var start = require('./common')
, assert = require('assert')
, random = require('../lib/utils').random
, mongoose = start.mongoose
, Mongoose = mongoose.Mongoose
, Schema = mongoose.Schema;
var uri = process.env.MONGOOSE_SHARD_TEST_URI;
if (!uri) {
console.log('\033[31m', '\n', 'You\'re not testing shards!'
, '\n', 'Please set the MONGOOSE_SHARD_TEST_URI env variable.', '\n'
, 'e.g: `mongodb://localhost:27017/database', '\n'
, 'Sharding must already be enabled on your database'
, '\033[39m');
// let expresso shut down this test
exports.r = function expressoHack(){}
return;
}
var schema = new Schema({
name: String
, age: Number
, likes: [String]
}, { shardkey: { name: 1, age: 1 }});
// to keep mongodb happy when sharding the collection
// we add a matching index
schema.index({ name: 1, age: 1 });
var collection = 'shardperson_' + random();
mongoose.model('ShardPerson', schema, collection);
var version;
var greaterThan20x;
var db;
describe('shard', function(){
before(function (done) {
db = start({ uri: uri });
db.on('error', function (err) {
if (/failed to connect/.test(err)) {
err.message = 'Shard test error: '
+ err.message
+ '\n'
+ ' Are you sure there is a db running at '
+ uri + ' ?'
+ '\n'
}
return done(err);
});
db.on('open', function () {
// set up a sharded test collection
var P = db.model('ShardPerson', collection);
// an existing index on shard key is required before sharding
P.on('index', function () {
// enable sharding on our collection
var cmd = {};
cmd.shardcollection = db.name + '.' + collection;
cmd.key = P.schema.options.shardkey;
P.db.db.executeDbAdminCommand(cmd,function (err, res) {
assert.ifError(err);
if (!(res && res.documents && res.documents[0] && res.documents[0].ok)) {
err = new Error('could not shard test collection '
+ collection + '\n'
+ res.documents[0].errmsg +'\n'
+ 'Make sure to use a different database than what '
+ 'is used for the MULTI_MONGOS_TEST' );
return done(err);
}
db.db.admin(function (err, admin) {
assert.ifError(err);
admin.serverStatus(function (err, info) {
db.close();
assert.ifError(err);
version = info.version.split('.').map(function(n){return parseInt(n, 10) });
greaterThan20x = 2 < version[0] || 2==version[0] && 0<version[0];
done();
});
});
});
});
});
});
it('can read and write to a shard', function (done) {
var db = start({ uri: uri })
var P = db.model('ShardPerson', collection);
P.create({ name: 'ryu', age: 25, likes: ['street fighting']}, function (err, ryu) {
assert.ifError(err);
P.findById(ryu._id, function (err, doc) {
db.close();
assert.ifError(err);
assert.equal(doc.id,ryu.id);
done();
});
});
})
it('save() and remove() works with shard keys transparently', function (done) {
var db = start({ uri: uri })
var P = db.model('ShardPerson', collection);
var zangief = new P({ name: 'Zangief', age: 33 });
zangief.save(function (err) {
assert.ifError(err);
assert.equal(zangief.$__.shardval.name, 'Zangief');
assert.equal(zangief.$__.shardval.age, 33);
P.findById(zangief._id, function (err, zang) {
assert.ifError(err);
assert.equal(zang.$__.shardval.name, 'Zangief');
assert.equal(zang.$__.shardval.age, 33);
zang.likes = ['spinning', 'laughing'];
zang.save(function (err) {
assert.ifError(err);
assert.equal(zang.$__.shardval.name, 'Zangief');
assert.equal(zang.$__.shardval.age, 33);
zang.likes.addToSet('winning');
zang.save(function (err) {
assert.ifError(err);
assert.equal(zang.$__.shardval.name, 'Zangief');
assert.equal(zang.$__.shardval.age, 33);
zang.remove(function (err) {
db.close();
assert.ifError(err);
done();
});
});
});
});
});
})
it('inserting to a sharded collection without the full shard key fails', function (done) {
var db = start({ uri: uri })
var P = db.model('ShardPerson', collection);
var pending = 6;
P.create({ name: 'ryu', likes: ['street fighting']}, function (err, ryu) {
assert.ok(err);
assert.ok(/tried to insert object with no valid shard key/.test(err.message));
if (!--pending) {
db.close();
done();
}
});
P.create({ likes: ['street fighting']}, function (err, ryu) { assert.ok(err);
assert.ok(err);
assert.ok(/tried to insert object with no valid shard key/.test(err.message));
if (!--pending) {
db.close();
done();
}
});
P.create({ name: 'ryu' }, function (err, ryu) { assert.ok(err);
assert.ok(err);
assert.ok(/tried to insert object with no valid shard key/.test(err.message));
if (!--pending) {
db.close();
done();
}
});
P.create({ age: 49 }, function (err, ryu) { assert.ok(err);
assert.ok(err);
assert.ok(/tried to insert object with no valid shard key/.test(err.message));
if (!--pending) {
db.close();
done();
}
});
P.create({ likes: ['street fighting'], age: 8 }, function (err, ryu) {
assert.ok(err);
assert.ok(/tried to insert object with no valid shard key/.test(err.message))
if (!--pending) {
db.close();
done();
}
});
var p = new P;
p.save(function (err) {
assert.ok(err);
assert.ok(/tried to insert object with no valid shard key/.test(err.message));
if (!--pending) {
db.close();
done();
}
});
});
it('updating a sharded collection without the full shard key fails', function (done) {
var db = start({ uri: uri })
var P = db.model('ShardPerson', collection);
P.create({ name: 'ken', age: 27 }, function (err, ken) {
assert.ifError(err);
P.update({ name: 'ken' }, { likes: ['kicking', 'punching'] }, function (err) {
assert.ok(/shard key/.test(err.message));
P.update({ _id: ken._id, name: 'ken' }, { likes: ['kicking', 'punching'] }, function (err) {
// mongo 2.0.x returns: can't do non-multi update with query that doesn't have a valid shard key
if (greaterThan20x) {
assert.ok(!err, err);
} else {
assert.ok(/shard key/.test(err.message));
}
P.update({ _id: ken._id, age: 27 }, { likes: ['kicking', 'punching'] }, function (err) {
// mongo 2.0.x returns: can't do non-multi update with query that doesn't have a valid shard key
if (greaterThan20x) {
assert.ok(!err, err);
} else {
assert.ok(/shard key/.test(err.message));
}
P.update({ age: 27 }, { likes: ['kicking', 'punching'] }, function (err) {
db.close();
assert.ok(err);
done();
});
});
});
});
});
})
it('updating shard key values fails', function (done) {
var db = start({ uri: uri })
var P = db.model('ShardPerson', collection);
P.create({ name: 'chun li', age: 19, likes: ['street fighting']}, function (err, chunli) {
assert.ifError(err);
assert.equal(chunli.$__.shardval.name, 'chun li');
assert.equal(chunli.$__.shardval.age, 19);
chunli.age = 20;
chunli.save(function (err) {
assert.ok(/^Can't modify shard key's value/.test(err.message));
assert.equal(chunli.$__.shardval.name, 'chun li');
assert.equal(chunli.$__.shardval.age, 19);
P.findById(chunli._id, function (err, chunli) {
assert.ifError(err);
assert.equal(chunli.$__.shardval.name, 'chun li');
assert.equal(chunli.$__.shardval.age, 19);
chunli.name='chuuuun liiiii';
chunli.save(function (err) {
db.close();
assert.ok(/^Can't modify shard key's value/.test(err.message));
done();
});
});
});
});
});
it('allows null shard key values', function (done) {
var db = start({ uri: uri })
var P = db.model('ShardPerson', collection);
P.create({ name: null, age: 27 }, function (err, ken) {
assert.ifError(err);
P.findById(ken, function (err, ken) {
assert.ifError(err);
done();
});
});
});
after(function (done) {
var db = start({ uri: uri })
var P = db.model('ShardPerson', collection);
P.collection.drop(function (err) {
db.close();
done();
});
});
})