Skip to content

Commit f277649

Browse files
committed
part of a replset test
1 parent c396d05 commit f277649

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// ReplSetTest.java
2+
3+
package com.mongodb;
4+
5+
import java.util.*;
6+
7+
public class ReplSetTest {
8+
9+
static class R extends Thread {
10+
R( ServerAddress a ){
11+
_a = a;
12+
_mongo = new Mongo(a);
13+
_db = _mongo.getDB( "test" );
14+
_coll = _db.getCollection( "foo" );
15+
16+
_coll.slaveOk();
17+
}
18+
19+
public void run(){
20+
while ( true ){
21+
try {
22+
Thread.sleep( 500 );
23+
_coll.findOne();
24+
}
25+
catch ( Exception e ){
26+
System.out.println( _a + "\t" + e );
27+
}
28+
}
29+
}
30+
31+
final ServerAddress _a;
32+
final Mongo _mongo;
33+
final DB _db;
34+
final DBCollection _coll;
35+
}
36+
37+
public static void main( String args[] )
38+
throws Exception {
39+
40+
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
41+
addrs.add( new ServerAddress( "localhost" , 27017 ) );
42+
addrs.add( new ServerAddress( "localhost" , 27018 ) );
43+
addrs.add( new ServerAddress( "localhost" , 27019 ) );
44+
45+
Mongo m = new Mongo ( addrs );
46+
DB db = m.getDB( "test" );
47+
DBCollection c = db.getCollection( "foo" );
48+
c.insert( new BasicDBObject( "_id" , 17 ) );
49+
c.slaveOk();
50+
51+
for ( ServerAddress a : addrs ){
52+
new R(a).start();
53+
}
54+
55+
while ( true ){
56+
Thread.sleep( 500 );
57+
try {
58+
System.out.println( c.findOne() );
59+
c.update( new BasicDBObject( "_id" , 17 ) , new BasicDBObject( "$inc" , new BasicDBObject( "x" , 1 ) ) );
60+
}
61+
catch ( Exception e ){
62+
e.printStackTrace();
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)