|
20 | 20 |
|
21 | 21 | import org.testng.annotations.Test;
|
22 | 22 |
|
| 23 | +import java.net.UnknownHostException; |
| 24 | +import java.util.concurrent.BrokenBarrierException; |
| 25 | +import java.util.concurrent.Callable; |
23 | 26 | import java.util.concurrent.CountDownLatch;
|
| 27 | +import java.util.concurrent.ExecutionException; |
24 | 28 | import java.util.concurrent.ExecutorService;
|
25 | 29 | import java.util.concurrent.Executors;
|
| 30 | +import java.util.concurrent.Future; |
26 | 31 | import java.util.concurrent.TimeUnit;
|
27 | 32 |
|
28 | 33 | public class DBPortPoolTest extends com.mongodb.util.TestCase {
|
@@ -76,13 +81,48 @@ public void run(){
|
76 | 81 | es.shutdown();
|
77 | 82 | es.awaitTermination( Integer.MAX_VALUE, TimeUnit.SECONDS);
|
78 | 83 |
|
79 |
| - for(int x = 2; x<8; x++) { |
80 |
| -// Assert.assertNotSame( 0 , ports[x]._lastThread, x + ":" + ports[x].hashCode()); |
81 |
| - } |
82 |
| - |
83 | 84 | assertEquals( pool.getMaxSize() , pool.getAvailable() );
|
84 | 85 | }
|
85 | 86 |
|
| 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 | + |
86 | 126 | public static void main( String args[] ){
|
87 | 127 | (new DBPortPoolTest()).runConsole();
|
88 | 128 | }
|
|
0 commit comments