Search in sources :

Example 11 with TestStreamingClient

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

the class HilaAT method whenNoEventsThenFirstOffsetIsBEGIN.

@Test(timeout = 5000)
public void whenNoEventsThenFirstOffsetIsBEGIN() {
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "batch_flush_timeout=1").start();
    waitFor(() -> assertThat(client.getBatches(), not(empty())));
    assertThat(client.getBatches().get(0).getCursor().getOffset(), equalTo("001-0001--1"));
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Test(org.junit.Test)

Example 12 with TestStreamingClient

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

the class HilaAT method whenOffsetIsCommittedNextSessionStartsFromNextEventAfterCommitted.

@Test(timeout = 30000)
public void whenOffsetIsCommittedNextSessionStartsFromNextEventAfterCommitted() throws Exception {
    // write 4 events to event-type
    publishEvents(eventType.getName(), 4, x -> "{\"foo\":\"bar" + x + "\"}");
    // create session, read from subscription and wait for events to be sent
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "stream_limit=2").start();
    waitFor(() -> assertThat(client.getBatches(), hasSize(2)));
    assertThat(client.getBatches().get(0), equalToBatchIgnoringToken(singleEventBatch("0", "001-0001-000000000000000000", eventType.getName(), ImmutableMap.of("foo", "bar0"), "Stream started")));
    assertThat(client.getBatches().get(1), equalToBatchIgnoringToken(singleEventBatch("0", "001-0001-000000000000000001", eventType.getName(), ImmutableMap.of("foo", "bar1"))));
    // commit offset that will also trigger session closing as we reached stream_limit and committed
    commitCursors(subscription.getId(), ImmutableList.of(client.getBatches().get(1).getCursor()), client.getSessionId());
    waitFor(() -> assertThat(client.isRunning(), is(false)));
    // create new session and read from subscription again
    client.start();
    waitFor(() -> assertThat(client.getBatches(), hasSize(2)));
    // check that we have read the next two events with correct offsets
    assertThat(client.getBatches().get(0), equalToBatchIgnoringToken(singleEventBatch("0", "001-0001-000000000000000002", eventType.getName(), ImmutableMap.of("foo", "bar2"), "Stream started")));
    assertThat(client.getBatches().get(1), equalToBatchIgnoringToken(singleEventBatch("0", "001-0001-000000000000000003", eventType.getName(), ImmutableMap.of("foo", "bar3"))));
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Test(org.junit.Test)

Example 13 with TestStreamingClient

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

the class HilaAT method testSubscriptionStatsMultiET.

@Test
public void testSubscriptionStatsMultiET() throws IOException {
    final List<EventType> eventTypes = Lists.newArrayList(createEventType(), createEventType());
    publishEvents(eventTypes.get(0).getName(), 10, i -> "{\"foo\":\"bar\"}");
    publishEvents(eventTypes.get(1).getName(), 20, i -> "{\"foo\":\"bar\"}");
    final Subscription subscription = NakadiTestUtils.createSubscription(RandomSubscriptionBuilder.builder().withEventTypes(eventTypes.stream().map(EventType::getName).collect(Collectors.toSet())).withStartFrom(END).build());
    // client is needed only to initialize stats
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "batch_flush_timeout=1").start();
    waitFor(() -> assertThat(client.getBatches().isEmpty(), is(false)));
    publishEvents(eventTypes.get(0).getName(), 1, i -> "{\"foo\":\"bar\"}");
    publishEvents(eventTypes.get(1).getName(), 2, i -> "{\"foo\":\"bar\"}");
    NakadiTestUtils.getSubscriptionStat(subscription).then().content(new StringContains(JSON_TEST_HELPER.asJsonString(new SubscriptionEventTypeStats(eventTypes.get(0).getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 1L, client.getSessionId(), AUTO)))))).content(new StringContains(JSON_TEST_HELPER.asJsonString(new SubscriptionEventTypeStats(eventTypes.get(1).getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 2L, client.getSessionId(), AUTO))))));
    client.close();
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) NakadiTestUtils.createEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType) EventType(org.zalando.nakadi.domain.EventType) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) StringContains(org.hamcrest.core.StringContains) Test(org.junit.Test)

Example 14 with TestStreamingClient

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

the class HilaAT method whenCommitVeryFirstEventThenOk.

@Test(timeout = 5000)
public void whenCommitVeryFirstEventThenOk() throws Exception {
    publishEvent(eventType.getName(), "{\"foo\":\"bar\"}");
    // create session, read from subscription and wait for events to be sent
    final TestStreamingClient client = TestStreamingClient.create(subscription.getId()).start();
    waitFor(() -> assertThat(client.getBatches(), not(empty())));
    // commit and check that status is 204
    final int commitResult = commitCursors(subscription.getId(), ImmutableList.of(new SubscriptionCursor("0", "0", eventType.getName(), "token")), client.getSessionId());
    assertThat(commitResult, equalTo(SC_NO_CONTENT));
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Test(org.junit.Test)

Example 15 with TestStreamingClient

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

the class HilaAT method testGetSubscriptionStatWhenDirectAssignment.

@Test(timeout = 10000)
public void testGetSubscriptionStatWhenDirectAssignment() throws Exception {
    // connect with 1 stream directly requesting the partition
    final TestStreamingClient client = new TestStreamingClient(URL, subscription.getId(), "", Optional.empty(), Optional.of("{\"partitions\":[" + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"0\"}]}"));
    client.start();
    // wait for rebalance to finish
    waitFor(() -> assertThat(getNumberOfAssignedStreams(subscription.getId()), Matchers.is(1)));
    NakadiTestUtils.getSubscriptionStat(subscription).then().content(new StringContains(JSON_TEST_HELPER.asJsonString(new SubscriptionEventTypeStats(eventType.getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 0L, client.getSessionId(), DIRECT))))));
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) StringContains(org.hamcrest.core.StringContains) 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