e570ae0c7eff61342eed9882ab6f201b3a2883af
[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     // Logger for this class
41     private static final XLogger logger = XLoggerFactory.getXLogger(ThreadingTest.class);
42
43     /**
44      * Test thread factory initialization.
45      */
46     @Test
47     public void testThreadFactoryInitialization() {
48         final ApplicationThreadFactory threadFactory0 = new ApplicationThreadFactory("localName", 0);
49         assertNotNull("Failed to create ApplicationThreadFactory threadFactory0", threadFactory0);
50         logger.debug(threadFactory0.toString());
51         assertTrue("Failed to name ApplicationThreadFactory threadFactory0",
52                 threadFactory0.getName().startsWith("Apex-localName"));
53         final ApplicationThreadFactory threadFactory1 = new ApplicationThreadFactory("localName", 0);
54         assertNotNull("Failed to create ApplicationThreadFactory threadFactory1", threadFactory1);
55         logger.debug(threadFactory1.toString());
56         assertTrue("Failed to name ApplicationThreadFactory threadFactory1",
57                 threadFactory1.getName().startsWith("Apex-localName"));
58
59         testThreadFactory(threadFactory0, 0);
60         testThreadFactory(threadFactory1, 1);
61     }
62
63     /**
64      * Test thread factory.
65      *
66      * @param threadFactory the thread factory
67      * @param factoryId the factory id
68      */
69     private void testThreadFactory(final ApplicationThreadFactory threadFactory, final int factoryId) {
70         final List<ThreadingTestThread> threadList = new ArrayList<ThreadingTestThread>();
71
72         for (int i = 0; i < 5; i++) {
73             threadList.add(new ThreadingTestThread());
74             threadList.get(i).setThread(threadFactory.newThread(threadList.get(i)));
75             assertTrue(threadList.get(i).getName().startsWith("Apex-localName"));
76             assertTrue(threadList.get(i).getName().contains(":" + i));
77             threadList.get(i).getThread().start();
78         }
79
80         // Threads should need a little more than 300ms to count to 3
81         ThreadUtilities.sleep(380);
82
83         for (int i = 0; i < 5; i++) {
84             threadList.get(i).interrupt();
85         }
86
87         for (int i = 0; i < 5; i++) {
88             assertTrue("Thread (" + i + ") failed to get count (" + threadList.get(i).getCounter() + ") up to 3",
89                     threadList.get(i).getCounter() == 3);
90         }
91     }
92 }