1616
1717package com .mongodb .util ;
1818
19+ import java .util .concurrent .Callable ;
20+ import java .util .concurrent .ExecutionException ;
21+ import java .util .concurrent .ExecutorService ;
22+ import java .util .concurrent .Executors ;
23+ import java .util .concurrent .Future ;
24+
1925public class SimplePoolTest extends com .mongodb .util .TestCase {
2026
2127 class MyPool extends SimplePool <Integer > {
@@ -41,7 +47,7 @@ public Integer createNew(){
4147 }
4248
4349 @ org .testng .annotations .Test
44- public void testBasic1 (){
50+ public void testBasic1 () throws InterruptedException {
4551 MyPool p = new MyPool ( 10 );
4652
4753 int a = p .get ();
@@ -56,7 +62,7 @@ public void testBasic1(){
5662 }
5763
5864 @ org .testng .annotations .Test
59- public void testMax1 (){
65+ public void testMax1 () throws InterruptedException {
6066 MyPool p = new MyPool ( 10 );
6167
6268 int a = p .get ();
@@ -70,7 +76,7 @@ public void testMax1(){
7076 }
7177
7278 @ org .testng .annotations .Test
73- public void testMax2 (){
79+ public void testMax2 () throws InterruptedException {
7480 MyPool p = new MyPool ( 10 );
7581
7682 int a = p .get ();
@@ -83,7 +89,7 @@ public void testMax2(){
8389 }
8490
8591 @ org .testng .annotations .Test
86- public void testMax3 (){
92+ public void testMax3 () throws InterruptedException {
8793 MyPool p = new MyPool ( 10 );
8894
8995 int a = p .get ();
@@ -96,7 +102,7 @@ public void testMax3(){
96102 }
97103
98104 @ org .testng .annotations .Test
99- public void testThrowErrorFromCreate (){
105+ public void testThrowErrorFromCreate () throws InterruptedException {
100106 MyPool p = new MyPool ( 1 );
101107 p ._throwError = true ;
102108
@@ -115,7 +121,7 @@ public void testThrowErrorFromCreate(){
115121 }
116122
117123 @ org .testng .annotations .Test
118- public void testCouldCreate () {
124+ public void testCouldCreate () throws InterruptedException {
119125 SimplePool <Integer > p = new SimplePool <Integer >("pool" , 2 ) {
120126 @ Override
121127 protected Integer createNew () {
@@ -145,7 +151,7 @@ protected int pick(int recommended, boolean couldCreate) {
145151 }
146152
147153 @ org .testng .annotations .Test
148- public void testReturnNullFromCreate (){
154+ public void testReturnNullFromCreate () throws InterruptedException {
149155 MyPool p = new MyPool ( 1 );
150156 p ._returnNull = true ;
151157
@@ -163,6 +169,41 @@ public void testReturnNullFromCreate(){
163169 assertEquals ( Integer .valueOf (0 ) , a );
164170 }
165171
172+ @ org .testng .annotations .Test ()
173+ public void testThrowsInterruptedException () {
174+ final MyPool p = new MyPool (1 );
175+ try {
176+ p .get ();
177+ } catch (InterruptedException e ) {
178+ fail ("Should not throw InterruptedException here" );
179+ }
180+
181+ ExecutorService executor = Executors .newSingleThreadExecutor ();
182+ Callable <Boolean > callable = new Callable <Boolean >() {
183+ @ Override
184+ public Boolean call () {
185+ try {
186+ p .get ();
187+ return false ;
188+ } catch (InterruptedException e ) {
189+ // return true if interrupted
190+ return true ;
191+ }
192+ }
193+ };
194+ Future <Boolean > future = executor .submit (callable );
195+
196+ // Interrupt the thread
197+ executor .shutdownNow ();
198+
199+ try {
200+ assertEquals (true , future .get ());
201+ } catch (InterruptedException e ) {
202+ fail ("Should not happen, since this thread was not interrupted" );
203+ } catch (ExecutionException e ) {
204+ fail ("Should not happen" );
205+ }
206+ }
166207
167208 public static void main ( String args [] ){
168209 SimplePoolTest t = new SimplePoolTest ();
0 commit comments