File tree Expand file tree Collapse file tree 2 files changed +40
-4
lines changed Expand file tree Collapse file tree 2 files changed +40
-4
lines changed Original file line number Diff line number Diff line change @@ -444,6 +444,12 @@ public List<ServerAddress> getServerAddressList() {
444
444
*/
445
445
public void close (){
446
446
_connector .close ();
447
+ _cleaner .interrupt ();
448
+ try {
449
+ _cleaner .join ();
450
+ } catch (InterruptedException e ) {
451
+ //end early
452
+ }
447
453
}
448
454
449
455
/**
@@ -615,7 +621,11 @@ class DBCleanerThread extends Thread {
615
621
public void run () {
616
622
while (_connector .isOpen ()) {
617
623
try {
618
- Thread .sleep (cleanerIntervalMS );
624
+ try {
625
+ Thread .sleep (cleanerIntervalMS );
626
+ } catch (InterruptedException e ) {
627
+ //caused by the Mongo instance being closed -- proceed with cleanup
628
+ }
619
629
for (DB db : _dbs .values ()) {
620
630
db .cleanCursors (true );
621
631
}
Original file line number Diff line number Diff line change 19
19
package com .mongodb ;
20
20
21
21
import java .io .*;
22
- import java .util .*;
23
- import java .util .regex .*;
24
22
23
+ import org .testng .annotations .AfterTest ;
24
+ import org .testng .annotations .BeforeTest ;
25
25
import org .testng .annotations .Test ;
26
26
27
27
import com .mongodb .util .*;
@@ -34,7 +34,33 @@ public MongoTest()
34
34
}
35
35
36
36
final DB _db ;
37
-
37
+
38
+ int _originalCleanerIntervalMs ;
39
+
40
+ @ BeforeTest
41
+ public void setUp () {
42
+ _originalCleanerIntervalMs = Mongo .cleanerIntervalMS ;
43
+ }
44
+
45
+ @ Test
46
+ public void testClose_shouldNotReturnUntilCleanupThreadIsFinished () throws Exception {
47
+
48
+ System .out .println (Mongo .cleanerIntervalMS );
49
+ Mongo .cleanerIntervalMS = 250000 ; //set to a suitably large value to avoid race conditions in the test
50
+
51
+ Mongo mongo = new Mongo ();
52
+ assertNotEquals (mongo ._cleaner .getState (), Thread .State .NEW );
53
+
54
+ mongo .close ();
55
+
56
+ assertFalse (mongo ._cleaner .isAlive ());
57
+ }
58
+
59
+ @ AfterTest
60
+ public void tearDown () {
61
+ Mongo .cleanerIntervalMS = _originalCleanerIntervalMs ;
62
+ }
63
+
38
64
public static void main ( String args [] )
39
65
throws Exception {
40
66
(new MongoTest ()).runConsole ();
You can’t perform that action at this time.
0 commit comments