2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.comm;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.util.Arrays;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.atomic.AtomicBoolean;
32 import org.junit.Test;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.pap.main.comm.MultiPdpStatusListener;
35 import org.onap.policy.pdp.common.models.PdpStatus;
37 public class MultiPdpStatusListenerTest {
38 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
39 private static final String TOPIC = "my-topic";
40 private static final String NAME1 = "pdp_1";
41 private static final String NAME2 = "pdp_2";
42 private static final List<String> NAME_LIST = Arrays.asList(NAME1, NAME2);
44 private MultiPdpStatusListener listener;
45 private PdpStatus status;
48 public void testMultiPdpStatusListenerString() throws Exception {
49 listener = new MultiPdpStatusListener(NAME1);
50 assertEquals(Arrays.asList(NAME1).toString(), listener.getUnseenPdpNames().toString());
52 // a name is in the queue - not done yet
53 assertFalse(doWait(0));
57 public void testMultiPdpStatusListenerCollectionOfString() throws Exception {
58 List<String> lst = NAME_LIST;
60 listener = new MultiPdpStatusListener(lst);
61 assertEquals(lst.toString(), listener.getUnseenPdpNames().toString());
63 // a name is in the queue - not done yet
64 assertFalse(doWait(0));
67 * Try with an empty list - should already be complete.
69 listener = new MultiPdpStatusListener(new LinkedList<>());
70 assertTrue(listener.getUnseenPdpNames().isEmpty());
71 assertTrue(doWait(0));
75 public void testGetUnseenPdpNames() {
76 List<String> lst = NAME_LIST;
78 listener = new MultiPdpStatusListener(lst);
79 assertEquals(lst.toString(), listener.getUnseenPdpNames().toString());
81 // receive message from one PDP
82 status = new PdpStatus();
83 status.setName(NAME2);
84 listener.onTopicEvent(INFRA, TOPIC, status);
85 assertEquals(Arrays.asList(NAME1).toString(), listener.getUnseenPdpNames().toString());
87 // receive message from the other PDP
88 status = new PdpStatus();
89 status.setName(NAME1);
90 listener.onTopicEvent(INFRA, TOPIC, status);
91 assertTrue(listener.getUnseenPdpNames().isEmpty());
95 public void testAwait() throws Exception {
96 // try with an empty list - should already be complete
97 listener = new MultiPdpStatusListener(new LinkedList<>());
98 assertTrue(doWait(0));
100 // try it with something in the list
101 listener = new MultiPdpStatusListener(NAME_LIST);
102 assertFalse(doWait(0));
104 // process a message from one PDP - wait should block the entire time
105 status = new PdpStatus();
106 status.setName(NAME1);
107 listener.onTopicEvent(INFRA, TOPIC, status);
108 long tbeg = System.currentTimeMillis();
109 assertFalse(doWait(50));
110 assertTrue(System.currentTimeMillis() - tbeg >= 49);
112 // process a message from the other PDP - wait should NOT block
113 status = new PdpStatus();
114 status.setName(NAME2);
115 listener.onTopicEvent(INFRA, TOPIC, status);
116 tbeg = System.currentTimeMillis();
117 assertTrue(doWait(4000));
118 assertTrue(System.currentTimeMillis() - tbeg < 3000);
122 public void testOnTopicEvent() throws Exception {
123 listener = new MultiPdpStatusListener(NAME_LIST);
126 assertFalse(doWait(0));
128 // process a message - still not done as have another name to go
129 status = new PdpStatus();
130 status.setName(NAME1);
131 listener.onTopicEvent(INFRA, TOPIC, status);
132 assertFalse(doWait(0));
134 // process a message from the same PDP - still not done
135 status = new PdpStatus();
136 status.setName(NAME1);
137 listener.onTopicEvent(INFRA, TOPIC, status);
138 assertFalse(doWait(0));
140 // process another message - now we're done
141 status = new PdpStatus();
142 status.setName(NAME2);
143 listener.onTopicEvent(INFRA, TOPIC, status);
144 assertTrue(doWait(0));
148 * Waits for the listener to complete. Spawns a background thread to do the waiting so
149 * we can limit how long we wait.
151 * @param millisec milliseconds to wait
152 * @return {@code true} if the wait completed successfully, {@code false} otherwise
153 * @throws InterruptedException if this thread is interrupted while waiting for the
154 * background thread to complete
156 private boolean doWait(long millisec) throws InterruptedException {
157 AtomicBoolean done = new AtomicBoolean(false);
159 Thread thread = new Thread() {
163 done.set(listener.await(millisec, TimeUnit.MILLISECONDS));
165 } catch (InterruptedException expected) {