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