package com.ankurm.cloudstream; import static org.assertj.core.api.Assertions.assertThat; import java.time.Duration; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.WebApplicationType; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.ConfigurableApplicationContext; /** * The same pipeline on RabbitMQ. Rabbit has no partitions inside a queue: the binder fakes them with one queue per * partition (orders.pricing-0, -1, -2) and each consumer instance owns exactly one of them. */ @ExtendWith(OutputCaptureExtension.class) class Test04RabbitPipelineTest { private static ConfigurableApplicationContext start(String suffix, int index, String definition, String... extra) { return new SpringApplicationBuilder(CloudStreamApplication.class).web(WebApplicationType.NONE) .run(Brokers.concat(new String[] {"--spring.profiles.active=rabbit", "--demo.max-orders=12", "--spring.cloud.function.definition=" + definition, "--spring.cloud.stream.bindings.priceOrder-in-0.consumer.instance-index=" + index}, Test02KafkaPipelineTest.prefixed(Brokers.rabbitProps()), Test02KafkaPipelineTest.prefixed(Brokers.suffixed(suffix)), extra)); } @Test void oneInstanceOwnsOneQueue(CapturedOutput output) throws Exception { try (Transcript t = new Transcript("06-rabbit-partitions")) { t.line("$ profile rabbit, 12 orders: alice, bob, carol, dave x 3, partition-count=3, consumer instance-count=3 instance-index=0"); // Run A: the producer declares only the exchange, so a partition with no queue yet is dropped. int before = Test01TestBinderTest.count(output, "billed o-"); try (ConfigurableApplicationContext a = start("r4a", 0, "orderSource;priceOrder;bill")) { Await.until(Duration.ofSeconds(20), () -> Test01TestBinderTest.count(output, "billed o-") - before >= 6); Thread.sleep(3000); t.line(""); t.line("run A, no required-groups on the orders producer: billed %d of 12", Test01TestBinderTest.count(output, "billed o-") - before); t.line("queues on the broker (name, messages, consumers):"); Brokers.listQueues("orders-r4a").lines().forEach(l -> t.line(" %s", l)); assertThat(Test01TestBinderTest.count(output, "billed o-") - before).isEqualTo(6); } // Run B: required-groups makes the producer declare every partition queue up front. int beforeB = Test01TestBinderTest.count(output, "billed o-"); List extras = new ArrayList<>(); try (ConfigurableApplicationContext b = start("r4b", 0, "orderSource;priceOrder;bill", "--spring.cloud.stream.bindings.orderSource-out-0.producer.required-groups=pricing")) { Await.until(Duration.ofSeconds(20), () -> Test01TestBinderTest.count(output, "billed o-") - beforeB >= 6); Thread.sleep(3000); t.line(""); t.line("run B, required-groups=pricing on the orders producer: billed %d of 12", Test01TestBinderTest.count(output, "billed o-") - beforeB); t.line("queues on the broker (name, messages, consumers):"); Brokers.listQueues("orders-r4b").lines().forEach(l -> t.line(" %s", l)); assertThat(Brokers.depth("orders-r4b.pricing-1")).isEqualTo(3); assertThat(Brokers.depth("orders-r4b.pricing-2")).isEqualTo(3); // Run C: two more instances, one per remaining partition queue. extras.add(start("r4b", 1, "priceOrder")); extras.add(start("r4b", 2, "priceOrder")); Await.until(Duration.ofSeconds(30), () -> Test01TestBinderTest.count(output, "billed o-") - beforeB == 12); Thread.sleep(2000); t.line(""); t.line("run C, plus instance-index 1 and 2: billed %d of 12", Test01TestBinderTest.count(output, "billed o-") - beforeB); t.line("queues on the broker (name, messages, consumers):"); Brokers.listQueues("orders-r4b").lines().forEach(l -> t.line(" %s", l)); assertThat(Test01TestBinderTest.count(output, "billed o-") - beforeB).isEqualTo(12); } finally { extras.forEach(ConfigurableApplicationContext::close); } } } }