Search in sources :

Example 1 with Topic

use of io.zeebe.client.topic.Topic in project zeebe by zeebe-io.

the class TopicSubscriptionTest method testSubscriptionsWithSameNameOnDifferentTopicShouldReceiveRespectiveEvents.

@Test
public void testSubscriptionsWithSameNameOnDifferentTopicShouldReceiveRespectiveEvents() {
    // given
    final String anotherTopicName = "another-topic";
    final TopicEventImpl event = (TopicEventImpl) client.topics().create(anotherTopicName, 1).execute();
    final int anotherPartitionId = 2;
    final TopicSubscription topic0subscription = client.topics().newSubscription(clientRule.getDefaultTopic()).startAtHeadOfTopic().handler(recordingHandler).name(SUBSCRIPTION_NAME).open();
    final RecordingEventHandler anotherRecordingHandler = new RecordingEventHandler();
    final TopicSubscription topic1Subscription = client.topics().newSubscription(anotherTopicName).startAtHeadOfTopic().handler(anotherRecordingHandler).name(SUBSCRIPTION_NAME).open();
    // when
    clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").execute();
    clientRule.tasks().create(anotherTopicName, "bar").execute();
    // then
    waitUntil(() -> recordingHandler.numRecordedTaskEvents() >= 2);
    waitUntil(() -> anotherRecordingHandler.numRecordedTaskEvents() >= 2);
    topic0subscription.close();
    topic1Subscription.close();
    Set<String> receivedTopicNamesSubscription = recordingHandler.getRecordedEvents().stream().filter((re) -> re.getMetadata().getType() == TopicEventType.TASK).map((re) -> re.getMetadata().getTopicName()).collect(Collectors.toSet());
    Set<Integer> receivedPartitionIdsSubscription = recordingHandler.getRecordedEvents().stream().filter((re) -> re.getMetadata().getType() == TopicEventType.TASK).map((re) -> re.getMetadata().getPartitionId()).collect(Collectors.toSet());
    assertThat(receivedTopicNamesSubscription).containsExactly(clientRule.getDefaultTopic());
    assertThat(receivedPartitionIdsSubscription).containsExactly(clientRule.getDefaultPartition());
    receivedTopicNamesSubscription = anotherRecordingHandler.getRecordedEvents().stream().filter((re) -> re.getMetadata().getType() == TopicEventType.TASK).map((re) -> re.getMetadata().getTopicName()).collect(Collectors.toSet());
    receivedPartitionIdsSubscription = anotherRecordingHandler.getRecordedEvents().stream().filter((re) -> re.getMetadata().getType() == TopicEventType.TASK).map((re) -> re.getMetadata().getPartitionId()).collect(Collectors.toSet());
    assertThat(receivedTopicNamesSubscription).containsExactly(anotherTopicName);
    assertThat(receivedPartitionIdsSubscription).containsExactly(anotherPartitionId);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) ZeebeClient(io.zeebe.client.ZeebeClient) TopicEventImpl(io.zeebe.client.topic.impl.TopicEventImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Topic(io.zeebe.client.topic.Topic) EmbeddedBrokerRule(io.zeebe.broker.it.EmbeddedBrokerRule) Timeout(org.junit.rules.Timeout) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) RuleChain(org.junit.rules.RuleChain) List(java.util.List) ClientRule(io.zeebe.broker.it.ClientRule) Rule(org.junit.Rule) CreateTaskCommandImpl(io.zeebe.client.task.impl.CreateTaskCommandImpl) ClientProperties(io.zeebe.client.ClientProperties) Topics(io.zeebe.client.topic.Topics) io.zeebe.client.event(io.zeebe.client.event) TestUtil(io.zeebe.test.util.TestUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TopicEventImpl(io.zeebe.client.topic.impl.TopicEventImpl) Test(org.junit.Test)

Example 2 with Topic

use of io.zeebe.client.topic.Topic in project zeebe by zeebe-io.

the class CreateTopicClusteredTest method shouldCreateTopic.

@Test
public void shouldCreateTopic() {
    // given
    // when
    final Topic topic = clusteringRule.createTopic("foo", PARTITION_COUNT);
    // then
    assertThat(topic.getName()).isEqualTo("foo");
    assertThat(topic.getPartitions().size()).isEqualTo(PARTITION_COUNT);
}
Also used : Topic(io.zeebe.client.topic.Topic) Test(org.junit.Test)

Example 3 with Topic

use of io.zeebe.client.topic.Topic in project zeebe by zeebe-io.

the class CreateTopicClusteredTest method shouldRequestTopicsAfterTopicCreation.

@Test
public void shouldRequestTopicsAfterTopicCreation() {
    // given
    clusteringRule.createTopic("foo", PARTITION_COUNT);
    // when
    final Topics topicsResponse = doRepeatedly(() -> client.topics().getTopics().execute()).until((topics -> topics.getTopics().size() == 1));
    final List<Topic> topics = topicsResponse.getTopics();
    // then
    assertThat(topics.size()).isEqualTo(1);
    assertThat(topics.get(0).getName()).isEqualTo("foo");
    final List<Integer> partitions = topics.get(0).getPartitions().stream().map((partition -> partition.getId())).collect(Collectors.toList());
    assertThat(partitions.size()).isEqualTo(5);
    assertThat(partitions).containsExactlyInAnyOrder(1, 2, 3, 4, 5);
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) TopologyBroker(io.zeebe.client.clustering.impl.TopologyBroker) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Collectors(java.util.stream.Collectors) TestUtil.doRepeatedly(io.zeebe.test.util.TestUtil.doRepeatedly) ZeebeClient(io.zeebe.client.ZeebeClient) RuleChain(org.junit.rules.RuleChain) ClientRule(io.zeebe.broker.it.ClientRule) List(java.util.List) SocketAddress(io.zeebe.transport.SocketAddress) Rule(org.junit.Rule) Topic(io.zeebe.client.topic.Topic) Timeout(org.junit.rules.Timeout) Topics(io.zeebe.client.topic.Topics) AutoCloseableRule(io.zeebe.test.util.AutoCloseableRule) Before(org.junit.Before) Topics(io.zeebe.client.topic.Topics) Topic(io.zeebe.client.topic.Topic) Test(org.junit.Test)

Example 4 with Topic

use of io.zeebe.client.topic.Topic in project zeebe by zeebe-io.

the class TaskEventClusteredTest method shouldCreateTaskWhenFollowerUnavailable.

@Test
public void shouldCreateTaskWhenFollowerUnavailable() {
    // given
    final ZeebeClient client = clientRule.getClient();
    final String topicName = "foo";
    clusteringRule.createTopic(topicName, 1);
    final Topics topics = client.topics().getTopics().execute();
    final Topic topic = topics.getTopics().stream().filter(t -> topicName.equals(t.getName())).findFirst().get();
    final TopologyBroker leader = clusteringRule.getLeaderForPartition(topic.getPartitions().get(0).getId());
    // choosing a new leader in a raft group where the previously leading broker is no longer available
    clusteringRule.stopBroker(leader.getSocketAddress());
    // when
    final TaskEvent taskEvent = client.tasks().create(topicName, "bar").execute();
    // then
    assertThat(taskEvent.getState()).isEqualTo("CREATED");
}
Also used : Topics(io.zeebe.client.topic.Topics) ZeebeClient(io.zeebe.client.ZeebeClient) TaskEvent(io.zeebe.client.event.TaskEvent) Topic(io.zeebe.client.topic.Topic) TopologyBroker(io.zeebe.client.clustering.impl.TopologyBroker) Test(org.junit.Test)

Example 5 with Topic

use of io.zeebe.client.topic.Topic in project zeebe by zeebe-io.

the class TopicSubscriptionTest method shouldReceiveEventsFromMultiplePartitions.

@Test
public void shouldReceiveEventsFromMultiplePartitions() {
    // given
    final String topicName = "pasta al forno";
    final int numPartitions = 2;
    client.topics().create(topicName, numPartitions).execute();
    final Topics topics = client.topics().getTopics().execute();
    final Topic topic = topics.getTopics().stream().filter(t -> t.getName().equals(topicName)).findFirst().get();
    final Integer[] partitionIds = topic.getPartitions().stream().mapToInt(p -> p.getId()).boxed().toArray(Integer[]::new);
    createTaskOnPartition(topicName, partitionIds[0]);
    createTaskOnPartition(topicName, partitionIds[1]);
    final List<Integer> receivedPartitionIds = new ArrayList<>();
    // when
    client.topics().newSubscription(topicName).handler(recordingHandler).taskEventHandler(e -> {
        if ("CREATE".equals(e.getState())) {
            receivedPartitionIds.add(e.getMetadata().getPartitionId());
        }
    }).startAtHeadOfTopic().name("foo").open();
    // then
    waitUntil(() -> receivedPartitionIds.size() == numPartitions);
    assertThat(receivedPartitionIds).containsExactlyInAnyOrder(partitionIds);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) ZeebeClient(io.zeebe.client.ZeebeClient) TopicEventImpl(io.zeebe.client.topic.impl.TopicEventImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Topic(io.zeebe.client.topic.Topic) EmbeddedBrokerRule(io.zeebe.broker.it.EmbeddedBrokerRule) Timeout(org.junit.rules.Timeout) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) RuleChain(org.junit.rules.RuleChain) List(java.util.List) ClientRule(io.zeebe.broker.it.ClientRule) Rule(org.junit.Rule) CreateTaskCommandImpl(io.zeebe.client.task.impl.CreateTaskCommandImpl) ClientProperties(io.zeebe.client.ClientProperties) Topics(io.zeebe.client.topic.Topics) io.zeebe.client.event(io.zeebe.client.event) TestUtil(io.zeebe.test.util.TestUtil) Topics(io.zeebe.client.topic.Topics) ArrayList(java.util.ArrayList) Topic(io.zeebe.client.topic.Topic) Test(org.junit.Test)

Aggregations

Topic (io.zeebe.client.topic.Topic)8 Test (org.junit.Test)8 ZeebeClient (io.zeebe.client.ZeebeClient)6 ClientRule (io.zeebe.broker.it.ClientRule)5 Topics (io.zeebe.client.topic.Topics)5 TestUtil.waitUntil (io.zeebe.test.util.TestUtil.waitUntil)5 List (java.util.List)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Rule (org.junit.Rule)5 CreateTaskCommandImpl (io.zeebe.client.task.impl.CreateTaskCommandImpl)4 Before (org.junit.Before)4 RuleChain (org.junit.rules.RuleChain)4 Timeout (org.junit.rules.Timeout)4 EmbeddedBrokerRule (io.zeebe.broker.it.EmbeddedBrokerRule)3 TaskEvent (io.zeebe.client.event.TaskEvent)3 TestUtil (io.zeebe.test.util.TestUtil)3 Duration (java.time.Duration)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 ExpectedException (org.junit.rules.ExpectedException)3