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
|
/*******************************************************************************
* * org.onap.ccsdk
* * ===========================================================================
* * Copyright © 2023 AT&T Intellectual Property. All rights reserved.
* * ===========================================================================
* * 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====================================================
* *
* *
******************************************************************************/
package org.onap.ccsdk.apps.cadi.util.test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.*;
import org.junit.*;
import org.onap.ccsdk.apps.cadi.CadiException;
import org.onap.ccsdk.apps.cadi.util.Log;
import org.onap.ccsdk.apps.cadi.util.Pool;
import org.onap.ccsdk.apps.cadi.util.Pool.*;
public class JU_Pool {
private class IntegerCreator implements Creator<Integer> {
private int current = 0;
@Override
public Integer create() {
return current++;
}
@Override
public void destroy(Integer t) {
t = 0;
}
@Override
public boolean isValid(Integer t) {
return (t & 0x1) == 0;
}
@Override
public void reuse(Integer t) {
}
}
// Used for CustomLogger Testing
private StringBuilder sb = new StringBuilder();
private class CustomLogger implements Log {
@Override
public void log(Log.Type type, Object... o) {
for (Object item : o) {
sb.append(item.toString());
}
}
}
/**
* Enter variable amount in this order
*
* count, used, max_range, max_objects
* @param intPool
* @param ints
*/
private void check(Pool<Integer> intPool, int ... ints) {
String rpt = intPool.toString();
// Fallthrough on purpose, to process only the ints entered, but in the right order.
switch(ints.length) {
case 4:
assertTrue(rpt.contains(String.format("max_objects(%d)", ints[3])));
case 3:
assertTrue(rpt.contains(String.format("max_range(%d)", ints[2])));
case 2:
assertTrue(rpt.contains(String.format("used(%d)", ints[1])));
case 1:
assertTrue(rpt.contains(String.format("count(%d)", ints[0])));
}
}
@Test
public void settings() throws CadiException {
Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
check(intPool,0,0,Pool.MAX_RANGE,Pool.MAX_OBJECTS);
// Check MaxObjects, min is 0
intPool.setMaxObjects(-10);
check(intPool,0,0,Pool.MAX_RANGE,0);
intPool.setMaxObjects(10);
check(intPool,0,0,Pool.MAX_RANGE,10);
// Check MaxRange, min is 0
intPool.setMaxRange(-10);
check(intPool,0,0,0,10);
intPool.setMaxRange(2);
check(intPool,0,0,2,10);
// Validate Priming
intPool.prime(3);
check(intPool,3,3,2,10);
// Drain
intPool.drain();
check(intPool,0,0,2,10);
}
@Test
public void range() throws CadiException {
Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
intPool.setMaxRange(2);
check(intPool,0,0,2);
// Prime
intPool.prime(3);
check(intPool,3,3,2);
// Using 3 leaves count (in Pool) and Used (by System) 3
List<Pooled<Integer>> using = new ArrayList<>();
for(int i=0;i<3;++i) {
using.add(intPool.get());
}
check(intPool,0,3,2);
// Using 3 more creates more Objects, and uses immediately
for(int i=0;i<3;++i) {
using.add(intPool.get());
}
check(intPool,0,6,2);
// Clean out all Objects in possession, but there are 6 Objects not returned yet.
intPool.drain();
check(intPool,0,6,2);
// Returning Objects
for(Pooled<Integer> i : using) {
i.done();
}
// Since Range is 2, keep only 2, and destroy the rest
check(intPool,2,2,2);
// Shutdown (helpful for stopping Services) involves turning off range
intPool.setMaxRange(0).drain();
check(intPool,0,0,0);
}
@Test
public void tooManyObjects() throws CadiException {
/*
* It should be noted that "tooManyObjects" isn't enforced by the Pool, because Objects are not
* tracked (other than used) once they leave the pool.
*
* It is information that using entities, like Thread Pools, can use to limit creations of expensive objects
*/
Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
intPool.setMaxObjects(10).setMaxRange(2);
check(intPool,0,0,2,10);
assertFalse(intPool.tooManyObjects());
// Obtain up to maxium Objects
List<Pooled<Integer>> using = new ArrayList<>();
for(int i=0;i<10;++i) {
using.add(intPool.get());
}
check(intPool,0,10,2,10);
assertFalse(intPool.tooManyObjects());
using.add(intPool.get());
check(intPool,0,11,2,10);
assertTrue(intPool.tooManyObjects());
// Returning Objects
for(Pooled<Integer> i : using) {
i.done();
}
// Returning Objects puts Pool back in range
check(intPool,2,2,2,10);
assertFalse(intPool.tooManyObjects());
}
@Test
public void bulkTest() throws CadiException {
Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
intPool.prime(10);
// Remove all of the invalid items (in this case, odd numbers)
assertFalse(intPool.validate());
// Make sure we got them all
assertTrue(intPool.validate());
// Get an item from the pool
Pooled<Integer> gotten = intPool.get();
assertThat(gotten.content, is(0));
// finalize that item, then check the next one to make sure we actually purged
// the odd numbers
gotten = intPool.get();
assertThat(gotten.content, is(2));
intPool.drain();
}
@Test
public void loggingTest() {
Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
// Log to Log.NULL for coverage
intPool.log(Log.Type.info,"Test log output");
intPool.setLogger(new CustomLogger());
intPool.log(Log.Type.info,"Test log output");
assertThat(sb.toString(), is("Test log output"));
}
}
|