Wait

Async coordination

KafkaTestListeners handles listener startup readiness; KafkaTestLatch and KafkaTestAwait remain available for tests that still wait on a listener side effect.

Prefer listener readiness when the race is "did I publish before the listener was assigned partitions?" Use latches when the test needs to wait for an application side effect after the record is consumed.

When two listeners on the same topic must both handle each record, give them distinct consumer groups. Listeners in the same consumer group compete for partitions; on a one-partition test topic, waiting for both to be assigned will correctly time out.

KafkaTestListeners

Package: io.typecheck.wiretap.core

PropertyDefaultDescription
awaitAllReady()—Waits for all discovered listener containers to be running and assigned.
awaitListenerReady(id)—Waits for one named listener container.
awaitListenersForTopic(topic)—Waits for listeners subscribed to a topic.
assertThatTopic(topic).hasReadyListeners(...)—Topic assertion shortcut for listener readiness before publishing.
Java
assertThatTopic("orders.in")
    .hasReadyListeners("orderRouter");

KafkaTestPublisher.send("orders.in", "ord-7782", order);

KafkaTestLatch

Package: io.typecheck.wiretap.core

PropertyDefaultDescription
latch()—Creates a single-use latch with count 1.
latch(int)—Creates a latch with an explicit count.
await()—Waits using the default timeout of 10 seconds.
await(description)—Same default timeout, with a clearer failure description.
await(timeout)—Waits using a custom timeout.
await(timeout, description)—Full control over timeout and failure description.
asCountDownLatch()—Exposes the underlying latch for APIs that require CountDownLatch.
Java
KafkaTestLatch latch = KafkaTestLatch.latch();

@KafkaListener(topics = "orders-topic")
void onMessage(OrderEvent event) {
    receivedEvents.add(event);
    latch.countDown();
}

KafkaTestPublisher.send("orders-topic", order.getOrderId(), order);
latch.await(Duration.ofSeconds(5), "orders listener");

KafkaTestAwait

Use KafkaTestAwait when you already have a raw CountDownLatch and want the same assertion-style timeout failures.

PropertyDefaultDescription
latch()—Creates a CountDownLatch with count 1.
latch(int)—Creates a CountDownLatch with an explicit count.
await(latch)—Waits using the default timeout of 10 seconds.
await(latch, description)—Same default timeout, with a clearer failure description.
await(latch, timeout)—Waits using a custom timeout.
await(latch, timeout, description)—Full control over timeout and failure description.

Why use it

  • keeps latch-based tests compact
  • produces an assertion-style timeout message with the remaining count
  • restores the interrupt flag if the wait is interrupted

When to use it

Use KafkaTestLatch when your test owns the latch. Use KafkaTestAwait when a test or library already gives you a raw CountDownLatch.