2929import org .apache .pulsar .client .api .PulsarClient ;
3030import org .apache .pulsar .client .api .PulsarClientException ;
3131import org .apache .pulsar .client .api .Schema ;
32+ import org .assertj .core .api .InstanceOfAssertFactories ;
3233import org .junit .jupiter .api .AfterEach ;
3334import org .junit .jupiter .api .BeforeEach ;
3435import org .junit .jupiter .api .Test ;
@@ -78,13 +79,13 @@ void cleanupFromTest() throws PulsarClientException {
7879 void whenConditionIsSpecifiedThenMessagesConsumedUntilConditionMet () {
7980 var topic = testTopic ("cond" );
8081 IntStream .range (0 , 5 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
81- var msgs = PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
82+ var consumerTestUtil = PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
8283 .fromTopic (topic )
8384 .withSchema (Schema .STRING )
8485 .awaitAtMost (Duration .ofSeconds (5 ))
85- .until (desiredMessageCount (3 ))
86- .get ();
87- assertThat ( msgs ). hasSize ( 3 );
86+ .until (desiredMessageCount (3 ));
87+ assertThat ( consumerTestUtil .get ()). hasSize ( 3 );
88+ assertThatLocallyCreatedClientIsNull ( consumerTestUtil );
8889 }
8990
9091 @ Test
@@ -132,17 +133,18 @@ void whenConditionNotMetWithinAwaitDurationThenExceptionIsThrown() {
132133 void consumeMessagesWithNoArgsUsesPulsarContainerIfAvailable () {
133134 var topic = testTopic ("no-arg" );
134135 IntStream .range (0 , 2 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
135- var msgs = PulsarConsumerTestUtil .<String >consumeMessages ()
136+ var consumerTestUtil = PulsarConsumerTestUtil .<String >consumeMessages ()
136137 .fromTopic (topic )
137138 .withSchema (Schema .STRING )
138139 .awaitAtMost (Duration .ofSeconds (5 ))
139- .until (desiredMessageCount (2 ))
140- .get ();
141- assertThat ( msgs ). hasSize ( 2 );
140+ .until (desiredMessageCount (2 ));
141+ assertThat ( consumerTestUtil .get ()). hasSize ( 2 );
142+ assertThatLocallyCreatedClientIsClosed ( consumerTestUtil );
142143 }
143144
144145 @ Test
145146 void consumeMessagesWithNoArgsUsesDefaultUrlWhenPulsarContainerNotAvailable () {
147+ // @formatter::off
146148 try (MockedStatic <PulsarTestContainerSupport > containerSupport = Mockito
147149 .mockStatic (PulsarTestContainerSupport .class )) {
148150 containerSupport .when (PulsarTestContainerSupport ::isContainerStarted ).thenReturn (false );
@@ -155,32 +157,46 @@ void consumeMessagesWithNoArgsUsesDefaultUrlWhenPulsarContainerNotAvailable() {
155157 .get ())
156158 .withStackTraceContaining ("Connection refused: localhost" );
157159 }
160+ // @formatter:on
158161 }
159162
160163 @ Test
161164 void consumeMessagesWithBrokerUrl () {
162165 var topic = testTopic ("url-arg" );
163166 IntStream .range (0 , 2 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
164- var msgs = PulsarConsumerTestUtil .<String >consumeMessages (PulsarTestContainerSupport .getPulsarBrokerUrl ())
167+ var consumerTestUtil = PulsarConsumerTestUtil
168+ .<String >consumeMessages (PulsarTestContainerSupport .getPulsarBrokerUrl ())
165169 .fromTopic (topic )
166170 .withSchema (Schema .STRING )
167171 .awaitAtMost (Duration .ofSeconds (5 ))
168- .until (desiredMessageCount (2 ))
169- .get ();
170- assertThat ( msgs ). hasSize ( 2 );
172+ .until (desiredMessageCount (2 ));
173+ assertThat ( consumerTestUtil .get ()). hasSize ( 2 );
174+ assertThatLocallyCreatedClientIsClosed ( consumerTestUtil );
171175 }
172176
173177 @ Test
174178 void consumeMessagesWithPulsarClient () {
175179 var topic = testTopic ("client-arg" );
176180 IntStream .range (0 , 2 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
177- var msgs = PulsarConsumerTestUtil .<String >consumeMessages (this .pulsarClient )
181+ var consumerTestUtil = PulsarConsumerTestUtil .<String >consumeMessages (this .pulsarClient )
178182 .fromTopic (topic )
179183 .withSchema (Schema .STRING )
180184 .awaitAtMost (Duration .ofSeconds (5 ))
181- .until (desiredMessageCount (2 ))
182- .get ();
183- assertThat (msgs ).hasSize (2 );
185+ .until (desiredMessageCount (2 ));
186+ assertThat (consumerTestUtil .get ()).hasSize (2 );
187+ assertThatLocallyCreatedClientIsNull (consumerTestUtil );
188+ }
189+
190+ private void assertThatLocallyCreatedClientIsNull (ConditionsSpec <?> consumerTestUtil ) {
191+ assertThat (consumerTestUtil ).extracting ("locallyCreatedPulsarClient" ).isNull ();
192+ }
193+
194+ private void assertThatLocallyCreatedClientIsClosed (ConditionsSpec <?> consumerTestUtil ) {
195+ assertThat (consumerTestUtil ).extracting ("locallyCreatedPulsarClient" )
196+ .isNotNull ()
197+ .asInstanceOf (InstanceOfAssertFactories .type (PulsarClient .class ))
198+ .extracting (PulsarClient ::isClosed )
199+ .isEqualTo (Boolean .TRUE );
184200 }
185201
186202 @ Test
0 commit comments