Search in sources :

Example 1 with TestStreamingClient

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

the class UserJourneyAT method userJourneyHila.

@Test(timeout = 15000)
public void userJourneyHila() throws InterruptedException, IOException {
    postEvents(rangeClosed(0, 3).mapToObj(x -> "{\"foo\":\"bar" + x + "\"}").collect(Collectors.toList()).toArray(new String[4]));
    // create subscription
    final SubscriptionBase subscriptionToCreate = RandomSubscriptionBuilder.builder().withOwningApplication("stups_aruha-test-end2end-nakadi").withEventType(eventTypeName).withStartFrom(BEGIN).buildSubscriptionBase();
    final Subscription subscription = createSubscription(jsonRequestSpec(), subscriptionToCreate);
    // list subscriptions
    jsonRequestSpec().param("event_type", eventTypeName).get("/subscriptions").then().statusCode(OK.value()).body("items.size()", equalTo(1)).body("items[0].id", equalTo(subscription.getId()));
    // create client and wait till we receive all events
    final TestStreamingClient client = new TestStreamingClient(RestAssured.baseURI + ":" + RestAssured.port, subscription.getId(), "", oauthToken).start();
    waitFor(() -> assertThat(client.getBatches(), hasSize(4)));
    final List<StreamBatch> batches = client.getBatches();
    // validate the content of events
    for (int i = 0; i < batches.size(); i++) {
        final SubscriptionCursor cursor = new SubscriptionCursor("0", TestUtils.toTimelineOffset(i), eventTypeName, "");
        final StreamBatch expectedBatch = new StreamBatch(cursor, ImmutableList.of(ImmutableMap.of("foo", "bar" + i)), i == 0 ? new StreamMetadata("Stream started") : null);
        final StreamBatch batch = batches.get(i);
        assertThat(batch, equalToBatchIgnoringToken(expectedBatch));
    }
    // as we didn't commit, there should be still 4 unconsumed events
    jsonRequestSpec().get("/subscriptions/{sid}/stats", subscription.getId()).then().statusCode(OK.value()).body("items[0].partitions[0].unconsumed_events", equalTo(4));
    // commit cursor of latest event
    final StreamBatch lastBatch = batches.get(batches.size() - 1);
    final int commitCode = commitCursors(jsonRequestSpec(), subscription.getId(), ImmutableList.of(lastBatch.getCursor()), client.getSessionId());
    assertThat(commitCode, equalTo(NO_CONTENT.value()));
    // now there should be 0 unconsumed events
    jsonRequestSpec().get("/subscriptions/{sid}/stats", subscription.getId()).then().statusCode(OK.value()).body("items[0].partitions[0].unconsumed_events", equalTo(0));
    // get cursors
    jsonRequestSpec().get("/subscriptions/{sid}/cursors", subscription.getId()).then().statusCode(OK.value()).body("items[0].partition", equalTo("0")).body("items[0].offset", equalTo("001-0001-000000000000000003"));
    // delete subscription
    jsonRequestSpec().delete("/subscriptions/{sid}", subscription.getId()).then().statusCode(NO_CONTENT.value());
}
Also used : TestUtils.randomTextString(org.zalando.nakadi.utils.TestUtils.randomTextString) CREATED(org.springframework.http.HttpStatus.CREATED) RequestSpecification(com.jayway.restassured.specification.RequestSpecification) IntStream.rangeClosed(java.util.stream.IntStream.rangeClosed) NOT_FOUND(org.springframework.http.HttpStatus.NOT_FOUND) RandomSubscriptionBuilder(org.zalando.nakadi.utils.RandomSubscriptionBuilder) NO_CONTENT(org.springframework.http.HttpStatus.NO_CONTENT) BEGIN(org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Subscription(org.zalando.nakadi.domain.Subscription) Retryer.executeWithRetry(org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry) JsonConfig(org.zalando.nakadi.config.JsonConfig) ImmutableList(com.google.common.collect.ImmutableList) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) RetryForSpecifiedTimeStrategy(org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) EventType(org.zalando.nakadi.domain.EventType) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) ImmutableMap(com.google.common.collect.ImmutableMap) Resources(com.google.common.io.Resources) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Matchers(org.hamcrest.Matchers) TestUtils.waitFor(org.zalando.nakadi.utils.TestUtils.waitFor) Test(org.junit.Test) IOException(java.io.IOException) JSON(com.jayway.restassured.http.ContentType.JSON) StreamMetadata(org.zalando.nakadi.domain.StreamMetadata) TestUtils(org.zalando.nakadi.utils.TestUtils) Collectors(java.util.stream.Collectors) NakadiTestUtils.commitCursors(org.zalando.nakadi.webservice.utils.NakadiTestUtils.commitCursors) MatcherIgnoringToken.equalToBatchIgnoringToken(org.zalando.nakadi.webservice.hila.StreamBatch.MatcherIgnoringToken.equalToBatchIgnoringToken) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Header(com.jayway.restassured.response.Header) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) RestAssured(com.jayway.restassured.RestAssured) OK(org.springframework.http.HttpStatus.OK) TestUtils.randomValidEventTypeName(org.zalando.nakadi.utils.TestUtils.randomValidEventTypeName) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) StreamMetadata(org.zalando.nakadi.domain.StreamMetadata) TestUtils.randomTextString(org.zalando.nakadi.utils.TestUtils.randomTextString) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Test(org.junit.Test)

