use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.
the class SubscriptionAT method testSubscriptionBaseOperations.
@Test
public void testSubscriptionBaseOperations() throws IOException {
// create event type in Nakadi
final EventType eventType = createEventType();
// create subscription
final String subscription = "{\"owning_application\":\"app\",\"event_types\":[\"" + eventType.getName() + "\"]}";
Response response = given().body(subscription).contentType(JSON).post(SUBSCRIPTIONS_URL);
// assert response
response.then().statusCode(HttpStatus.SC_CREATED).contentType(JSON).body("owning_application", equalTo("app")).body("event_types", containsInAnyOrder(ImmutableSet.of(eventType.getName()).toArray())).body("consumer_group", not(isEmptyString())).body("id", not(isEmptyString())).body("created_at", not(isEmptyString())).body("start_from", not(isEmptyString()));
// retrieve subscription object from response
final Subscription subFirst = MAPPER.readValue(response.print(), Subscription.class);
// when we try to create that subscription again - we should get status 200
// and the subscription that already exists should be returned
response = given().body(subscription).contentType(JSON).post(SUBSCRIPTIONS_URL);
// assert status code
response.then().statusCode(HttpStatus.SC_OK).contentType(JSON);
// check that second time already existing subscription was returned
final Subscription subSecond = MAPPER.readValue(response.print(), Subscription.class);
assertThat(subSecond, equalTo(subFirst));
// check get subscription endpoint
response = get(format(SUBSCRIPTION_URL, subFirst.getId()));
response.then().statusCode(HttpStatus.SC_OK).contentType(JSON);
final Subscription gotSubscription = MAPPER.readValue(response.print(), Subscription.class);
assertThat(gotSubscription, equalTo(subFirst));
}
use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.
the class SubscriptionAT method testSubscriptionWithManyEventTypesIsCreated.
@Test
public void testSubscriptionWithManyEventTypesIsCreated() throws IOException {
final List<String> eventTypes = IntStream.range(0, 10).mapToObj(i -> createEventType()).map(EventTypeBase::getName).collect(Collectors.toList());
final String subscription = "{\"owning_application\":\"app\",\"event_types\":" + "[" + eventTypes.stream().map(et -> "\"" + et + "\"").collect(Collectors.joining(",")) + "]}";
final Response response = given().body(subscription).contentType(JSON).post(SUBSCRIPTIONS_URL);
// assert response
response.then().statusCode(HttpStatus.SC_CREATED).contentType(JSON);
final Subscription gotSubscription = MAPPER.readValue(response.print(), Subscription.class);
Assert.assertNotNull(gotSubscription.getId());
}
use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.
the class SubscriptionConsumptionTest method test2TimelinesInaRowNoBegin.
@Test
public void test2TimelinesInaRowNoBegin() throws IOException, InterruptedException {
final EventType eventType = createEventType();
final Subscription subscription = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).build());
final CountDownLatch finished = new CountDownLatch(1);
final AtomicReference<String[]> inTimelineCursors = new AtomicReference<>();
createParallelConsumer(subscription, 2, finished, inTimelineCursors::set);
// Still old topic
createTimeline(eventType.getName());
// New topic
createTimeline(eventType.getName());
// Another new topic
createTimeline(eventType.getName());
publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
finished.await();
Assert.assertArrayEquals(new String[] { "001-0004-000000000000000000", "001-0004-000000000000000001" }, inTimelineCursors.get());
final Subscription subscription2 = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).withStartFrom(SubscriptionBase.InitialPosition.BEGIN).build());
final CountDownLatch finished2 = new CountDownLatch(1);
final AtomicReference<String[]> inTimelineCursors2 = new AtomicReference<>();
createParallelConsumer(subscription2, 2, finished2, inTimelineCursors2::set);
finished2.await();
Assert.assertArrayEquals(new String[] { "001-0004-000000000000000000", "001-0004-000000000000000001" }, inTimelineCursors2.get());
}
use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.
the class SubscriptionConsumptionTest method test2TimelinesInaRow.
@Test(timeout = 60000)
public void test2TimelinesInaRow() throws IOException, InterruptedException {
final EventType eventType = createEventType();
final Subscription subscription = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).build());
final CountDownLatch finished = new CountDownLatch(1);
final AtomicReference<String[]> inTimelineCursors = new AtomicReference<>();
createParallelConsumer(subscription, 5, finished, inTimelineCursors::set);
publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
// Still old topic
createTimeline(eventType.getName());
// New topic
createTimeline(eventType.getName());
// Another new topic
createTimeline(eventType.getName());
publishEvents(eventType.getName(), 1, i -> "{\"foo\":\"bar\"}");
createTimeline(eventType.getName());
createTimeline(eventType.getName());
publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
finished.await();
Assert.assertArrayEquals(new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0004-000000000000000000", "001-0006-000000000000000000", "001-0006-000000000000000001" }, inTimelineCursors.get());
// Will create subscription clone
final Subscription clone = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).withStartFrom(SubscriptionBase.InitialPosition.BEGIN).build());
final CountDownLatch finished2 = new CountDownLatch(1);
createParallelConsumer(clone, 5, finished2, inTimelineCursors::set);
finished2.await();
Assert.assertArrayEquals(new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0004-000000000000000000", "001-0006-000000000000000000", "001-0006-000000000000000001" }, inTimelineCursors.get());
}
use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.
the class CursorsService method commitCursors.
/**
* It is guaranteed, that len(cursors) == len(result)
*/
public List<Boolean> commitCursors(final String streamId, final String subscriptionId, final List<NakadiCursor> cursors) throws ServiceUnavailableException, InvalidCursorException, InvalidStreamIdException, NoSuchEventTypeException, InternalNakadiException, NoSuchSubscriptionException, UnableProcessException {
TimeLogger.addMeasure("getSubscription");
final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
TimeLogger.addMeasure("validateSubscriptionCursors");
validateSubscriptionCommitCursors(subscription, cursors);
TimeLogger.addMeasure("createSubscriptionClient");
final ZkSubscriptionClient zkClient = zkSubscriptionFactory.createClient(subscription, "subscription." + subscriptionId + "." + streamId + ".offsets");
TimeLogger.addMeasure("validateStreamId");
validateStreamId(cursors, streamId, zkClient);
TimeLogger.addMeasure("writeToZK");
return zkClient.commitOffsets(cursors.stream().map(cursorConverter::convertToNoToken).collect(Collectors.toList()), new SubscriptionCursorComparator(new NakadiCursorComparator(eventTypeCache)));
}
Aggregations