aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/comm/TimerManagerTest.java
blob: 6d4ac025fa51905b47f1f5f18e5a858713b4e2ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.comm;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.onap.policy.pap.main.comm.TimerManager.Timer;

class TimerManagerTest extends Threaded {
    private static final String EXPECTED_EXCEPTION = "expected exception";
    private static final String MGR_NAME = "my-manager";
    private static final String NAME1 = "timer-A";
    private static final String NAME2 = "timer-B";
    private static final String NAME3 = "timer-C";

    private static final long MGR_TIMEOUT_MS = 10000;

    private MyManager mgr;

    /*
     * This is a field rather than a local variable to prevent checkstyle from complaining
     * about the distance between its assignment and its use.
     */
    private long tcur;

    /**
     * Sets up.
     *
     * @throws Exception if an error occurs
     */
    @BeforeEach
    public void setUp() throws Exception {
        super.setUp();

        mgr = new MyManager(MGR_NAME, MGR_TIMEOUT_MS);
    }

    @AfterEach
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Override
    protected void stopThread() throws Exception {
        if (mgr != null) {
            mgr.stop();
            mgr.allowSleep(10);
        }
    }

    @Test
    void testTimerManager_testStop() throws Exception {
        startThread(mgr);

        mgr.stop();
        assertTrue(waitStop());

        // ensure we can call "stop" a second time
        mgr.stop();
    }

    @Test
    void testRegister() throws Exception {
        mgr.register(NAME2, mgr::addToQueue);
        mgr.registerNewTime(NAME1, mgr::addToQueue);

        // goes to the end of the queue
        mgr.registerNewTime(NAME2, mgr::addToQueue);

        startThread(mgr);

        mgr.allowSleep(2);

        assertEquals(NAME1, mgr.awaitTimer());
        assertEquals(NAME2, mgr.awaitTimer());
    }

    @Test
    void testRun_Ex() throws Exception {
        startThread(mgr);
        mgr.register(NAME1, mgr::addToQueue);

        mgr.awaitSleep();

        // background thread is "sleeping" - now we can interrupt it
        interruptThread();

        assertTrue(waitStop());
    }

    @Test
    void testProcessTimers() throws Exception {
        startThread(mgr);
        mgr.register(NAME1, mgr::addToQueue);
        mgr.awaitSleep();
        mgr.allowSleep(1);

        mgr.registerNewTime(NAME2, mgr::addToQueue);
        mgr.awaitSleep();

        // tell it to stop before returning from "sleep"
        mgr.stop();
        mgr.allowSleep(1);

        assertTrue(waitStop());

        assertEquals(NAME1, mgr.pollTimer());
        assertNull(mgr.pollTimer());
    }

    @Test
    void testGetNextTimer() throws Exception {
        startThread(mgr);
        mgr.register(NAME1, mgr::addToQueue);
        mgr.awaitSleep();
        mgr.allowSleep(1);

        mgr.registerNewTime(NAME2, mgr::addToQueue);
        mgr.awaitSleep();
    }

    @Test
    void testProcessTimer_StopWhileWaiting() throws Exception {
        startThread(mgr);
        mgr.register(NAME1, mgr::addToQueue);
        mgr.awaitSleep();
        mgr.allowSleep(1);

        mgr.registerNewTime(NAME2, mgr::addToQueue);
        mgr.awaitSleep();

        mgr.stop();
        mgr.allowSleep(1);

        assertTrue(waitStop());

        // should have stopped after processing the first timer
        assertEquals(NAME1, mgr.pollTimer());
        assertNull(mgr.pollTimer());
    }

    @Test
    void testProcessTimer_CancelWhileWaiting() throws Exception {
        startThread(mgr);
        Timer timer = mgr.register(NAME1, mgr::addToQueue);
        mgr.awaitSleep();

        timer.cancel();
        mgr.allowSleep(1);

        mgr.registerNewTime(NAME2, mgr::addToQueue);
        mgr.awaitSleep();
        mgr.allowSleep(1);

        mgr.registerNewTime(NAME1, mgr::addToQueue);
        mgr.awaitSleep();

        // should have fired timer 2, but not timer 1
        assertEquals(NAME2, mgr.pollTimer());
        assertNull(mgr.pollTimer());
    }

    @Test
    void testProcessTimer_TimerEx() throws Exception {

        mgr.register(NAME1, name -> {
            throw new RuntimeException(EXPECTED_EXCEPTION);
        });

        mgr.register(NAME2, mgr::addToQueue);

        // same times, so only need one sleep
        startThread(mgr);
        mgr.awaitSleep();
        mgr.allowSleep(1);

        mgr.registerNewTime(NAME3, mgr::addToQueue);
        mgr.awaitSleep();

        // timer 1 fired but threw an exception, so only timer 2 should be in the queue
        assertEquals(NAME2, mgr.pollTimer());
    }

