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
|
load("jstests/replsets/rslib.js");
doTest = function(signal) {
// Test basic replica set functionality.
// -- Replication
// -- Failover
// Replica set testing API
// Create a new replica set test. Specify set name and the number of nodes you want.
var replTest = new ReplSetTest({name: 'testSet', nodes: 3});
// call startSet() to start each mongod in the replica set
// this returns a list of nodes
var nodes = replTest.startSet();
// Call initiate() to send the replSetInitiate command
// This will wait for initiation
replTest.initiate();
// Call getPrimary to return a reference to the node that's been
// elected primary.
var primary = replTest.getPrimary();
// save some records
var len = 100;
for (var i = 0; i < len; ++i) {
primary.getDB("foo").foo.save({a: i});
}
waitForAllMembers(primary.getDB("foo"));
// This method will check the oplogs of the primary
// and secondaries in the set and wait until the change has replicated.
replTest.awaitReplication();
secondaries = replTest.getSecondaries();
assert(secondaries.length == 2, "Expected 2 secondaries but length was " + secondaries.length);
secondaries.forEach(function(secondary) {
// try to read from secondary
secondary.setSecondaryOk();
var count = secondary.getDB("foo").foo.find().itcount();
printjson(count);
assert.eq(len, count, "secondary count wrong: " + secondary);
print("Doing a findOne to verify we can get a row");
var one = secondary.getDB("foo").foo.findOne();
printjson(one);
print("Calling inline mr() with secondaryOk=true, must succeed");
secondary.setSecondaryOk();
map = function() {
emit(this.a, 1);
};
reduce = function(key, vals) {
var sum = 0;
for (var i = 0; i < vals.length; ++i) {
sum += vals[i];
}
return sum;
};
secondary.getDB("foo").foo.mapReduce(map, reduce, {out: {"inline": 1}});
print("Calling mr() to collection with secondaryOk=true, must fail");
try {
secondary.getDB("foo").foo.mapReduce(map, reduce, "output");
assert(false, "mapReduce() to collection succeeded on secondary");
} catch (e) {
print("Received exception: " + e);
}
print("Calling inline mr() with secondaryOk=false, must fail");
secondary.setSecondaryOk(false);
try {
secondary.getDB("foo").foo.mapReduce(map, reduce, {out: {"inline": 1}});
assert(false, "mapReduce() succeeded on secondary with secondaryOk=false");
} catch (e) {
print("Received exception: " + e);
}
print("Calling mr() to collection with secondaryOk=false, must fail");
try {
secondary.getDB("foo").foo.mapReduce(map, reduce, "output");
assert(false,
"mapReduce() to collection succeeded on secondary with secondaryOk=false");
} catch (e) {
print("Received exception: " + e);
}
});
// Shut down the set and finish the test.
replTest.stopSet(signal);
};
doTest(15);
print("SUCCESS");
|