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());
}
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()));
}
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)));
}
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));
}
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()));
}
Aggregations