    @Test
    void testTimerAwait() throws Exception {
        startThread(mgr);

        // same times - should only sleep once
        mgr.register(NAME1, mgr::addToQueue);
        mgr.register(NAME2, mgr::addToQueue);
        mgr.awaitSleep();

        tcur = mgr.currentTimeMillis();

        mgr.allowSleep(1);

        // next one will have a new timeout, so expect to sleep again
        mgr.registerNewTime(NAME3, mgr::addToQueue);
        mgr.awaitSleep();

        long tcur2 = mgr.currentTimeMillis();
        assertTrue(tcur2 >= tcur + MGR_TIMEOUT_MS);

        assertEquals(NAME1, mgr.pollTimer());
        assertEquals(NAME2, mgr.pollTimer());
        assertNull(mgr.pollTimer());
    }

    @Test
    void testTimerCancel_WhileWaiting() throws Exception {
        startThread(mgr);

        Timer timer = mgr.register(NAME1, mgr::addToQueue);
        mgr.awaitSleep();

        // cancel while sleeping
        timer.cancel();

        mgr.registerNewTime(NAME2, mgr::addToQueue);

        // allow it to sleep through both timers
        mgr.allowSleep(2);

        // only timer 2 should have fired
        assertEquals(NAME2, mgr.awaitTimer());
    }

    @Test
    void testTimerCancel_ViaReplace() throws Exception {
        startThread(mgr);

        mgr.register(NAME1, name -> mgr.addToQueue("hello"));
        mgr.awaitSleep();

        // replace the timer while the background thread is sleeping
        mgr.registerNewTime(NAME1, name -> mgr.addToQueue("world"));

        // allow it to sleep through both timers
        mgr.allowSleep(2);

        // only timer 2 should have fired
        assertEquals("world", mgr.awaitTimer());
    }

    @Test
    void testTimerToString() {
        Timer timer = mgr.register(NAME1, mgr::addToQueue);
        assertNotNull(timer.toString());
        assertTrue(timer.toString().contains(NAME1));
    }

    @Test
    void testCurrentTimeMillis() {
        long tbeg = System.currentTimeMillis();
        long tcur = new TimerManager(MGR_NAME, MGR_TIMEOUT_MS).currentTimeMillis();
        long tend = System.currentTimeMillis();

        assertTrue(tcur >= tbeg);
        assertTrue(tend >= tcur);
    }

    @Test
    void testSleep() throws Exception {
        long tbeg = System.currentTimeMillis();
        new TimerManager(MGR_NAME, MGR_TIMEOUT_MS).sleep(10);
        long tend = System.currentTimeMillis();

        assertTrue(tend >= tbeg + 10);
    }


    /**
     * Timer Manager whose notions of time are controlled here. It also overrides the
     * {@link #sleep(long)} method so that the test thread can control when the background
     * timer thread finishes sleeping.
     */
    private static class MyManager extends TimerManager {
        private final Object lockit = new Object();
        private long curTime = 1000;
        private int offset = 0;
        private final Semaphore sleepEntered = new Semaphore(0);
        private final Semaphore sleepsAllowed = new Semaphore(0);
        private final LinkedBlockingQueue<String> results = new LinkedBlockingQueue<>();

        public MyManager(String name, long waitTimeMs) {
            super(name, waitTimeMs);
        }

        /**
         * Registers a timer with a new starting time. Because the manager uses the
         * current time when determining the expiration time, we have to temporarily
         * fiddle with {@link #curTime}, but we leave it unchanged when we're done.
         * Increases the {@link #offset} each time it's invoked.
         *
         * @return the new timer
         */
        public Timer registerNewTime(String timerName, Consumer<String> action) {
            synchronized (lockit) {
                offset++;

                curTime += offset;
                Timer timer = super.register(timerName, action);
                curTime -= offset;

                return timer;
            }
        }

        /**
         * Allows the manager to "sleep" several times.
         *
         * @param ntimes the number of times the manager should sleep
         */
        public void allowSleep(int ntimes) {
            sleepsAllowed.release(ntimes);
        }

        /**
         * Waits for the manager to "sleep".
         *
         * @throws InterruptedException if the thread is interrupted while waiting for the
         *         background thread to sleep
         */
        public void awaitSleep() throws InterruptedException {
            if (!sleepEntered.tryAcquire(MAX_WAIT_MS, TimeUnit.MILLISECONDS)) {
                fail("background thread failed to sleep");
            }
        }

        @Override
        protected long currentTimeMillis() {
            synchronized (lockit) {
                return curTime;
            }
        }

        @Override
        protected void sleep(long timeMs) throws InterruptedException {
            sleepEntered.release();
            sleepsAllowed.acquire();

            synchronized (lockit) {
                curTime += timeMs;
            }
        }

        /**
         * Waits for a timer to fire.
         *
         * @return the message the timer added to {@link #results}
         * @throws InterruptedException if this thread is interrupted while waiting
         */
        private String awaitTimer() throws InterruptedException {
            return results.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS);
        }

        /**
         * Adds a name to the queue.
         *
         * @param name the name to add
         */
        private void addToQueue(String name) {
            results.add(name);
        }

        /**
         * Polls to see if a timer has fired.
         *
         * @return the message the timer added to {@link #results}, or {@code null} if no
         *         timer has fired yet
         */
        private String pollTimer() {
            return results.poll();
        }
    }
}