Example 2 with TestStreamingClient

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

the class HilaRebalanceAT method whenTwoStreamsDirectlyRequestOnePartitionThenConflict.

@Test(timeout = 15000)
public void whenTwoStreamsDirectlyRequestOnePartitionThenConflict() throws IOException, InterruptedException {
    // first stream wants to read partition 6
    final TestStreamingClient client1 = new TestStreamingClient(URL, subscription.getId(), "", Optional.empty(), Optional.of("{\"partitions\":[" + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"6\"}" + "]}"));
    client1.start();
    waitFor(() -> assertThat(client1.getResponseCode(), is(HttpStatus.OK.value())));
    // second stream wants to read partitions 5 and 6
    final TestStreamingClient client2 = new TestStreamingClient(URL, subscription.getId(), "", Optional.empty(), Optional.of("{\"partitions\":[" + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"5\"}," + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"6\"}" + "]}"));
    client2.start();
    // check that second client was disconnected and received status code 409
    waitFor(() -> assertThat(client2.isRunning(), is(false)));
    assertThat(client2.getResponseCode(), is(HttpStatus.CONFLICT.value()));
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Test(org.junit.Test)

Example 3 with TestStreamingClient

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

the class HilaRebalanceAT method whenMixedStreamsThenPartitionsAssignedCorrectly.

@Test(timeout = 15000)
public void whenMixedStreamsThenPartitionsAssignedCorrectly() throws IOException, InterruptedException {
    // start 2 streams not specifying partitions directly
    final TestStreamingClient autoClient1 = new TestStreamingClient(URL, subscription.getId(), "");
    autoClient1.start();
    final TestStreamingClient autoClient2 = new TestStreamingClient(URL, subscription.getId(), "");
    autoClient2.start();
    // start a stream requesting to read from partition 6
    final TestStreamingClient directClient1 = new TestStreamingClient(URL, subscription.getId(), "", Optional.empty(), Optional.of("{\"partitions\":[" + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"6\"}]}"));
    directClient1.start();
    // start a stream requesting to read from partition 7
    final TestStreamingClient directClient2 = new TestStreamingClient(URL, subscription.getId(), "", Optional.empty(), Optional.of("{\"partitions\":[" + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"7\"}]}"));
    directClient2.start();
    // wait for rebalance to finish
    waitFor(() -> assertThat(getNumberOfAssignedStreams(subscription.getId()), is(4)));
    // publish 2 events to each partition
    publishBusinessEventWithUserDefinedPartition(eventType.getName(), 16, x -> "blah" + x, x -> String.valueOf(x % 8));
    // we should receive 2 batches for streams that directly requested 1 partition
    waitFor(() -> assertThat(directClient1.getBatches(), hasSize(2)));
    checkAllEventsAreFromPartitions(directClient1, ImmutableSet.of("6"));
    waitFor(() -> assertThat(directClient2.getBatches(), hasSize(2)));
    checkAllEventsAreFromPartitions(directClient2, ImmutableSet.of("7"));
    // we should receive 6 batches for streams that use auto balance (they read 3 partitions each)
    waitFor(() -> assertThat(autoClient1.getBatches(), hasSize(6)));
    waitFor(() -> assertThat(autoClient2.getBatches(), hasSize(6)));
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Test(org.junit.Test)

Example 4 with TestStreamingClient

use of org.zalando.nakadi.webservice.utils.TestStreamingClient 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 5 with TestStreamingClient

use of org.zalando.nakadi.webservice.utils.TestStreamingClient 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)

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