23f458a671edcbd2bebb0bdd232f9d3f6ef03d53
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.infrastructure.threading;
23
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.concurrent.TimeUnit;
31
32 import org.junit.Test;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
35
36 /**
37  * The Class ThreadingTest.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class ThreadingTest {
42
43     private static final String LOCAL_NAME = "localName";
44     // Logger for this class
45     private static final XLogger logger = XLoggerFactory.getXLogger(ThreadingTest.class);
46
47     /**
48      * Test thread factory initialization.
49      */
50     @Test
51     public void testThreadFactoryInitialization() {
52         final ApplicationThreadFactory objUnderTest = new ApplicationThreadFactory(LOCAL_NAME, 0);
53         assertNotNull("Failed to create ApplicationThreadFactory threadFactory0", objUnderTest);
54         logger.debug(objUnderTest.toString());
55         assertTrue("Failed to name ApplicationThreadFactory threadFactory0",
56                 objUnderTest.getName().startsWith("Apex-" + LOCAL_NAME));
57
58         final ApplicationThreadFactory objUnderTest1 = new ApplicationThreadFactory(LOCAL_NAME, 0);
59         assertNotNull("Failed to create ApplicationThreadFactory threadFactory1", objUnderTest1);
60         logger.debug(objUnderTest1.toString());
61         assertTrue("Failed to name ApplicationThreadFactory threadFactory1",
62                 objUnderTest1.getName().startsWith("Apex-" + LOCAL_NAME));
63
64         testThreadFactory(objUnderTest);
65         testThreadFactory(objUnderTest1);
66     }
67
68     /**
69      * Test thread factory.
70      *
71      * @param threadFactory the thread factory
72      */
73     private void testThreadFactory(final ApplicationThreadFactory threadFactory) {
74         final List<ThreadingTestThread> threadList = new ArrayList<>();
75
76         for (int i = 0; i < 5; i++) {
77             final ThreadingTestThread runnable = new ThreadingTestThread();
78             threadList.add(runnable);
79
80             final Thread thread = threadFactory.newThread(runnable);
81             thread.start();
82
83             if (i == 4) {
84                 await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> thread.isAlive());
85             }
86
87         }
88
89         for (int i = 0; i < 5; i++) {
90             threadList.get(i).interrupt();
91         }
92
93         for (int i = 0; i < 5; i++) {
94             ThreadingTestThread thread = threadList.get(i);
95             assertTrue(thread.getName().startsWith("Apex-" + LOCAL_NAME));
96             assertTrue(thread.getName().contains(":" + i));
97             assertTrue("Thread (" + i + ") count should be greater than 0 ", thread.getCounter() > 0);
98         }
99     }
100 }