d6a0d1f184e032f6919d7ca9e6b6ef6ea4467b9a
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / Threaded.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm;
22
23 import org.junit.After;
24 import org.junit.Before;
25
26 /**
27  * Super class for tests that run a background thread.
28  */
29 public abstract class Threaded {
30
31     /**
32      * Max time to wait, in milliseconds, for a thread to terminate or for a message to be
33      * published.
34      */
35     public static final long MAX_WAIT_MS = 5000;
36
37     /**
38      * The current background thread.
39      */
40     private Thread thread;
41
42     /**
43      * Indicates that a test is about to begin.
44      *
45      * @throws Exception if an error occurs
46      */
47     @Before
48     public void setUp() throws Exception {
49         thread = null;
50     }
51
52     /**
53      * Invokes the "stopper" function to tell the background thread to exit and then waits
54      * for it to terminate.
55      *
56      * @throws Exception if an error occurs
57      */
58     @After
59     public void tearDown() throws Exception {
60         stopThread();
61         waitStop();
62     }
63
64     /**
65      * Signals the background thread to stop.
66      *
67      * @throws Exception if an error occurs
68      */
69     protected abstract void stopThread() throws Exception;
70
71     /**
72      * Starts a background thread.
73      *
74      * @param runner what should be executed in the background thread
75      * @throws IllegalStateException if a background thread is already running
76      */
77     protected void startThread(Runnable runner) {
78         if (thread != null) {
79             throw new IllegalStateException("a background thread is already running");
80         }
81
82         thread = new Thread(runner);
83         thread.setDaemon(true);
84         thread.start();
85     }
86
87     /**
88      * Interrupts the background thread.
89      */
90     protected void interruptThread() {
91         thread.interrupt();
92     }
93
94     /**
95      * Waits for the background thread to stop.
96      *
97      * @return {@code true} if the thread has stopped, {@code false} otherwise
98      * @throws InterruptedException if this thread is interrupted while waiting
99      */
100     protected boolean waitStop() throws InterruptedException {
101         if (thread != null) {
102             Thread thread2 = thread;
103             thread = null;
104
105             thread2.join(MAX_WAIT_MS);
106
107             return !thread2.isAlive();
108         }
109
110         return true;
111     }
112 }