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.incoming;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import org.junit.Test;
27 public class OffsetManagerTest {
30 * This test validates that as events are cached, and flagged as published, that the 'safe' offset
31 * is advanced correctly.
34 public void testOffsetAdvancement() throws Exception {
36 final Long offsetPeriod = 500L; // ms
38 // Create an instance of the offset manager.
39 OffsetManager offsetManager = new OffsetManager(10, offsetPeriod);
41 // Now, cache some events as if we had consumed them and they
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);
48 // Mark some of them as 'published'
49 offsetManager.markAsPublished(event1Index);
50 offsetManager.markAsPublished(event2Index);
51 offsetManager.markAsPublished(event4Index);
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);
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);
62 nextOffset = waitForOffsetUpdate(nextOffset, offsetPeriod, offsetManager);
63 assertTrue("Unexpected 'next offset' value. Expected=4, actual=" + nextOffset, nextOffset == 4);
66 private Long waitForOffsetUpdate(Long currentOffset, Long offsetPeriod, OffsetManager offsetManager)
67 throws InterruptedException {
69 Long newOffset = currentOffset;
71 while (currentOffset == newOffset) {
73 // Wait long enough for the offset manager to have hopefully kicked it's offset
75 Thread.sleep(offsetPeriod);
76 newOffset = offsetManager.getNextOffsetToCommit();
78 // We might have just missed the update due to timing, so we will retry a
79 // few times before giving up...
82 fail("Safe offset was not updated as expected");