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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.infrastructure.threading;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import java.util.ArrayList;
28 import java.util.List;
29 import org.junit.Test;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * The Class ThreadingTest.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 public class ThreadingTest {
40 private static final String LOCAL_NAME = "localName";
41 // Logger for this class
42 private static final XLogger logger = XLoggerFactory.getXLogger(ThreadingTest.class);
45 * Test thread factory initialization.
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));
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));
61 testThreadFactory(objUnderTest);
62 testThreadFactory(objUnderTest1);
66 * Test thread factory.
68 * @param threadFactory the thread factory
70 private void testThreadFactory(final ApplicationThreadFactory threadFactory) {
71 final List<Thread> threadList = new ArrayList<>();
73 for (int i = 0; i < 5; i++) {
74 final Thread thread = threadFactory.newThread(() -> {
76 threadList.add(thread);
80 for (int i = 0; i < 5; i++) {
81 Thread thread = threadList.get(i);
82 assertTrue(thread.getName().startsWith("Apex-" + LOCAL_NAME));
83 assertTrue(thread.getName().contains(":" + i));