use of org.apache.kafka.test.TestUtils.waitForCondition in project kafka by apache.
the class AdjustStreamThreadCountTest method shouldAddAndRemoveStreamThreadsWhileKeepingNamesCorrect.
@Test
public void shouldAddAndRemoveStreamThreadsWhileKeepingNamesCorrect() throws Exception {
try (final KafkaStreams kafkaStreams = new KafkaStreams(builder.build(), properties)) {
addStreamStateChangeListener(kafkaStreams);
startStreamsAndWaitForRunning(kafkaStreams);
int oldThreadCount = kafkaStreams.metadataForLocalThreads().size();
stateTransitionHistory.clear();
assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2" }));
final Optional<String> name = kafkaStreams.addStreamThread();
assertThat("New thread has index 3", "3".equals(name.get().split("-StreamThread-")[1]));
TestUtils.waitForCondition(() -> kafkaStreams.metadataForLocalThreads().stream().sequential().map(ThreadMetadata::threadName).anyMatch(t -> t.equals(name.get())), "Stream thread has not been added");
assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount + 1));
assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2", "3" }));
waitForTransitionFromRebalancingToRunning();
oldThreadCount = kafkaStreams.metadataForLocalThreads().size();
stateTransitionHistory.clear();
final Optional<String> removedThread = kafkaStreams.removeStreamThread();
assertThat(removedThread, not(Optional.empty()));
assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount - 1));
waitForTransitionFromRebalancingToRunning();
stateTransitionHistory.clear();
final Optional<String> name2 = kafkaStreams.addStreamThread();
assertThat(name2, not(Optional.empty()));
TestUtils.waitForCondition(() -> kafkaStreams.metadataForLocalThreads().stream().sequential().map(ThreadMetadata::threadName).anyMatch(t -> t.equals(name2.orElse(""))), "Wait for the thread to be added");
assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount));
assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2", "3" }));
assertThat("the new thread should have received the old threads name", name2.equals(removedThread));
waitForTransitionFromRebalancingToRunning();
}
}
use of org.apache.kafka.test.TestUtils.waitForCondition in project kafka by apache.
the class AdjustStreamThreadCountTest method shouldAddStreamThread.
@Test
public void shouldAddStreamThread() throws Exception {
try (final KafkaStreams kafkaStreams = new KafkaStreams(builder.build(), properties)) {
addStreamStateChangeListener(kafkaStreams);
startStreamsAndWaitForRunning(kafkaStreams);
final int oldThreadCount = kafkaStreams.metadataForLocalThreads().size();
assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2" }));
stateTransitionHistory.clear();
final Optional<String> name = kafkaStreams.addStreamThread();
assertThat(name, not(Optional.empty()));
TestUtils.waitForCondition(() -> kafkaStreams.metadataForLocalThreads().stream().sequential().map(ThreadMetadata::threadName).anyMatch(t -> t.equals(name.orElse(""))), "Wait for the thread to be added");
assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount + 1));
assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2", "3" }));
waitForTransitionFromRebalancingToRunning();
}
}
Aggregations