Skip to content

Commit 79f979c

Browse files
committed
JAVA-635: Improved test coverage for InterruptedException/MongoInterruptedException
1 parent 2bbfac6 commit 79f979c

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/test/com/mongodb/DBPortPoolTest.java

+44-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020

2121
import org.testng.annotations.Test;
2222

23+
import java.net.UnknownHostException;
24+
import java.util.concurrent.BrokenBarrierException;
25+
import java.util.concurrent.Callable;
2326
import java.util.concurrent.CountDownLatch;
27+
import java.util.concurrent.ExecutionException;
2428
import java.util.concurrent.ExecutorService;
2529
import java.util.concurrent.Executors;
30+
import java.util.concurrent.Future;
2631
import java.util.concurrent.TimeUnit;
2732

2833
public class DBPortPoolTest extends com.mongodb.util.TestCase {
@@ -76,13 +81,48 @@ public void run(){
7681
es.shutdown();
7782
es.awaitTermination( Integer.MAX_VALUE, TimeUnit.SECONDS);
7883

79-
for(int x = 2; x<8; x++) {
80-
// Assert.assertNotSame( 0 , ports[x]._lastThread, x + ":" + ports[x].hashCode());
81-
}
82-
8384
assertEquals( pool.getMaxSize() , pool.getAvailable() );
8485
}
8586

87+
@Test
88+
public void testInterruptedException() throws UnknownHostException, InterruptedException {
89+
MongoOptions options = new MongoOptions();
90+
options.connectionsPerHost = 1;
91+
final DBPortPool pool = new DBPortPool( new ServerAddress("localhost"), options );
92+
pool.get();
93+
94+
ExecutorService executor = Executors.newSingleThreadExecutor();
95+
final CountDownLatch ready = new CountDownLatch(1);
96+
97+
Callable<Boolean> callable = new Callable<Boolean>() {
98+
@Override
99+
public Boolean call() throws BrokenBarrierException, InterruptedException {
100+
try {
101+
ready.countDown();
102+
pool.get();
103+
return false;
104+
} catch (MongoInterruptedException e) {
105+
// return true if interrupted
106+
return true;
107+
}
108+
}
109+
};
110+
Future<Boolean> future = executor.submit(callable);
111+
112+
ready.await();
113+
// Interrupt the thread
114+
executor.shutdownNow();
115+
116+
try {
117+
assertEquals(true, future.get());
118+
} catch (InterruptedException e) {
119+
fail("Should not happen, since this thread was not interrupted");
120+
} catch (ExecutionException e) {
121+
e.printStackTrace();
122+
fail("Should not happen");
123+
}
124+
}
125+
86126
public static void main( String args[] ){
87127
(new DBPortPoolTest()).runConsole();
88128
}

src/test/com/mongodb/util/SimplePoolTest.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.util;
1818

1919
import java.util.concurrent.Callable;
20+
import java.util.concurrent.CountDownLatch;
2021
import java.util.concurrent.ExecutionException;
2122
import java.util.concurrent.ExecutorService;
2223
import java.util.concurrent.Executors;
@@ -170,19 +171,22 @@ public void testReturnNullFromCreate() throws InterruptedException {
170171
}
171172

172173
@org.testng.annotations.Test()
173-
public void testThrowsInterruptedException() {
174-
final MyPool p = new MyPool(1);
174+
public void testThrowsInterruptedException() throws InterruptedException {
175+
final MyPool p = new MyPool(1);
175176
try {
176177
p.get();
177178
} catch (InterruptedException e) {
178179
fail("Should not throw InterruptedException here");
179180
}
180181

181182
ExecutorService executor = Executors.newSingleThreadExecutor();
183+
final CountDownLatch ready = new CountDownLatch(1);
184+
182185
Callable<Boolean> callable = new Callable<Boolean>() {
183186
@Override
184187
public Boolean call() {
185188
try {
189+
ready.countDown();
186190
p.get();
187191
return false;
188192
} catch (InterruptedException e) {
@@ -193,6 +197,7 @@ public Boolean call() {
193197
};
194198
Future<Boolean> future = executor.submit(callable);
195199

200+
ready.await();
196201
// Interrupt the thread
197202
executor.shutdownNow();
198203

0 commit comments

Comments
 (0)