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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/*
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
* Copyright (C) 2018 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.policy.drools.pooling.state;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.drools.pooling.message.BucketAssignments;
import org.onap.policy.drools.pooling.message.Identification;
import org.onap.policy.drools.pooling.message.Leader;
import org.onap.policy.drools.pooling.message.Message;
import org.onap.policy.drools.pooling.message.Query;
import org.onap.policy.drools.pooling.state.ProcessingState.HostBucket;
public class ProcessingStateTest extends BasicStateTester {
private ProcessingState state;
private HostBucket hostBucket;
@Before
public void setUp() throws Exception {
super.setUp();
state = new ProcessingState(mgr, MY_HOST);
hostBucket = new HostBucket(MY_HOST);
}
@Test
public void testGetFilter() {
Map<String, Object> filter = state.getFilter();
FilterUtilsTest utils = new FilterUtilsTest();
utils.checkArray(FilterUtils.CLASS_OR, 2, filter);
utils.checkEquals(FilterUtils.MSG_CHANNEL, Message.ADMIN, utils.getItem(filter, 0));
utils.checkEquals(FilterUtils.MSG_CHANNEL, MY_HOST, utils.getItem(filter, 1));
}
@Test
public void testProcessQuery() {
State next = mock(State.class);
when(mgr.goQuery()).thenReturn(next);
assertEquals(next, state.process(new Query()));
Identification ident = captureAdminMessage(Identification.class);
assertEquals(MY_HOST, ident.getSource());
assertEquals(ASGN3, ident.getAssignments());
}
@Test
public void testProcessingState() {
/*
* Null assignments should be OK.
*/
when(mgr.getAssignments()).thenReturn(null);
state = new ProcessingState(mgr, LEADER);
/*
* Empty assignments should be OK.
*/
when(mgr.getAssignments()).thenReturn(EMPTY_ASGN);
state = new ProcessingState(mgr, LEADER);
assertEquals(MY_HOST, state.getHost());
assertEquals(LEADER, state.getLeader());
assertEquals(EMPTY_ASGN, state.getAssignments());
/*
* Now try something with assignments.
*/
when(mgr.getAssignments()).thenReturn(ASGN3);
state = new ProcessingState(mgr, LEADER);
/*
* Prove the state is attached to the manager by invoking getHost(), which
* delegates to the manager.
*/
assertEquals(MY_HOST, state.getHost());
assertEquals(LEADER, state.getLeader());
assertEquals(ASGN3, state.getAssignments());
}
@Test(expected = IllegalArgumentException.class)
public void testProcessingState_NullLeader() {
when(mgr.getAssignments()).thenReturn(EMPTY_ASGN);
state = new ProcessingState(mgr, null);
}
@Test(expected = IllegalArgumentException.class)
public void testProcessingState_ZeroLengthHostArray() {
when(mgr.getAssignments()).thenReturn(new BucketAssignments(new String[] {}));
state = new ProcessingState(mgr, LEADER);
}
@Test
public void testGetAssignments() {
// assignments from constructor
assertEquals(ASGN3, state.getAssignments());
// null assignments - no effect
state.setAssignments(null);
assertEquals(ASGN3, state.getAssignments());
// empty assignments
state.setAssignments(EMPTY_ASGN);
assertEquals(EMPTY_ASGN, state.getAssignments());
// non-empty assignments
state.setAssignments(ASGN3);
assertEquals(ASGN3, state.getAssignments());
}
@Test
public void testSetAssignments() {
state.setAssignments(null);
verify(mgr, never()).startDistributing(any());
state.setAssignments(ASGN3);
verify(mgr).startDistributing(ASGN3);
}
@Test
public void testGetLeader() {
// check value from constructor
assertEquals(MY_HOST, state.getLeader());
state.setLeader(HOST2);
assertEquals(HOST2, state.getLeader());
state.setLeader(HOST3);
assertEquals(HOST3, state.getLeader());
}
@Test
public void testSetLeader() {
state.setLeader(MY_HOST);
assertEquals(MY_HOST, state.getLeader());
}
@Test(expected = IllegalArgumentException.class)
public void testSetLeader_Null() {
state.setLeader(null);
}
@Test
public void testIsLeader() {
state.setLeader(MY_HOST);
assertTrue(state.isLeader());
state.setLeader(HOST2);
assertFalse(state.isLeader());
}
@Test
public void testBecomeLeader() {
State next = mock(State.class);
when(mgr.goActive()).thenReturn(next);
assertEquals(next, state.becomeLeader(sortHosts(MY_HOST, HOST2)));
Leader msg = captureAdminMessage(Leader.class);
verify(mgr).startDistributing(msg.getAssignments());
verify(mgr).goActive();
}
@Test(expected = IllegalArgumentException.class)
public void testBecomeLeader_NotFirstAlive() {
// alive list contains something before my host name
state.becomeLeader(sortHosts(PREV_HOST, MY_HOST));
}
@Test
public void testMakeLeader() throws Exception {
state.becomeLeader(sortHosts(MY_HOST, HOST2));
Leader msg = captureAdminMessage(Leader.class);
// need a channel before invoking checkValidity()
msg.setChannel(Message.ADMIN);
msg.checkValidity();
assertEquals(MY_HOST, msg.getSource());
assertNotNull(msg.getAssignments());
assertTrue(msg.getAssignments().hasAssignment(MY_HOST));
assertTrue(msg.getAssignments().hasAssignment(HOST2));
// this one wasn't in the list of hosts, so it should have been removed
assertFalse(msg.getAssignments().hasAssignment(HOST1));
}
@Test
public void testMakeAssignments() throws Exception {
state.becomeLeader(sortHosts(MY_HOST, HOST2));
captureAssignments().checkValidity();
}
@Test
public void testMakeBucketArray_NullAssignments() {
when(mgr.getAssignments()).thenReturn(null);
state = new ProcessingState(mgr, MY_HOST);
state.becomeLeader(sortHosts(MY_HOST));
String[] arr = captureHostArray();
assertEquals(BucketAssignments.MAX_BUCKETS, arr.length);
assertTrue(Arrays.asList(arr).stream().allMatch(host -> MY_HOST.equals(host)));
}
@Test
public void testMakeBucketArray_ZeroAssignments() {
// bucket assignment with a zero-length array
state.setAssignments(new BucketAssignments(new String[0]));
state.becomeLeader(sortHosts(MY_HOST));
String[] arr = captureHostArray();
assertEquals(BucketAssignments.MAX_BUCKETS, arr.length);
assertTrue(Arrays.asList(arr).stream().allMatch(host -> MY_HOST.equals(host)));
}
@Test
public void testMakeBucketArray() {
/*
* All hosts are still alive, so it should have the exact same assignments as it
* had to start.
*/
state.setAssignments(ASGN3);
state.becomeLeader(sortHosts(HOST_ARR3));
String[] arr = captureHostArray();
assertTrue(arr != HOST_ARR3);
assertEquals(Arrays.asList(HOST_ARR3), Arrays.asList(arr));
}
@Test
public void testRemoveExcessHosts() {
/**
* All hosts are still alive, plus some others.
*/
state.setAssignments(ASGN3);
state.becomeLeader(sortHosts(MY_HOST, HOST1, HOST2, HOST3, HOST4));
// assignments should be unchanged
assertEquals(Arrays.asList(HOST_ARR3), captureHostList());
}
@Test
public void testAddIndicesToHostBuckets() {
// some are null, some hosts are no longer alive
String[] asgn = {null, MY_HOST, HOST3, null, HOST4, HOST1, HOST2};
state.setAssignments(new BucketAssignments(asgn));
state.becomeLeader(sortHosts(MY_HOST, HOST1, HOST2));
// every bucket should be assigned to one of the three hosts
String[] expected = {MY_HOST, MY_HOST, HOST1, HOST2, MY_HOST, HOST1, HOST2};
assertEquals(Arrays.asList(expected), captureHostList());
}
@Test
public void testAssignNullBuckets() {
/*
* Ensure buckets are assigned to the host with the fewest buckets.
*/
String[] asgn = {MY_HOST, HOST1, MY_HOST, null, null, null, null, null, MY_HOST};
state.setAssignments(new BucketAssignments(asgn));
state.becomeLeader(sortHosts(MY_HOST, HOST1, HOST2));
String[] expected = {MY_HOST, HOST1, MY_HOST, HOST2, HOST1, HOST2, HOST1, HOST2, MY_HOST};
assertEquals(Arrays.asList(expected), captureHostList());
}
@Test
public void testRebalanceBuckets() {
/**
* Some are very lopsided.
*/
String[] asgn = {MY_HOST, HOST1, MY_HOST, MY_HOST, MY_HOST, MY_HOST, HOST1, HOST2, HOST1, HOST3};
state.setAssignments(new BucketAssignments(asgn));
state.becomeLeader(sortHosts(MY_HOST, HOST1, HOST2, HOST3));
String[] expected = {HOST2, HOST1, HOST3, MY_HOST, MY_HOST, MY_HOST, HOST1, HOST2, HOST1, HOST3};
assertEquals(Arrays.asList(expected), captureHostList());
}
@Test
public void testHostBucketRemove_testHostBucketAdd_testHostBucketSize() {
assertEquals(0, hostBucket.size());
hostBucket.add(20);
hostBucket.add(30);
hostBucket.add(40);
assertEquals(3, hostBucket.size());
assertEquals(20, hostBucket.remove().intValue());
assertEquals(30, hostBucket.remove().intValue());
assertEquals(1, hostBucket.size());
// add more before taking the last item
hostBucket.add(50);
hostBucket.add(60);
assertEquals(3, hostBucket.size());
assertEquals(40, hostBucket.remove().intValue());
assertEquals(50, hostBucket.remove().intValue());
assertEquals(60, hostBucket.remove().intValue());
assertEquals(0, hostBucket.size());
// add more AFTER taking the last item
hostBucket.add(70);
assertEquals(70, hostBucket.remove().intValue());
assertEquals(0, hostBucket.size());
}
@Test
public void testHostBucketCompareTo() {
HostBucket hb1 = new HostBucket(PREV_HOST);
HostBucket hb2 = new HostBucket(MY_HOST);
assertEquals(0, hb1.compareTo(hb1));
assertEquals(0, hb1.compareTo(new HostBucket(PREV_HOST)));
// both empty
assertTrue(hb1.compareTo(hb2) < 0);
assertTrue(hb2.compareTo(hb1) > 0);
// hb1 has one bucket, so it should not be larger
hb1.add(100);
assertTrue(hb1.compareTo(hb2) > 0);
assertTrue(hb2.compareTo(hb1) < 0);
// hb1 has two buckets, so it should still be larger
hb1.add(200);
assertTrue(hb1.compareTo(hb2) > 0);
assertTrue(hb2.compareTo(hb1) < 0);
// hb1 has two buckets, hb2 has one, so hb1 should still be larger
hb2.add(1000);
assertTrue(hb1.compareTo(hb2) > 0);
assertTrue(hb2.compareTo(hb1) < 0);
// same number of buckets, so hb2 should now be larger
hb2.add(2000);
assertTrue(hb1.compareTo(hb2) < 0);
assertTrue(hb2.compareTo(hb1) > 0);
// hb2 has more buckets, it should still be larger
hb2.add(3000);
assertTrue(hb1.compareTo(hb2) < 0);
assertTrue(hb2.compareTo(hb1) > 0);
}
@Test(expected = UnsupportedOperationException.class)
public void testHostBucketHashCode() {
hostBucket.hashCode();
}
@Test(expected = UnsupportedOperationException.class)
public void testHostBucketEquals() {
hostBucket.equals(hostBucket);
}
}
|