Search in sources :

Example 1 with TimelineSync

use of org.zalando.nakadi.service.timeline.TimelineSync in project nakadi by zalando.

the class EventTypeCacheTest method testEventTypesPreloaded.

@Test
public void testEventTypesPreloaded() throws Exception {
    final EventTypeRepository etRepo = Mockito.mock(EventTypeRepository.class);
    final TimelineDbRepository timelineRepository = Mockito.mock(TimelineDbRepository.class);
    final ZooKeeperHolder zkHolder = Mockito.mock(ZooKeeperHolder.class);
    final TimelineSync timelineSync = Mockito.mock(TimelineSync.class);
    final EventType et = TestUtils.buildDefaultEventType();
    Mockito.when(etRepo.list()).thenReturn(Collections.singletonList(et));
    final Timeline timeline = TestUtils.buildTimeline(et.getName());
    final List<Timeline> timelines = Collections.singletonList(timeline);
    Mockito.when(timelineRepository.listTimelinesOrdered()).thenReturn(timelines);
    Mockito.when(timelineSync.registerTimelineChangeListener(Matchers.eq(et.getName()), Mockito.any())).thenReturn(() -> {
    });
    final EventTypeCache eventTypeCache = new EventTypeCache(etRepo, timelineRepository, zkHolder, null, timelineSync) {

        @Override
        public void created(final String name) throws Exception {
        // ignore this call, because mocking is too complex
        }
    };
    Assert.assertSame(et, eventTypeCache.getEventType(et.getName()));
    Mockito.verify(etRepo, Mockito.times(0)).findByName(Mockito.any());
    Mockito.verify(etRepo, Mockito.times(1)).list();
    Assert.assertEquals(timelines, eventTypeCache.getTimelinesOrdered(et.getName()));
    Mockito.verify(timelineRepository, Mockito.times(0)).listTimelinesOrdered(Mockito.any());
    Mockito.verify(timelineRepository, Mockito.times(1)).listTimelinesOrdered();
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) EventType(org.zalando.nakadi.domain.EventType) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) ZooKeeperHolder(org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder) Test(org.junit.Test)

Example 2 with TimelineSync

use of org.zalando.nakadi.service.timeline.TimelineSync in project nakadi by zalando.

the class TimelineSyncAT method testPublishPauseOnTimelineUpdate.

@Test
public void testPublishPauseOnTimelineUpdate() throws InterruptedException, IOException {
    final TimelineSync t1 = createTimeline();
    final TimelineSync t2 = createTimeline();
    final String eventType = UUID.randomUUID().toString();
    // Lock publishing
    t1.startTimelineUpdate(eventType, TimeUnit.SECONDS.toMillis(30));
    final AtomicBoolean lockTaken = new AtomicBoolean(false);
    new Thread(() -> {
        try (Closeable cl = t2.workWithEventType(eventType, TimeUnit.SECONDS.toMillis(5))) {
            lockTaken.set(true);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }, "publisher").start();
    // Wait for thread to start.
    Thread.sleep(TimeUnit.SECONDS.toMillis(2));
    Assert.assertEquals(false, lockTaken.get());
    // Now release event type
    t1.finishTimelineUpdate(eventType);
    Assert.assertEquals(true, lockTaken.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Closeable(java.io.Closeable) TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with TimelineSync

use of org.zalando.nakadi.service.timeline.TimelineSync in project nakadi by zalando.

the class TimelineSyncAT method testNotificationsCalledWhenTimelineUpdated.

@Test
public void testNotificationsCalledWhenTimelineUpdated() throws InterruptedException {
    final String eventType1 = UUID.randomUUID().toString();
    final String eventType2 = UUID.randomUUID().toString();
    final RefreshListener l1 = new RefreshListener();
    final TimelineSync t1 = createTimeline();
    t1.registerTimelineChangeListener(eventType1, l1.createConsumer());
    final RefreshListener l2 = new RefreshListener();
    final TimelineSync t2 = createTimeline();
    t2.registerTimelineChangeListener(eventType2, l2.createConsumer());
    t1.startTimelineUpdate(eventType1, TimeUnit.SECONDS.toMillis(3));
    t1.startTimelineUpdate(eventType2, TimeUnit.SECONDS.toMillis(3));
    Assert.assertEquals(0, l1.getData().size());
    Assert.assertEquals(0, l2.getData().size());
    t1.finishTimelineUpdate(eventType1);
    // Wait for listeners
    TestUtils.waitFor(() -> Assert.assertEquals(1, l1.getData().size()), TimeUnit.SECONDS.toMillis(1));
    Assert.assertEquals(new Integer(1), l1.getData().get(eventType1));
    Assert.assertEquals(0, l2.getData().size());
    t1.finishTimelineUpdate(eventType2);
    Assert.assertEquals(1, l1.getData().size());
    Assert.assertEquals(new Integer(1), l1.getData().get(eventType1));
    Assert.assertEquals(1, l2.getData().size());
    Assert.assertEquals(new Integer(1), l2.getData().get(eventType2));
}
Also used : TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) Test(org.junit.Test)

Example 4 with TimelineSync

use of org.zalando.nakadi.service.timeline.TimelineSync in project nakadi by zalando.

the class TimelineSyncAT method testTimelineUpdateWaitsForActivePublish.

@Test
public void testTimelineUpdateWaitsForActivePublish() throws InterruptedException, IOException, TimeoutException {
    final TimelineSync t1 = createTimeline();
    final TimelineSync t2 = createTimeline();
    final String eventType = UUID.randomUUID().toString();
    final AtomicBoolean updated = new AtomicBoolean(false);
    try (Closeable ignored = t1.workWithEventType(eventType, TimeUnit.SECONDS.toMillis(1))) {
        new Thread(() -> {
            try {
                t2.startTimelineUpdate(eventType, TimeUnit.SECONDS.toMillis(30));
                updated.set(true);
            } catch (final InterruptedException ex) {
                throw new RuntimeException(ex);
            }
        }, "timeline_update").start();
        // Wait a little bit for thread to start timeline update
        Thread.sleep(TimeUnit.SECONDS.toMillis(3));
        Assert.assertEquals(false, updated.get());
    }
    TestUtils.waitFor(() -> Assert.assertTrue(updated.get()));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Closeable(java.io.Closeable) TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 TimelineSync (org.zalando.nakadi.service.timeline.TimelineSync)4 Closeable (java.io.Closeable)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 EventType (org.zalando.nakadi.domain.EventType)1 Timeline (org.zalando.nakadi.domain.Timeline)1 EventTypeRepository (org.zalando.nakadi.repository.EventTypeRepository)1 ZooKeeperHolder (org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder)1