Search in sources :

Example 16 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionAT method testGetSubscriptionCursors.

@Test
public void testGetSubscriptionCursors() throws IOException, InterruptedException {
    final String etName = createEventType().getName();
    final Subscription subscription = createSubscriptionForEventType(etName);
    final String cursor = "{\"items\":[{\"partition\":\"0\",\"offset\":\"25\",\"event_type\":\"" + etName + "\",\"cursor_token\":\"abc\"}]}";
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "").start();
    waitFor(() -> assertThat(client.getSessionId(), not(equalTo(SESSION_ID_UNKNOWN))));
    commitCursors(subscription, cursor, client.getSessionId()).then().statusCode(HttpStatus.SC_NO_CONTENT);
    final List<SubscriptionCursor> actualCursors = NakadiTestUtils.getSubscriptionCursors(subscription).getItems();
    assertThat(actualCursors, hasSize(1));
    final SubscriptionCursor actualCursor = actualCursors.get(0);
    assertThat(actualCursor.getPartition(), equalTo("0"));
    assertThat(actualCursor.getOffset(), equalTo(TestUtils.toTimelineOffset(25)));
    assertThat(actualCursor.getEventType(), equalTo(etName));
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Test(org.junit.Test)

Example 17 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionAT method whenStreamWrongPartitionsThenUnprocessableEntity.

@Test
public void whenStreamWrongPartitionsThenUnprocessableEntity() throws IOException {
    final String et = createEventType().getName();
    final Subscription s = createSubscriptionForEventType(et);
    final String body = "{\"partitions\":[" + "{\"event_type\":\"" + et + "\",\"partition\":\"0\"}," + "{\"event_type\":\"" + et + "\",\"partition\":\"1\"}," + "{\"event_type\":\"dummy-et-123\",\"partition\":\"0\"}]}";
    given().body(body).contentType(JSON).when().post("/subscriptions/{sid}/events", s.getId()).then().statusCode(HttpStatus.SC_UNPROCESSABLE_ENTITY).body(JSON_HELPER.matchesObject(Problem.valueOf(MoreStatus.UNPROCESSABLE_ENTITY, "Wrong partitions specified - some partitions don't belong to subscription: " + "EventTypePartition{eventType='" + et + "', partition='1'}, " + "EventTypePartition{eventType='dummy-et-123', partition='0'}")));
}
Also used : Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Test(org.junit.Test)

Example 18 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionAT method testSubscriptionWithInitialCursors.

@Test
public void testSubscriptionWithInitialCursors() throws Exception {
    final EventType et1 = createBusinessEventTypeWithPartitions(2);
    final EventType et2 = createBusinessEventTypeWithPartitions(2);
    // write 10 events to each partition of two event-types
    publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "0");
    publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "1");
    publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "0");
    publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "1");
    // create subscription with initial cursors
    final SubscriptionBase subscriptionBase = RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of(et1.getName(), et2.getName())).withStartFrom(SubscriptionBase.InitialPosition.CURSORS).withInitialCursors(ImmutableList.of(new SubscriptionCursorWithoutToken(et1.getName(), "0", "000000000000000007"), new SubscriptionCursorWithoutToken(et1.getName(), "1", "000000000000000002"), new SubscriptionCursorWithoutToken(et2.getName(), "0", Cursor.BEFORE_OLDEST_OFFSET), new SubscriptionCursorWithoutToken(et2.getName(), "1", "000000000000000009"))).buildSubscriptionBase();
    final Subscription subscription = createSubscription(subscriptionBase);
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "max_uncommitted_events=100").start();
    // we should read 19 events in total
    waitFor(() -> assertThat(client.getBatches(), hasSize(19)));
    final List<StreamBatch> batches = client.getBatches();
    // check that first events of each partition have correct offsets
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et1.getName(), "0")), equalTo(Optional.of("001-0001-000000000000000008")));
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et1.getName(), "1")), equalTo(Optional.of("001-0001-000000000000000003")));
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et2.getName(), "0")), equalTo(Optional.of("001-0001-000000000000000000")));
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et2.getName(), "1")), equalTo(Optional.empty()));
}
Also used : SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) NakadiTestUtils.createSubscriptionForEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscriptionForEventType) EventType(org.zalando.nakadi.domain.EventType) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Test(org.junit.Test)

Example 19 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionAT method testGetSubscriptionCursorsEmpty.

@Test
public void testGetSubscriptionCursorsEmpty() throws IOException {
    final String etName = createEventType().getName();
    final Subscription subscription = createSubscriptionForEventType(etName);
    Assert.assertTrue(NakadiTestUtils.getSubscriptionCursors(subscription).getItems().isEmpty());
}
Also used : Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Test(org.junit.Test)

Example 20 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionConsumptionTest method testAllEventsConsumed.

@Test
public void testAllEventsConsumed() throws IOException, InterruptedException {
    final String[] expected = new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0002-000000000000000000", "001-0002-000000000000000001", "001-0003-000000000000000000", "001-0003-000000000000000001", "001-0004-000000000000000000", "001-0004-000000000000000001" };
    // Do not test last case, because it makes no sense...
    for (int idx = -1; idx < expected.length - 1; ++idx) {
        final CountDownLatch finished = new CountDownLatch(1);
        final AtomicReference<String[]> receivedOffset = new AtomicReference<>();
        final Subscription subscription = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).withStartFrom(SubscriptionBase.InitialPosition.CURSORS).withInitialCursors(Collections.singletonList(new SubscriptionCursorWithoutToken(eventType.getName(), "0", idx == -1 ? "BEGIN" : expected[idx]))).build());
        createParallelConsumer(subscription, expected.length - 1 - idx, finished, receivedOffset::set);
        finished.await();
        final String[] testedOffsets = Arrays.copyOfRange(expected, idx + 1, expected.length);
        Assert.assertArrayEquals(testedOffsets, receivedOffset.get());
    }
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Test(org.junit.Test)

Aggregations

Subscription (org.zalando.nakadi.domain.Subscription)55 Test (org.junit.Test)40 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)21 EventType (org.zalando.nakadi.domain.EventType)15 SubscriptionBase (org.zalando.nakadi.domain.SubscriptionBase)12 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)11 List (java.util.List)10 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)9 TestStreamingClient (org.zalando.nakadi.webservice.utils.TestStreamingClient)9 ImmutableList (com.google.common.collect.ImmutableList)8 Collectors (java.util.stream.Collectors)8 SubscriptionCursor (org.zalando.nakadi.view.SubscriptionCursor)8 ItemsWrapper (org.zalando.nakadi.domain.ItemsWrapper)7 PaginationLinks (org.zalando.nakadi.domain.PaginationLinks)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 IOException (java.io.IOException)6 Optional (java.util.Optional)6 PaginationWrapper (org.zalando.nakadi.domain.PaginationWrapper)6 SubscriptionEventTypeStats (org.zalando.nakadi.domain.SubscriptionEventTypeStats)6 NakadiTestUtils.createEventType (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType)5