Java 17 / Spring 6 / Spring Boot 3 Upgrade
[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  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.comm;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.util.Set;
29 import java.util.concurrent.ConcurrentHashMap;
30 import org.junit.jupiter.api.Test;
31
32 class QueueTokenTest {
33     private static final String STRING1 = "a string";
34     private static final String STRING2 = "another string";
35
36     private QueueToken<String> token;
37
38     @Test
39     void test() throws Exception {
40         token = new QueueToken<>(STRING1);
41         assertEquals(STRING1, token.get());
42
43         assertEquals(STRING1, token.replaceItem(STRING2));
44         assertEquals(STRING2, token.get());
45
46         assertEquals(STRING2, token.replaceItem(null));
47         assertNull(token.get());
48
49         assertNull(token.replaceItem(null));
50         assertNull(token.get());
51
52         assertNull(token.replaceItem(STRING1));
53         assertNull(token.get());
54
55         /*
56          * Now do some mult-threaded tests, hopefully causing some contention.
57          */
58
59         token = new QueueToken<>("");
60
61         Set<String> values = ConcurrentHashMap.newKeySet();
62
63         // create and configure the threads
64         Thread[] threads = new Thread[100];
65         for (int x = 0; x < threads.length; ++x) {
66             final int xfinal = x;
67             threads[x] = new Thread(() -> values.add(token.replaceItem("me-" + xfinal)));
68             threads[x].setDaemon(true);
69         }
70
71         // start the threads all at once
72         for (Thread thread : threads) {
73             thread.start();
74         }
75
76         // wait for the threads to stop
77         for (Thread thread : threads) {
78             thread.join(5000);
79         }
80
81         values.add(token.replaceItem(null));
82
83         for (int x = 0; x < threads.length; ++x) {
84             String msg = "me-" + x;
85             assertTrue(values.contains(msg), msg);
86         }
87     }
88
89 }