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
|
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 getMaster to return a reference to the node that's been
// elected master.
var master = replTest.getMaster();
// save some records
var len = 100
for (var i = 0; i < len; ++i) {
master.getDB("foo").foo.save({a: i});
}
waitForAllMembers(master.getDB("foo"));
// This method will check the oplogs of the master
// and slaves in the set and wait until the change has replicated.
replTest.awaitReplication();
slaves = replTest.liveNodes.slaves;
assert( slaves.length == 2, "Expected 2 slaves but length was " + slaves.length );
slaves.forEach(function(slave) {
// try to read from slave
slave.slaveOk = true;
var count = slave.getDB("foo").foo.count();
printjson( count );
assert.eq( len , count , "slave count wrong: " + slave );
print("Doing a findOne to verify we can get a row");
var one = slave.getDB("foo").foo.findOne();
printjson(one);
// stats = slave.getDB("foo").adminCommand({replSetGetStatus:1});
// printjson(stats);
print("Calling group() with slaveOk=true, must succeed");
slave.slaveOk = true;
count = slave.getDB("foo").foo.group({initial: {n:0}, reduce: function(obj,out){out.n++;}});
printjson( count );
assert.eq( len , count[0].n , "slave group count wrong: " + slave );
print("Calling group() with slaveOk=false, must fail");
slave.slaveOk = false;
try {
count = slave.getDB("foo").foo.group({initial: {n:0}, reduce: function(obj,out){out.n++;}});
assert(false, "group() succeeded with slaveOk=false");
} catch (e) {
print("Received exception: " + e);
}
print("Calling inline mr() with slaveOk=true, must succeed");
slave.slaveOk = true;
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; };
slave.getDB("foo").foo.mapReduce(map, reduce, {out: { "inline" : 1}});
print("Calling mr() to collection with slaveOk=true, must fail");
try {
slave.getDB("foo").foo.mapReduce(map, reduce, "output");
assert(false, "mapReduce() to collection succeeded on slave");
} catch (e) {
print("Received exception: " + e);
}
print("Calling inline mr() with slaveOk=false, must fail");
slave.slaveOk = false;
try {
slave.getDB("foo").foo.mapReduce(map, reduce, {out: { "inline" : 1}});
assert(false, "mapReduce() succeeded on slave with slaveOk=false");
} catch (e) {
print("Received exception: " + e);
}
print("Calling mr() to collection with slaveOk=false, must fail");
try {
slave.getDB("foo").foo.mapReduce(map, reduce, "output");
assert(false, "mapReduce() to collection succeeded on slave with slaveOk=false");
} catch (e) {
print("Received exception: " + e);
}
});
// Shut down the set and finish the test.
replTest.stopSet( signal );
}
doTest( 15 );
print("SUCCESS");
|