Search in sources :

Example 26 with TestStreamingClient

use of org.zalando.nakadi.webservice.utils.TestStreamingClient in project nakadi by zalando.

the class HilaRebalanceAT method whenDirectlyRequestPartitionsTheyAssignedCorrectly.

@Test(timeout = 15000)
public void whenDirectlyRequestPartitionsTheyAssignedCorrectly() throws IOException {
    // publish 2 events to each partition
    publishBusinessEventWithUserDefinedPartition(eventType.getName(), 16, x -> "blah" + x, x -> String.valueOf(x % 8));
    // start a stream requesting to read from partitions 5, 6
    final TestStreamingClient client = new TestStreamingClient(URL, subscription.getId(), "", Optional.empty(), Optional.of("{\"partitions\":[" + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"6\"}," + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"5\"}" + "]}"));
    client.start();
    // wait till we receive 4 batches (2 per partition)
    waitFor(() -> assertThat(client.getBatches(), hasSize(4)));
    // check that all batches are from partitions 5, 6
    checkAllEventsAreFromPartitions(client, ImmutableSet.of("5", "6"));
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Test(org.junit.Test)

Example 27 with TestStreamingClient

use of org.zalando.nakadi.webservice.utils.TestStreamingClient in project nakadi by zalando.

the class HilaRebalanceAT method whenNotCommittedThenEventsAreReplayedAfterRebalance.

@Test(timeout = 15000)
public void whenNotCommittedThenEventsAreReplayedAfterRebalance() {
    publishBusinessEventWithUserDefinedPartition(eventType.getName(), 2, x -> "blah" + x, x -> String.valueOf(x % 8));
    final TestStreamingClient clientA = TestStreamingClient.create(URL, subscription.getId(), "").start();
    waitFor(() -> assertThat(clientA.getBatches(), hasSize(2)));
    final TestStreamingClient clientB = TestStreamingClient.create(URL, subscription.getId(), "").start();
    // after commit_timeout of first client exceeds it is closed and all events are resent to second client
    waitFor(() -> assertThat(clientB.getBatches(), hasSize(2)), 10000);
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Test(org.junit.Test)

Example 28 with TestStreamingClient

use of org.zalando.nakadi.webservice.utils.TestStreamingClient in project nakadi by zalando.

the class HilaRebalanceAT method whenNoFreeSlotsForAutoClientThenConflict.

@Test(timeout = 15000)
public void whenNoFreeSlotsForAutoClientThenConflict() throws IOException, InterruptedException {
    // direct client reads all but one partition
    final TestStreamingClient directClient = new TestStreamingClient(URL, subscription.getId(), "", Optional.empty(), Optional.of("{\"partitions\":[" + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"0\"}," + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"1\"}," + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"2\"}," + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"3\"}," + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"4\"}," + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"5\"}," + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"6\"}" + "]}"));
    directClient.start();
    waitFor(() -> assertThat(directClient.getResponseCode(), is(HttpStatus.OK.value())));
    // first auto-balance client reads remaining partition
    final TestStreamingClient autoClient1 = new TestStreamingClient(URL, subscription.getId(), "");
    autoClient1.start();
    waitFor(() -> assertThat(autoClient1.getResponseCode(), is(HttpStatus.OK.value())));
    // second auto-balance client has nothing to read - should get 409 (Conflict) status code
    final TestStreamingClient autoClient2 = new TestStreamingClient(URL, subscription.getId(), "");
    autoClient2.start();
    waitFor(() -> assertThat(autoClient2.getResponseCode(), is(HttpStatus.CONFLICT.value())));
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Test(org.junit.Test)

Example 29 with TestStreamingClient

use of org.zalando.nakadi.webservice.utils.TestStreamingClient in project nakadi by zalando.

the class SubscriptionAT method testOffsetsCommit.

@Test
public void testOffsetsCommit() throws Exception {
    // create event type in Nakadi
    final String etName = createEventType().getName();
    final Subscription subscription = createSubscriptionForEventType(etName);
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "").start();
    waitFor(() -> assertThat(client.getSessionId(), not(equalTo(SESSION_ID_UNKNOWN))));
    String cursor = "{\"items\":[{\"partition\":\"0\",\"offset\":\"25\",\"event_type\":\"" + etName + "\",\"cursor_token\":\"abc\"}]}";
    commitCursors(subscription, cursor, client.getSessionId()).then().statusCode(HttpStatus.SC_NO_CONTENT);
    // check that offset is actually committed to Zookeeper
    String committedOffset = getCommittedOffsetFromZk(etName, subscription, "0");
    assertThat(committedOffset, equalTo(TestUtils.toTimelineOffset(25)));
    // commit lower offsets and expect 200
    cursor = "{\"items\":[{\"partition\":\"0\",\"offset\":\"10\",\"event_type\":\"" + etName + "\",\"cursor_token\":\"abc\"}]}";
    commitCursors(subscription, cursor, client.getSessionId()).then().statusCode(HttpStatus.SC_OK);
    // check that committed offset in Zookeeper is not changed
    committedOffset = getCommittedOffsetFromZk(etName, subscription, "0");
    assertThat(committedOffset, equalTo(TestUtils.toTimelineOffset(25)));
}
Also used : 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 30 with TestStreamingClient

use of org.zalando.nakadi.webservice.utils.TestStreamingClient in project nakadi by zalando.

the class SubscriptionConsumptionTest method createParallelConsumer.

private static void createParallelConsumer(final Subscription subscription, final int expectedEvents, final CountDownLatch finished, final Consumer<String[]> inTimeCursors) throws InterruptedException {
    final String params = Stream.of("batch_limit=1", "batch_flush_timeout=1", "stream_limit=" + expectedEvents, "stream_timeout=30").collect(Collectors.joining("&"));
    final TestStreamingClient streamingClient = new TestStreamingClient(BaseAT.URL, subscription.getId(), params);
    streamingClient.startWithAutocommit(batches -> {
        inTimeCursors.accept(batchesToCursors(batches));
        finished.countDown();
    });
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient)

Aggregations

TestStreamingClient (org.zalando.nakadi.webservice.utils.TestStreamingClient)30 Test (org.junit.Test)29 SubscriptionCursor (org.zalando.nakadi.view.SubscriptionCursor)11 Subscription (org.zalando.nakadi.domain.Subscription)8 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)8 EventType (org.zalando.nakadi.domain.EventType)6 StringContains (org.hamcrest.core.StringContains)5 SubscriptionBase (org.zalando.nakadi.domain.SubscriptionBase)5 SubscriptionEventTypeStats (org.zalando.nakadi.domain.SubscriptionEventTypeStats)5 IOException (java.io.IOException)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.hasSize (org.hamcrest.Matchers.hasSize)4 Before (org.junit.Before)4 BEGIN (org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN)4 RandomSubscriptionBuilder (org.zalando.nakadi.utils.RandomSubscriptionBuilder)4 TestUtils.waitFor (org.zalando.nakadi.utils.TestUtils.waitFor)4 NakadiTestUtils.commitCursors (org.zalando.nakadi.webservice.utils.NakadiTestUtils.commitCursors)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3