2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
27 package org.openecomp.appc.pool;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertNotNull;
32 import static org.junit.Assert.assertNull;
33 import static org.junit.Assert.assertTrue;
35 import java.io.IOException;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.lang.reflect.Proxy;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.openecomp.appc.pool.Allocator;
43 import org.openecomp.appc.pool.Destructor;
44 import org.openecomp.appc.pool.Pool;
45 import org.openecomp.appc.pool.PoolDrainedException;
46 import org.openecomp.appc.pool.PoolExtensionException;
47 import org.openecomp.appc.pool.PoolSpecificationException;
48 import org.openecomp.appc.pool.*;
51 public class PoolTest implements Allocator<Testable>, Destructor<Testable> {
53 private Pool<Testable> pool;
54 private static final int MIN = 10;
55 private static final int MAX = 100;
56 private int index = 0;
57 private int destroyCount = 0;
60 * Set up the test by allocating a pool with MIN-MAX size (bounded pool)
62 * @throws PoolSpecificationException
63 * If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
66 public void setup() throws PoolSpecificationException {
67 pool = new Pool<>(MIN, MAX);
73 * Test that trying to construct a pool with a bad minimum throws an exception
75 * @throws PoolSpecificationException
76 * If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
78 @Test(expected = PoolSpecificationException.class)
79 public void testInvalidMinSize() throws PoolSpecificationException {
80 pool = new Pool<>(-1, MAX);
84 * Test that trying to construct a pool with a bad maximum throws an exception
86 * @throws PoolSpecificationException
87 * If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
89 @Test(expected = PoolSpecificationException.class)
90 public void testInvalidMaxSize() throws PoolSpecificationException {
91 pool = new Pool<>(MIN, -1);
95 * Test creation of a pool where max is less than min fails
97 * @throws PoolSpecificationException
98 * If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
100 @Test(expected = PoolSpecificationException.class)
101 public void testInvalidSizeRange() throws PoolSpecificationException {
102 pool = new Pool<>(MAX, MIN);
109 public void testMinPool() {
110 assertEquals(MIN, pool.getMinPool());
117 public void testMaxPool() {
118 assertEquals(MAX, pool.getMaxPool());
125 public void testAllocator() {
126 assertNull(pool.getAllocator());
127 pool.setAllocator(this);
128 assertNotNull(pool.getAllocator());
135 public void testDestructor() {
136 assertNull(pool.getDestructor());
137 pool.setDestructor(this);
138 assertNotNull(pool.getDestructor());
142 * Test that we can allocate and release elements and that the pool maintains them in MRU order
144 * @throws PoolExtensionException
145 * If the pool cannot be extended
146 * @throws PoolDrainedException
147 * If the caller is trying to reserve an element from a drained pool
150 public void testAllocateAndRelease() throws PoolExtensionException, PoolDrainedException {
151 pool.setAllocator(this);
153 assertFalse(pool.isDrained());
156 * Allocate three elements
158 Testable value1 = pool.reserve();
159 assertNotNull(value1);
160 assertEquals(Integer.valueOf(MIN - 1), value1.getId());
161 assertEquals(1, pool.getAllocatedSize());
162 assertEquals(MIN - 1, pool.getFreeSize());
163 assertEquals(1, pool.getAllocatedSize());
165 Testable value2 = pool.reserve();
166 assertNotNull(value2);
167 assertEquals(Integer.valueOf(MIN - 2), value2.getId());
168 assertEquals(2, pool.getAllocatedSize());
169 assertEquals(MIN - 2, pool.getFreeSize());
170 assertEquals(2, pool.getAllocatedSize());
172 Testable value3 = pool.reserve();
173 assertNotNull(value3);
174 assertEquals(Integer.valueOf(MIN - 3), value3.getId());
175 assertEquals(3, pool.getAllocatedSize());
176 assertEquals(MIN - 3, pool.getFreeSize());
177 assertEquals(3, pool.getAllocatedSize());
180 * Now, release them in the order obtained
182 pool.release(value1);
183 pool.release(value2);
184 pool.release(value3);
186 assertEquals(0, pool.getAllocatedSize());
187 assertEquals(MIN, pool.getFreeSize());
190 * Now, allocate them again, but their values should be reversed (3, 2, 1) representing the most recently used
191 * to the least recently used.
193 value1 = pool.reserve();
194 assertNotNull(value1);
195 assertEquals(Integer.valueOf(MIN - 3), value1.getId());
197 value2 = pool.reserve();
198 assertNotNull(value2);
199 assertEquals(Integer.valueOf(MIN - 2), value2.getId());
201 value3 = pool.reserve();
202 assertNotNull(value3);
203 assertEquals(Integer.valueOf(MIN - 1), value3.getId());
207 * Test that we can trim the pool to a desired size
209 * @throws PoolExtensionException
210 * If the pool cannot be extended
211 * @throws NoSuchMethodException
212 * if a matching method is not found.
213 * @throws SecurityException
214 * if the request is denied.
215 * @throws IllegalAccessException
216 * if this Method object is enforcing Java language access control and the underlying method is
218 * @throws IllegalArgumentException
219 * if the method is an instance method and the specified object argument is not an instance of the class
220 * or interface declaring the underlying method (or of a subclass or implementor thereof); if the number
221 * of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or
222 * if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal
223 * parameter type by a method invocation conversion.
224 * @throws InvocationTargetException
225 * if the underlying method throws an exception.
226 * @throws PoolDrainedException
227 * If the caller is trying to reserve an element from a drained pool
229 @SuppressWarnings("nls")
231 public void testTrim() throws PoolExtensionException, NoSuchMethodException, SecurityException,
232 IllegalAccessException, IllegalArgumentException, InvocationTargetException, PoolDrainedException {
233 pool.setAllocator(this);
235 Proxy[] array = new Proxy[SIZE];
237 assertEquals(0, pool.getAllocatedSize());
238 for (int i = 0; i < SIZE; i++) {
239 array[i] = (Proxy) pool.reserve();
241 assertEquals(SIZE, pool.getAllocatedSize());
243 for (int i = 0; i < SIZE; i++) {
244 pool.release((Testable) array[i]);
246 assertEquals(0, pool.getAllocatedSize());
248 assertEquals(SIZE, pool.getFreeSize());
250 Method trimMethod = Pool.class.getDeclaredMethod("trim", new Class[] {
253 trimMethod.setAccessible(true);
254 trimMethod.invoke(pool, new Object[] {
258 assertEquals(MIN, pool.getFreeSize());
262 * Test that we can drain a pool containing a mix of free and allocated elements
264 * @throws PoolExtensionException
265 * If the pool cannot be extended
266 * @throws PoolDrainedException
267 * If the caller is trying to reserve an element from a drained pool
270 public void testDrain() throws PoolExtensionException, PoolDrainedException {
273 int ALLOC = SIZE - FREE;
275 Proxy[] array = new Proxy[SIZE];
276 pool.setAllocator(this);
277 pool.setDestructor(this);
279 assertFalse(pool.isDrained());
281 assertEquals(0, pool.getAllocatedSize());
282 for (int i = 0; i < SIZE; i++) {
283 array[i] = (Proxy) pool.reserve();
285 assertEquals(SIZE, pool.getAllocatedSize());
287 for (int i = 0; i < FREE; i++) {
288 pool.release((Testable) array[i]);
290 assertEquals(ALLOC, pool.getAllocatedSize());
291 assertEquals(FREE, pool.getFreeSize());
294 assertEquals(0, pool.getFreeSize());
295 assertEquals(0, pool.getAllocatedSize());
296 assertTrue(pool.isDrained());
298 assertEquals(SIZE, destroyCount);
302 * @see org.openecomp.appc.pool.Destructor#destroy(java.io.Closeable, org.openecomp.appc.pool.Pool)
305 public void destroy(Testable obj, Pool<Testable> pool) {
309 } catch (IOException e) {
315 * @see org.openecomp.appc.pool.Allocator#allocate(org.openecomp.appc.pool.Pool)
318 public Testable allocate(Pool<Testable> pool) {
319 Testable e = new Element(index++);