2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2018 IBM.
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
28 package org.onap.appc.pool;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertNotNull;
33 import static org.junit.Assert.assertNull;
34 import static org.junit.Assert.assertTrue;
36 import java.io.IOException;
37 import java.lang.reflect.InvocationTargetException;
38 import java.lang.reflect.Method;
39 import java.lang.reflect.Proxy;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.appc.pool.Allocator;
44 import org.onap.appc.pool.Destructor;
45 import org.onap.appc.pool.Pool;
46 import org.onap.appc.pool.PoolDrainedException;
47 import org.onap.appc.pool.PoolExtensionException;
48 import org.onap.appc.pool.PoolSpecificationException;
49 import org.onap.appc.pool.*;
52 public class PoolTest implements Allocator<Testable>, Destructor<Testable> {
54 private Pool<Testable> pool;
55 private static final int MIN = 10;
56 private static final int MAX = 100;
57 private int index = 0;
58 private int destroyCount = 0;
61 * Set up the test by allocating a pool with MIN-MAX size (bounded pool)
63 * @throws PoolSpecificationException
64 * If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
67 public void setup() throws PoolSpecificationException {
68 pool = new Pool<>(MIN, MAX);
74 * Test that trying to construct a pool with a bad minimum throws an exception
76 * @throws PoolSpecificationException
77 * If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
79 @Test(expected = PoolSpecificationException.class)
80 public void testInvalidMinSize() throws PoolSpecificationException {
81 pool = new Pool<>(-1, MAX);
85 * Test that trying to construct a pool with a bad maximum throws an exception
87 * @throws PoolSpecificationException
88 * If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
90 @Test(expected = PoolSpecificationException.class)
91 public void testInvalidMaxSize() throws PoolSpecificationException {
92 pool = new Pool<>(MIN, -1);
96 * Test creation of a pool where max is less than min fails
98 * @throws PoolSpecificationException
99 * If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
101 @Test(expected = PoolSpecificationException.class)
102 public void testInvalidSizeRange() throws PoolSpecificationException {
103 pool = new Pool<>(MAX, MIN);
110 public void testMinPool() {
111 assertEquals(MIN, pool.getMinPool());
118 public void testMaxPool() {
119 assertEquals(MAX, pool.getMaxPool());
126 public void testAllocator() {
127 assertNull(pool.getAllocator());
128 pool.setAllocator(this);
129 assertNotNull(pool.getAllocator());
136 public void testDestructor() {
137 assertNull(pool.getDestructor());
138 pool.setDestructor(this);
139 assertNotNull(pool.getDestructor());
143 * Test that we can allocate and release elements and that the pool maintains them in MRU order
145 * @throws PoolExtensionException
146 * If the pool cannot be extended
147 * @throws PoolDrainedException
148 * If the caller is trying to reserve an element from a drained pool
151 public void testAllocateAndRelease() throws PoolExtensionException, PoolDrainedException {
152 pool.setAllocator(this);
154 assertFalse(pool.isDrained());
157 * Allocate three elements
159 Testable value1 = pool.reserve();
160 assertNotNull(value1);
161 assertEquals(Integer.valueOf(MIN - 1), value1.getId());
162 assertEquals(1, pool.getAllocatedSize());
163 assertEquals(MIN - 1, pool.getFreeSize());
164 assertEquals(1, pool.getAllocatedSize());
166 Testable value2 = pool.reserve();
167 assertNotNull(value2);
168 assertEquals(Integer.valueOf(MIN - 2), value2.getId());
169 assertEquals(2, pool.getAllocatedSize());
170 assertEquals(MIN - 2, pool.getFreeSize());
171 assertEquals(2, pool.getAllocatedSize());
173 Testable value3 = pool.reserve();
174 assertNotNull(value3);
175 assertEquals(Integer.valueOf(MIN - 3), value3.getId());
176 assertEquals(3, pool.getAllocatedSize());
177 assertEquals(MIN - 3, pool.getFreeSize());
178 assertEquals(3, pool.getAllocatedSize());
181 * Now, release them in the order obtained
183 pool.release(value1);
184 pool.release(value2);
185 pool.release(value3);
187 assertEquals(0, pool.getAllocatedSize());
188 assertEquals(MIN, pool.getFreeSize());
191 * Now, allocate them again, but their values should be reversed (3, 2, 1) representing the most recently used
192 * to the least recently used.
194 value1 = pool.reserve();
195 assertNotNull(value1);
196 assertEquals(Integer.valueOf(MIN - 3), value1.getId());
198 value2 = pool.reserve();
199 assertNotNull(value2);
200 assertEquals(Integer.valueOf(MIN - 2), value2.getId());
202 value3 = pool.reserve();
203 assertNotNull(value3);
204 assertEquals(Integer.valueOf(MIN - 1), value3.getId());
208 * Test that we can trim the pool to a desired size
210 * @throws PoolExtensionException
211 * If the pool cannot be extended
212 * @throws NoSuchMethodException
213 * if a matching method is not found.
214 * @throws SecurityException
215 * if the request is denied.
216 * @throws IllegalAccessException
217 * if this Method object is enforcing Java language access control and the underlying method is
219 * @throws IllegalArgumentException
220 * if the method is an instance method and the specified object argument is not an instance of the class
221 * or interface declaring the underlying method (or of a subclass or implementor thereof); if the number
222 * of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or
223 * if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal
224 * parameter type by a method invocation conversion.
225 * @throws InvocationTargetException
226 * if the underlying method throws an exception.
227 * @throws PoolDrainedException
228 * If the caller is trying to reserve an element from a drained pool
230 @SuppressWarnings("nls")
232 public void testTrim() throws PoolExtensionException, NoSuchMethodException, SecurityException,
233 IllegalAccessException, IllegalArgumentException, InvocationTargetException, PoolDrainedException {
234 pool.setAllocator(this);
236 Proxy[] array = new Proxy[SIZE];
238 assertEquals(0, pool.getAllocatedSize());
239 for (int i = 0; i < SIZE; i++) {
240 array[i] = (Proxy) pool.reserve();
242 assertEquals(SIZE, pool.getAllocatedSize());
244 for (int i = 0; i < SIZE; i++) {
245 pool.release((Testable) array[i]);
247 assertEquals(0, pool.getAllocatedSize());
249 assertEquals(SIZE, pool.getFreeSize());
251 Method trimMethod = Pool.class.getDeclaredMethod("trim", new Class[] {
254 trimMethod.setAccessible(true);
255 trimMethod.invoke(pool, new Object[] {
259 assertEquals(MIN, pool.getFreeSize());
263 * Test that we can drain a pool containing a mix of free and allocated elements
265 * @throws PoolExtensionException
266 * If the pool cannot be extended
267 * @throws PoolDrainedException
268 * If the caller is trying to reserve an element from a drained pool
271 public void testDrain() throws PoolExtensionException, PoolDrainedException {
274 int ALLOC = SIZE - FREE;
276 Proxy[] array = new Proxy[SIZE];
277 pool.setAllocator(this);
278 pool.setDestructor(this);
280 assertFalse(pool.isDrained());
282 assertEquals(0, pool.getAllocatedSize());
283 for (int i = 0; i < SIZE; i++) {
284 array[i] = (Proxy) pool.reserve();
286 assertEquals(SIZE, pool.getAllocatedSize());
288 for (int i = 0; i < FREE; i++) {
289 pool.release((Testable) array[i]);
291 assertEquals(ALLOC, pool.getAllocatedSize());
292 assertEquals(FREE, pool.getFreeSize());
295 assertEquals(0, pool.getFreeSize());
296 assertEquals(0, pool.getAllocatedSize());
297 assertTrue(pool.isDrained());
299 assertEquals(SIZE, destroyCount);
303 * @see org.onap.appc.pool.Destructor#destroy(java.io.Closeable, org.onap.appc.pool.Pool)
306 public void destroy(Testable obj, Pool<Testable> pool) {
310 } catch (IOException e) {
316 * @see org.onap.appc.pool.Allocator#allocate(org.onap.appc.pool.Pool)
319 public Testable allocate(Pool<Testable> pool) {
320 Testable e = new Element(index++);
326 public void testGetAndSetProperties() throws PoolSpecificationException
328 pool= new Pool<Testable>(3, 5);
329 pool.setProperty("key1", "value1");
330 assertEquals("value1", pool.getProperty("key1"));