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