/
KeyedResourcePoolTest.java
261 lines (223 loc) · 9.95 KB
/
KeyedResourcePoolTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright 2012 LinkedIn, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package voldemort.utils.pool;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.Before;
import org.junit.Test;
import voldemort.utils.Time;
public class KeyedResourcePoolTest extends KeyedResourcePoolTestBase {
protected static int POOL_SIZE = 5;
protected static long TIMEOUT_MS = 500;
@Before
public void setUp() {
factory = new TestResourceFactory();
config = new ResourcePoolConfig().setMaxPoolSize(POOL_SIZE)
.setTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS);
this.pool = new KeyedResourcePool<String, TestResource>(factory, config);
}
@Test
public void testResourcePoolConfigTimeout() {
// Issue 343
assertEquals(config.getTimeout(TimeUnit.MILLISECONDS), TIMEOUT_MS);
assertEquals(config.getTimeout(TimeUnit.NANOSECONDS), TIMEOUT_MS * Time.NS_PER_MS);
}
@Test
public void testPoolingOccurs() throws Exception {
TestResource r1 = this.pool.checkout("a");
this.pool.checkin("a", r1);
TestResource r2 = this.pool.checkout("a");
assertTrue("Checked out value should equal checked in value (found " + r1 + ", " + r2 + ")",
r1 == r2);
}
@Test
public void testFullPoolBlocks() throws Exception {
for(int i = 0; i < POOL_SIZE; i++)
this.pool.checkout("a");
try {
this.pool.checkout("a");
fail("Checking out more items than in the pool should timeout.");
} catch(TimeoutException e) {
// this is good
}
}
@Test
public void testExceptionOnDestroy() throws Exception {
assertTrue("POOL_SIZE is not big enough", POOL_SIZE >= 2);
Exception toThrow = new Exception("An exception! (This exception is expected and so will print out some output to stderr.)");
this.factory.setDestroyException(toThrow);
assertEquals(0, this.pool.getTotalResourceCount());
try {
TestResource checkedOut = this.pool.checkout("a");
assertFalse(checkedOut.isDestroyed());
assertEquals(1, this.factory.getCreated());
assertEquals(1, this.pool.getTotalResourceCount());
assertEquals(0, this.pool.getCheckedInResourceCount());
this.pool.checkin("a", checkedOut);
assertEquals(1, this.factory.getCreated());
assertEquals(1, this.pool.getTotalResourceCount());
assertEquals(1, this.pool.getCheckedInResourceCount());
this.pool.checkout("a");
assertEquals(1, this.factory.getCreated());
assertEquals(1, this.pool.getTotalResourceCount());
assertEquals(0, this.pool.getCheckedInResourceCount());
for(int i = 0; i < POOL_SIZE - 1; i++) {
checkedOut = this.pool.checkout("a");
assertFalse(checkedOut.isDestroyed());
}
assertEquals(POOL_SIZE, this.factory.getCreated());
assertEquals(POOL_SIZE, this.pool.getTotalResourceCount());
assertEquals(0, this.pool.getCheckedInResourceCount());
assertEquals(0, this.factory.getDestroyed());
checkedOut.invalidate();
try {
// pool.checkin should catch and print out the destroy
// exception.
this.pool.checkin("a", checkedOut);
} catch(Exception caught) {
fail("No exception expected.");
}
assertEquals(POOL_SIZE - 1, this.pool.getTotalResourceCount());
assertEquals(0, this.pool.getCheckedInResourceCount());
assertEquals(0, this.factory.getDestroyed());
this.pool.checkout("a");
assertEquals(POOL_SIZE + 1, this.factory.getCreated());
assertEquals(POOL_SIZE, this.pool.getTotalResourceCount());
assertEquals(0, this.pool.getCheckedInResourceCount());
assertEquals(0, this.factory.getDestroyed());
} catch(Exception caught) {
fail("No exception expected.");
}
}
@Test
public void testExceptionOnCreate() throws Exception {
Exception toThrow = new Exception("An exception!");
assertEquals(0, this.pool.getTotalResourceCount());
this.factory.setCreateException(toThrow);
try {
this.pool.checkout("b");
fail("Expected exception!");
} catch(Exception caught) {
assertEquals("The exception thrown by the factory should propagate to the caller.",
toThrow,
caught);
}
// failed checkout shouldn't affect count
assertEquals(0, this.pool.getTotalResourceCount());
assertEquals(0, this.pool.getCheckedInResourceCount());
}
// NEGATIVE Test. See comment in line.
@Test
public void repeatedCheckins() throws Exception {
assertEquals(0, this.pool.getTotalResourceCount());
TestResource resource = this.pool.checkout("a");
assertEquals(1, this.factory.getCreated());
assertEquals(1, this.pool.getTotalResourceCount());
assertEquals(0, this.pool.getCheckedInResourceCount());
this.pool.checkin("a", resource);
assertEquals(1, this.factory.getCreated());
assertEquals(1, this.pool.getTotalResourceCount());
assertEquals(1, this.pool.getCheckedInResourceCount());
this.pool.checkin("a", resource);
assertEquals(1, this.factory.getCreated());
assertEquals(1, this.pool.getTotalResourceCount());
// KeyedResourcePool does not protect against repeated checkins. It
// Should. If it did, then the commented out test below would be
// correct.
assertEquals(2, this.pool.getCheckedInResourceCount());
// assertEquals(1, this.pool.getCheckedInResourceCount());
}
// NEGATIVE Test. See comment in line.
@Test
public void testExceptionOnFullCheckin() throws Exception {
assertEquals(0, this.pool.getTotalResourceCount());
Queue<TestResource> resources = new LinkedList<TestResource>();
for(int i = 0; i < POOL_SIZE; i++) {
TestResource resource = this.pool.checkout("a");
resources.add(resource);
}
assertEquals(POOL_SIZE, this.pool.getTotalResourceCount());
for(int i = 0; i < POOL_SIZE; i++) {
this.pool.checkin("a", resources.poll());
}
assertEquals(POOL_SIZE, this.pool.getTotalResourceCount());
TestResource extraResource = this.factory.create("a");
try {
this.pool.checkin("a", extraResource);
fail("Checking in an extra resource should throw an exception.");
} catch(IllegalStateException ise) {
// this is good
}
// KeyedResourcePool does not protect against repeated or extraneous
// checkins. If an extraneous checkin occurs, then the checked in
// resource is destroyed and the size of the resource pool is reduced by
// one (even though it should not be in this exceptional case).
assertEquals(POOL_SIZE - 1, this.pool.getTotalResourceCount());
// assertEquals(POOL_SIZE, this.pool.getTotalResourceCount());
}
// NEGATIVE Test. See comment in line.
@Test
public void testCheckinExtraneousResource() throws Exception {
assertEquals(0, this.pool.getTotalResourceCount());
TestResource resource = this.pool.checkout("a");
this.pool.checkin("a", resource);
TestResource extraResource = this.factory.create("a");
// KeyedResourcePool should not permit random resources to be checked
// in. Until it protects against arbitrary resources being checked in,
// it is possible to checkin an extraneous resource.
this.pool.checkin("a", extraResource);
assertEquals(1, this.pool.getTotalResourceCount());
assertEquals(2, this.pool.getCheckedInResourceCount());
}
// NEGATIVE Test. See comment in line.
@Test
public void testNeverCheckin() throws Exception {
assertEquals(0, this.pool.getTotalResourceCount());
{
this.pool.checkout("a");
}
// KeyedResourcePool does not protect against resources being checked
// out and never checked back in (or destroyed).
assertEquals(1, this.pool.getTotalResourceCount());
assertEquals(0, this.pool.getCheckedInResourceCount());
}
@Test
public void testInvalidIsDestroyed() throws Exception {
TestResource r1 = this.pool.checkout("a");
r1.invalidate();
this.pool.checkin("a", r1);
TestResource r2 = this.pool.checkout("a");
assertTrue("Invalid objects should be destroyed.", r1 != r2);
assertTrue("Invalid objects should be destroyed.", r1.isDestroyed());
assertEquals(1, this.factory.getDestroyed());
}
@Test
public void testMaxInvalidCreations() throws Exception {
this.factory.setCreatedValid(false);
try {
this.pool.checkout("a");
fail("Exceeded max failed attempts without exception.");
} catch(ExcessiveInvalidResourcesException e) {
// this is expected
}
}
}