16
16
17
17
package com .mongodb .util ;
18
18
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
+
19
25
public class SimplePoolTest extends com .mongodb .util .TestCase {
20
26
21
27
class MyPool extends SimplePool <Integer > {
@@ -41,7 +47,7 @@ public Integer createNew(){
41
47
}
42
48
43
49
@ org .testng .annotations .Test
44
- public void testBasic1 (){
50
+ public void testBasic1 () throws InterruptedException {
45
51
MyPool p = new MyPool ( 10 );
46
52
47
53
int a = p .get ();
@@ -56,7 +62,7 @@ public void testBasic1(){
56
62
}
57
63
58
64
@ org .testng .annotations .Test
59
- public void testMax1 (){
65
+ public void testMax1 () throws InterruptedException {
60
66
MyPool p = new MyPool ( 10 );
61
67
62
68
int a = p .get ();
@@ -70,7 +76,7 @@ public void testMax1(){
70
76
}
71
77
72
78
@ org .testng .annotations .Test
73
- public void testMax2 (){
79
+ public void testMax2 () throws InterruptedException {
74
80
MyPool p = new MyPool ( 10 );
75
81
76
82
int a = p .get ();
@@ -83,7 +89,7 @@ public void testMax2(){
83
89
}
84
90
85
91
@ org .testng .annotations .Test
86
- public void testMax3 (){
92
+ public void testMax3 () throws InterruptedException {
87
93
MyPool p = new MyPool ( 10 );
88
94
89
95
int a = p .get ();
@@ -96,7 +102,7 @@ public void testMax3(){
96
102
}
97
103
98
104
@ org .testng .annotations .Test
99
- public void testThrowErrorFromCreate (){
105
+ public void testThrowErrorFromCreate () throws InterruptedException {
100
106
MyPool p = new MyPool ( 1 );
101
107
p ._throwError = true ;
102
108
@@ -115,7 +121,7 @@ public void testThrowErrorFromCreate(){
115
121
}
116
122
117
123
@ org .testng .annotations .Test
118
- public void testCouldCreate () {
124
+ public void testCouldCreate () throws InterruptedException {
119
125
SimplePool <Integer > p = new SimplePool <Integer >("pool" , 2 ) {
120
126
@ Override
121
127
protected Integer createNew () {
@@ -145,7 +151,7 @@ protected int pick(int recommended, boolean couldCreate) {
145
151
}
146
152
147
153
@ org .testng .annotations .Test
148
- public void testReturnNullFromCreate (){
154
+ public void testReturnNullFromCreate () throws InterruptedException {
149
155
MyPool p = new MyPool ( 1 );
150
156
p ._returnNull = true ;
151
157
@@ -163,6 +169,41 @@ public void testReturnNullFromCreate(){
163
169
assertEquals ( Integer .valueOf (0 ) , a );
164
170
}
165
171
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
+ }
166
207
167
208
public static void main ( String args [] ){
168
209
SimplePoolTest t = new SimplePoolTest ();
0 commit comments