Add version.properties file
[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
26 import org.junit.Test;
27 import org.onap.aai.spike.event.incoming.OffsetManager;
28
29 public class OffsetManagerTest {
30
31     /**
32      * This test validates that as events are cached, and flagged as published, that the 'safe' offset
33      * is advanced correctly.
34      */
35     @Test
36     public void testOffsetAdvancement() throws Exception {
37
38         final Long offsetPeriod = 50L; // ms
39
40         // Create an instance of the offset manager.
41         OffsetManager offsetManager = new OffsetManager(10, offsetPeriod);
42
43         // Now, cache some events as if we had consumed them and they
44         // are in flight.
45         final int event1Index = offsetManager.cacheEvent("1", 1);
46         final int event2Index = offsetManager.cacheEvent("2", 2);
47         final int event3Index = offsetManager.cacheEvent("3", 3);
48         final int event4Index = offsetManager.cacheEvent("4", 4);
49
50         // Mark some of them as 'published'
51         offsetManager.markAsPublished(event1Index);
52         offsetManager.markAsPublished(event2Index);
53         offsetManager.markAsPublished(event4Index);
54
55         // Validate that the offset manager reported the expected offset (ie: we can only commit up
56         // to event2, event though event4 has been processed, since event3 is still in flight).
57         Long nextOffset = waitForOffsetUpdate(null, offsetPeriod, offsetManager);
58         assertTrue("Unexpected 'next offset' value.  Expected=2, actual=" + nextOffset, nextOffset == 2);
59
60         // Now, let's mark event3 as 'published'. We should see the next safe offset
61         // advance to event4 (since is was previously flagged as 'published').
62         offsetManager.markAsPublished(event3Index);
63
64         nextOffset = waitForOffsetUpdate(nextOffset, offsetPeriod, offsetManager);
65         assertTrue("Unexpected 'next offset' value.  Expected=4, actual=" + nextOffset, nextOffset == 4);
66     }
67
68     private Long waitForOffsetUpdate(Long currentOffset, Long offsetPeriod, OffsetManager offsetManager)
69             throws InterruptedException {
70
71         Long newOffset = currentOffset;
72         int retries = 3;
73         while (currentOffset == newOffset) {
74
75             // Wait long enough for the offset manager to have hopefully kicked it's offset
76             // update task.
77             Thread.sleep(offsetPeriod);
78             newOffset = offsetManager.getNextOffsetToCommit();
79
80             // We might have just missed the update due to timing, so we will retry a
81             // few times before giving up...
82             retries--;
83             if (retries == 0) {
84                 fail("Safe offset was not updated as expected");
85             }
86         }
87         return newOffset;
88     }
89
90 }