2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.spike.event;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import org.junit.Test;
27 import org.onap.aai.spike.event.incoming.OffsetManager;
29 public class OffsetManagerTest {
32 * This test validates that as events are cached, and flagged as published, that the 'safe' offset
33 * is advanced correctly.
36 public void testOffsetAdvancement() throws Exception {
38 final Long offsetPeriod = 50L; // ms
40 // Create an instance of the offset manager.
41 OffsetManager offsetManager = new OffsetManager(10, offsetPeriod);
43 // Now, cache some events as if we had consumed them and they
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);
50 // Mark some of them as 'published'
51 offsetManager.markAsPublished(event1Index);
52 offsetManager.markAsPublished(event2Index);
53 offsetManager.markAsPublished(event4Index);
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);
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);
64 nextOffset = waitForOffsetUpdate(nextOffset, offsetPeriod, offsetManager);
65 assertTrue("Unexpected 'next offset' value. Expected=4, actual=" + nextOffset, nextOffset == 4);
68 private Long waitForOffsetUpdate(Long currentOffset, Long offsetPeriod, OffsetManager offsetManager)
69 throws InterruptedException {
71 Long newOffset = currentOffset;
73 while (currentOffset == newOffset) {
75 // Wait long enough for the offset manager to have hopefully kicked it's offset
77 Thread.sleep(offsetPeriod);
78 newOffset = offsetManager.getNextOffsetToCommit();
80 // We might have just missed the update due to timing, so we will retry a
81 // few times before giving up...
84 fail("Safe offset was not updated as expected");