@@ -25,67 +25,65 @@ public class DBTest extends TestCase {
25
25
26
26
public DBTest () {
27
27
super ();
28
- cleanupDB = "com_mongodb_unittest_DBTest" ;
29
- _db = cleanupMongo .getDB ( cleanupDB );
28
+ cleanupDB = "com_mongodb_unittest_DBTest" ;
29
+ _db = cleanupMongo .getDB (cleanupDB );
30
30
}
31
31
32
32
@ Test (groups = {"basic" })
33
33
public void testCreateCollection () {
34
- _db .getCollection ( "foo1" ).drop ();
35
- _db .getCollection ( "foo2" ).drop ();
36
- _db .getCollection ( "foo3" ).drop ();
37
- _db .getCollection ( "foo4" ).drop ();
34
+ _db .getCollection ("foo1" ).drop ();
35
+ _db .getCollection ("foo2" ).drop ();
36
+ _db .getCollection ("foo3" ).drop ();
37
+ _db .getCollection ("foo4" ).drop ();
38
38
39
39
BasicDBObject o1 = new BasicDBObject ("capped" , false );
40
40
DBCollection c = _db .createCollection ("foo1" , o1 );
41
41
42
42
DBObject o2 = BasicDBObjectBuilder .start ().add ("capped" , true )
43
- .add ("size" , 100000 ).add ("max" , 10 ).get ();
43
+ .add ("size" , 100000 ).add ("max" , 10 ).get ();
44
44
c = _db .createCollection ("foo2" , o2 );
45
- for (int i = 0 ; i < 30 ; i ++) {
45
+ for (int i = 0 ; i < 30 ; i ++) {
46
46
c .insert (new BasicDBObject ("x" , i ));
47
47
}
48
48
assertTrue (c .find ().count () <= 10 );
49
49
50
50
DBObject o3 = BasicDBObjectBuilder .start ().add ("capped" , true )
51
- .add ("size" , 1000 ).add ("max" , 2 ).get ();
51
+ .add ("size" , 1000 ).add ("max" , 2 ).get ();
52
52
c = _db .createCollection ("foo3" , o3 );
53
- for (int i = 0 ; i < 30 ; i ++) {
53
+ for (int i = 0 ; i < 30 ; i ++) {
54
54
c .insert (new BasicDBObject ("x" , i ));
55
55
}
56
56
assertEquals (c .find ().count (), 2 );
57
57
58
58
try {
59
59
DBObject o4 = BasicDBObjectBuilder .start ().add ("capped" , true )
60
- .add ("size" , -20 ).get ();
60
+ .add ("size" , -20 ).get ();
61
61
c = _db .createCollection ("foo4" , o4 );
62
- }
63
- catch (MongoException e ) {
62
+ } catch (MongoException e ) {
64
63
return ;
65
64
}
66
65
assertEquals (0 , 1 );
67
66
}
68
67
69
68
@ Test (groups = {"basic" })
70
- public void testForCollectionExistence ()
71
- {
72
- _db .getCollection ( "foo1" ).drop ();
73
- _db .getCollection ( "foo2" ).drop ();
74
- _db .getCollection ( "foo3" ).drop ();
75
- _db .getCollection ( "foo4" ).drop ();
69
+ public void testForCollectionExistence () {
70
+ _db .getCollection ("foo1" ).drop ();
71
+ _db .getCollection ("foo2" ).drop ();
72
+ _db .getCollection ("foo3" ).drop ();
73
+ _db .getCollection ("foo4" ).drop ();
76
74
77
- assertFalse (_db .collectionExists ( "foo1" ));
75
+ assertFalse (_db .collectionExists ("foo1" ));
78
76
79
77
BasicDBObject o1 = new BasicDBObject ("capped" , false );
80
78
DBCollection c = _db .createCollection ("foo1" , o1 );
81
79
82
- assertTrue (_db .collectionExists ( "foo1" ), "Collection 'foo' was supposed to be created, but 'collectionExists' did not return true." );
83
- assertTrue (_db .collectionExists ( "FOO1" ));
84
- assertTrue (_db .collectionExists ( "fOo1" ));
80
+ assertTrue (_db .collectionExists ("foo1" ), "Collection 'foo' was supposed to be created, but 'collectionExists' did not return true." );
81
+ assertTrue (_db .collectionExists ("FOO1" ));
82
+ assertTrue (_db .collectionExists ("fOo1" ));
85
83
86
- _db .getCollection ( "foo1" ).drop ();
84
+ _db .getCollection ("foo1" ).drop ();
87
85
88
- assertFalse (_db .collectionExists ( "foo1" ));
86
+ assertFalse (_db .collectionExists ("foo1" ));
89
87
}
90
88
91
89
@ Test (groups = {"basic" })
@@ -148,6 +146,34 @@ public void testEnsureConnection() throws UnknownHostException {
148
146
}
149
147
}
150
148
149
+ @ Test (groups = {"basic" })
150
+ public void whenRequestStartCallsAreNestedThenTheConnectionShouldBeReleaseOnLastCallToRequestEnd () throws UnknownHostException {
151
+ Mongo m = new MongoClient (Arrays .asList (new ServerAddress ("localhost" )),
152
+ MongoClientOptions .builder ().connectionsPerHost (1 ).maxWaitTime (10 ).build ());
153
+ DB db = m .getDB ("com_mongodb_unittest_DBTest" );
154
+
155
+ try {
156
+ db .requestStart ();
157
+ try {
158
+ db .command (new BasicDBObject ("ping" , 1 ));
159
+ db .requestStart ();
160
+ try {
161
+ db .command (new BasicDBObject ("ping" , 1 ));
162
+ } finally {
163
+ db .requestDone ();
164
+ }
165
+ } finally {
166
+ db .requestDone ();
167
+ }
168
+ } finally {
169
+ m .close ();
170
+ }
171
+ }
172
+
173
+ @ Test (groups = {"basic" })
174
+ public void whenRequestDoneIsCalledWithoutFirstCallingRequestStartNoExceptionIsThrown () throws UnknownHostException {
175
+ _db .requestDone ();
176
+ }
151
177
152
178
153
179
/*public static class Person extends DBObject {
@@ -194,8 +220,8 @@ public void test1(){
194
220
195
221
final DB _db ;
196
222
197
- public static void main ( String args [] )
198
- throws Exception {
223
+ public static void main (String args [])
224
+ throws Exception {
199
225
(new DBTest ()).runConsole ();
200
226
}
201
227
}
0 commit comments