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.assertTrue;
27 import java.util.concurrent.ConcurrentHashMap;
28 import org.junit.Test;
30 public class QueueTokenTest {
31 private static final String STRING1 = "a string";
32 private static final String STRING2 = "another string";
34 private QueueToken<String> token;
37 public void test() throws Exception {
38 token = new QueueToken<>(STRING1);
39 assertEquals(STRING1, token.get());
41 assertEquals(STRING1, token.replaceItem(STRING2));
42 assertEquals(STRING2, token.get());
44 assertEquals(STRING2, token.replaceItem(null));
45 assertEquals(null, token.get());
47 assertEquals(null, token.replaceItem(null));
48 assertEquals(null, token.get());
50 assertEquals(null, token.replaceItem(STRING1));
51 assertEquals(null, token.get());
54 * Now do some mult-threaded tests, hopefully causing some contention.
57 token = new QueueToken<>("");
59 Set<String> values = ConcurrentHashMap.newKeySet();
61 // create and configure the threads
62 Thread[] threads = new Thread[100];
63 for (int x = 0; x < threads.length; ++x) {
65 threads[x] = new Thread(() -> values.add(token.replaceItem("me-" + xfinal)));
66 threads[x].setDaemon(true);
69 // start the threads all at once
70 for (Thread thread : threads) {
74 // wait for the threads to stop
75 for (Thread thread : threads) {
79 values.add(token.replaceItem(null));
81 for (int x = 0; x < threads.length; ++x) {
82 String msg = "me-" + x;
83 assertTrue(msg, values.contains(msg));