summaryrefslogtreecommitdiffstats
path: root/appc-common/src/test/java/org/openecomp/appc/pool/PoolTest.java
blob: dedc70251e5a87a2c681fd12c5d1563726d4e29b (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*-
 * ============LICENSE_START=======================================================
 * APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2017 Amdocs
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */



package org.openecomp.appc.pool;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.junit.Before;
import org.junit.Test;
import org.openecomp.appc.pool.Allocator;
import org.openecomp.appc.pool.Destructor;
import org.openecomp.appc.pool.Pool;
import org.openecomp.appc.pool.PoolDrainedException;
import org.openecomp.appc.pool.PoolExtensionException;
import org.openecomp.appc.pool.PoolSpecificationException;
import org.openecomp.appc.pool.*;


public class PoolTest implements Allocator<Testable>, Destructor<Testable> {

    private Pool<Testable> pool;
    private static final int MIN = 10;
    private static final int MAX = 100;
    private int index = 0;
    private int destroyCount = 0;

    /**
     * Set up the test by allocating a pool with MIN-MAX size (bounded pool)
     *
     * @throws PoolSpecificationException
     *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
     */
    @Before
    public void setup() throws PoolSpecificationException {
        pool = new Pool<>(MIN, MAX);
        index = 0;
        destroyCount = 0;
    }

    /**
     * Test that trying to construct a pool with a bad minimum throws an exception
     *
     * @throws PoolSpecificationException
     *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
     */
    @Test(expected = PoolSpecificationException.class)
    public void testInvalidMinSize() throws PoolSpecificationException {
        pool = new Pool<>(-1, MAX);
    }

    /**
     * Test that trying to construct a pool with a bad maximum throws an exception
     *
     * @throws PoolSpecificationException
     *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
     */
    @Test(expected = PoolSpecificationException.class)
    public void testInvalidMaxSize() throws PoolSpecificationException {
        pool = new Pool<>(MIN, -1);
    }

    /**
     * Test creation of a pool where max is less than min fails
     *
     * @throws PoolSpecificationException
     *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
     */
    @Test(expected = PoolSpecificationException.class)
    public void testInvalidSizeRange() throws PoolSpecificationException {
        pool = new Pool<>(MAX, MIN);
    }

    /**
     * Test state
     */
    @Test
    public void testMinPool() {
        assertEquals(MIN, pool.getMinPool());
    }

    /**
     * Test state
     */
    @Test
    public void testMaxPool() {
        assertEquals(MAX, pool.getMaxPool());
    }

    /**
     * Test state
     */
    @Test
    public void testAllocator() {
        assertNull(pool.getAllocator());
        pool.setAllocator(this);
        assertNotNull(pool.getAllocator());
    }

    /**
     * Test state
     */
    @Test
    public void testDestructor() {
        assertNull(pool.getDestructor());
        pool.setDestructor(this);
        assertNotNull(pool.getDestructor());
    }

    /**
     * Test that we can allocate and release elements and that the pool maintains them in MRU order
     *
     * @throws PoolExtensionException
     *             If the pool cannot be extended
     * @throws PoolDrainedException
     *             If the caller is trying to reserve an element from a drained pool
     */
    @Test
    public void testAllocateAndRelease() throws PoolExtensionException, PoolDrainedException {
        pool.setAllocator(this);

        assertFalse(pool.isDrained());

        /*
         * Allocate three elements
         */
        Testable value1 = pool.reserve();
        assertNotNull(value1);
        assertEquals(Integer.valueOf(MIN - 1), value1.getId());
        assertEquals(1, pool.getAllocatedSize());
        assertEquals(MIN - 1, pool.getFreeSize());
        assertEquals(1, pool.getAllocatedSize());

        Testable value2 = pool.reserve();
        assertNotNull(value2);
        assertEquals(Integer.valueOf(MIN - 2), value2.getId());
        assertEquals(2, pool.getAllocatedSize());
        assertEquals(MIN - 2, pool.getFreeSize());
        assertEquals(2, pool.getAllocatedSize());

        Testable value3 = pool.reserve();
        assertNotNull(value3);
        assertEquals(Integer.valueOf(MIN - 3), value3.getId());
        assertEquals(3, pool.getAllocatedSize());
        assertEquals(MIN - 3, pool.getFreeSize());
        assertEquals(3, pool.getAllocatedSize());

        /*
         * Now, release them in the order obtained
         */
        pool.release(value1);
        pool.release(value2);
        pool.release(value3);

        assertEquals(0, pool.getAllocatedSize());
        assertEquals(MIN, pool.getFreeSize());

        /*
         * Now, allocate them again, but their values should be reversed (3, 2, 1) representing the most recently used
         * to the least recently used.
         */
        value1 = pool.reserve();
        assertNotNull(value1);
        assertEquals(Integer.valueOf(MIN - 3), value1.getId());

        value2 = pool.reserve();
        assertNotNull(value2);
        assertEquals(Integer.valueOf(MIN - 2), value2.getId());

        value3 = pool.reserve();
        assertNotNull(value3);
        assertEquals(Integer.valueOf(MIN - 1), value3.getId());
    }

    /**
     * Test that we can trim the pool to a desired size
     *
     * @throws PoolExtensionException
     *             If the pool cannot be extended
     * @throws NoSuchMethodException
     *             if a matching method is not found.
     * @throws SecurityException
     *             if the request is denied.
     * @throws IllegalAccessException
     *             if this Method object is enforcing Java language access control and the underlying method is
     *             inaccessible.
     * @throws IllegalArgumentException
     *             if the method is an instance method and the specified object argument is not an instance of the class
     *             or interface declaring the underlying method (or of a subclass or implementor thereof); if the number
     *             of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or
     *             if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal
     *             parameter type by a method invocation conversion.
     * @throws InvocationTargetException
     *             if the underlying method throws an exception.
     * @throws PoolDrainedException
     *             If the caller is trying to reserve an element from a drained pool
     */
    @SuppressWarnings("nls")
    @Test
    public void testTrim() throws PoolExtensionException, NoSuchMethodException, SecurityException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException, PoolDrainedException {
        pool.setAllocator(this);
        int SIZE = 50;
        Proxy[] array = new Proxy[SIZE];

        assertEquals(0, pool.getAllocatedSize());
        for (int i = 0; i < SIZE; i++) {
            array[i] = (Proxy) pool.reserve();
        }
        assertEquals(SIZE, pool.getAllocatedSize());

        for (int i = 0; i < SIZE; i++) {
            pool.release((Testable) array[i]);
        }
        assertEquals(0, pool.getAllocatedSize());

        assertEquals(SIZE, pool.getFreeSize());

        Method trimMethod = Pool.class.getDeclaredMethod("trim", new Class[] {
            Integer.TYPE
        });
        trimMethod.setAccessible(true);
        trimMethod.invoke(pool, new Object[] {
            SIZE - MIN
        });

        assertEquals(MIN, pool.getFreeSize());
    }

    /**
     * Test that we can drain a pool containing a mix of free and allocated elements
     *
     * @throws PoolExtensionException
     *             If the pool cannot be extended
     * @throws PoolDrainedException
     *             If the caller is trying to reserve an element from a drained pool
     */
    @Test
    public void testDrain() throws PoolExtensionException, PoolDrainedException {
        int SIZE = 50;
        int FREE = 20;
        int ALLOC = SIZE - FREE;

        Proxy[] array = new Proxy[SIZE];
        pool.setAllocator(this);
        pool.setDestructor(this);

        assertFalse(pool.isDrained());

        assertEquals(0, pool.getAllocatedSize());
        for (int i = 0; i < SIZE; i++) {
            array[i] = (Proxy) pool.reserve();
        }
        assertEquals(SIZE, pool.getAllocatedSize());

        for (int i = 0; i < FREE; i++) {
            pool.release((Testable) array[i]);
        }
        assertEquals(ALLOC, pool.getAllocatedSize());
        assertEquals(FREE, pool.getFreeSize());

        pool.drain();
        assertEquals(0, pool.getFreeSize());
        assertEquals(0, pool.getAllocatedSize());
        assertTrue(pool.isDrained());

        assertEquals(SIZE, destroyCount);
    }

    /**
     * @see org.openecomp.appc.pool.Destructor#destroy(java.io.Closeable, org.openecomp.appc.pool.Pool)
     */
    @Override
    public void destroy(Testable obj, Pool<Testable> pool) {
        destroyCount++;
        try {
            obj.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @see org.openecomp.appc.pool.Allocator#allocate(org.openecomp.appc.pool.Pool)
     */
    @Override
    public Testable allocate(Pool<Testable> pool) {
        Testable e = new Element(index++);

        return e;
    }
}