Fix issue with Spike using too much CPU
[aai/spike.git] / src / test / java / org / onap / aai / spike / event / incoming / OffsetManagerTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.spike.event.incoming;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import org.junit.Test;
26
27 public class OffsetManagerTest {
28
29     /**
30      * This test validates that as events are cached, and flagged as published, that the 'safe' offset
31      * is advanced correctly.
32      */
33     @Test
34     public void testOffsetAdvancement() throws Exception {
35
36         final Long offsetPeriod = 500L; // ms
37
38         // Create an instance of the offset manager.
39         OffsetManager offsetManager = new OffsetManager(10, offsetPeriod);
40
41         // Now, cache some events as if we had consumed them and they
42         // are in flight.
43         final int event1Index = offsetManager.cacheEvent("1", 1);
44         final int event2Index = offsetManager.cacheEvent("2", 2);
45         final int event3Index = offsetManager.cacheEvent("3", 3);
46         final int event4Index = offsetManager.cacheEvent("4", 4);
47
48         // Mark some of them as 'published'
49         offsetManager.markAsPublished(event1Index);
50         offsetManager.markAsPublished(event2Index);
51         offsetManager.markAsPublished(event4Index);
52
53         // Validate that the offset manager reported the expected offset (ie: we can only commit up
54         // to event2, event though event4 has been processed, since event3 is still in flight).
55         Long nextOffset = waitForOffsetUpdate(null, offsetPeriod, offsetManager);
56         assertTrue("Unexpected 'next offset' value.  Expected=2, actual=" + nextOffset, nextOffset == 2);
57
58         // Now, let's mark event3 as 'published'. We should see the next safe offset
59         // advance to event4 (since is was previously flagged as 'published').
60         offsetManager.markAsPublished(event3Index);
61
62         nextOffset = waitForOffsetUpdate(nextOffset, offsetPeriod, offsetManager);
63         assertTrue("Unexpected 'next offset' value.  Expected=4, actual=" + nextOffset, nextOffset == 4);
64     }
65
66     private Long waitForOffsetUpdate(Long currentOffset, Long offsetPeriod, OffsetManager offsetManager)
67             throws InterruptedException {
68
69         Long newOffset = currentOffset;
70         int retries = 3;
71         while (currentOffset == newOffset) {
72
73             // Wait long enough for the offset manager to have hopefully kicked it's offset
74             // update task.
75             Thread.sleep(offsetPeriod);
76             newOffset = offsetManager.getNextOffsetToCommit();
77
78             // We might have just missed the update due to timing, so we will retry a
79             // few times before giving up...
80             retries--;
81             if (retries == 0) {
82                 fail("Safe offset was not updated as expected");
83             }
84         }
85         return newOffset;
86     }
87
88 }