2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.comm;
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;
29 import java.util.concurrent.ConcurrentHashMap;
30 import org.junit.jupiter.api.Test;
32 class QueueTokenTest {
33 private static final String STRING1 = "a string";
34 private static final String STRING2 = "another string";
36 private QueueToken<String> token;
39 void test() throws Exception {
40 token = new QueueToken<>(STRING1);
41 assertEquals(STRING1, token.get());
43 assertEquals(STRING1, token.replaceItem(STRING2));
44 assertEquals(STRING2, token.get());
46 assertEquals(STRING2, token.replaceItem(null));
47 assertNull(token.get());
49 assertNull(token.replaceItem(null));
50 assertNull(token.get());
52 assertNull(token.replaceItem(STRING1));
53 assertNull(token.get());
56 * Now do some mult-threaded tests, hopefully causing some contention.
59 token = new QueueToken<>("");
61 Set<String> values = ConcurrentHashMap.newKeySet();
63 // create and configure the threads
64 Thread[] threads = new Thread[100];
65 for (int x = 0; x < threads.length; ++x) {
67 threads[x] = new Thread(() -> values.add(token.replaceItem("me-" + xfinal)));
68 threads[x].setDaemon(true);
71 // start the threads all at once
72 for (Thread thread : threads) {
76 // wait for the threads to stop
77 for (Thread thread : threads) {
81 values.add(token.replaceItem(null));
83 for (int x = 0; x < threads.length; ++x) {
84 String msg = "me-" + x;
85 assertTrue(values.contains(msg), msg);