3ff91edf393a52300e13915d08708141389f942a
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / QueueTokenTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.Set;
27 import java.util.concurrent.ConcurrentHashMap;
28 import org.junit.Test;
29
30 public class QueueTokenTest {
31     private static final String STRING1 = "a string";
32     private static final String STRING2 = "another string";
33
34     private QueueToken<String> token;
35
36     @Test
37     public void test() throws Exception {
38         token = new QueueToken<>(STRING1);
39         assertEquals(STRING1, token.get());
40
41         assertEquals(STRING1, token.replaceItem(STRING2));
42         assertEquals(STRING2, token.get());
43
44         assertEquals(STRING2, token.replaceItem(null));
45         assertEquals(null, token.get());
46
47         assertEquals(null, token.replaceItem(null));
48         assertEquals(null, token.get());
49
50         assertEquals(null, token.replaceItem(STRING1));
51         assertEquals(null, token.get());
52
53         /*
54          * Now do some mult-threaded tests, hopefully causing some contention.
55          */
56
57         token = new QueueToken<>("");
58
59         Set<String> values = ConcurrentHashMap.newKeySet();
60
61         // create and configure the threads
62         Thread[] threads = new Thread[100];
63         for (int x = 0; x < threads.length; ++x) {
64             final int xfinal = x;
65             threads[x] = new Thread(() -> values.add(token.replaceItem("me-" + xfinal)));
66             threads[x].setDaemon(true);
67         }
68
69         // start the threads all at once
70         for (Thread thread : threads) {
71             thread.start();
72         }
73
74         // wait for the threads to stop
75         for (Thread thread : threads) {
76             thread.join(5000);
77         }
78
79         values.add(token.replaceItem(null));
80
81         for (int x = 0; x < threads.length; ++x) {
82             String msg = "me-" + x;
83             assertTrue(msg, values.contains(msg));
84         }
85     }
86
87 }