29
29
import org .apache .pulsar .client .api .PulsarClient ;
30
30
import org .apache .pulsar .client .api .PulsarClientException ;
31
31
import org .apache .pulsar .client .api .Schema ;
32
+ import org .assertj .core .api .InstanceOfAssertFactories ;
32
33
import org .junit .jupiter .api .AfterEach ;
33
34
import org .junit .jupiter .api .BeforeEach ;
34
35
import org .junit .jupiter .api .Test ;
@@ -78,13 +79,13 @@ void cleanupFromTest() throws PulsarClientException {
78
79
void whenConditionIsSpecifiedThenMessagesConsumedUntilConditionMet () {
79
80
var topic = testTopic ("cond" );
80
81
IntStream .range (0 , 5 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
81
- var msgs = PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
82
+ var consumerTestUtil = PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
82
83
.fromTopic (topic )
83
84
.withSchema (Schema .STRING )
84
85
.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 );
88
89
}
89
90
90
91
@ Test
@@ -132,17 +133,18 @@ void whenConditionNotMetWithinAwaitDurationThenExceptionIsThrown() {
132
133
void consumeMessagesWithNoArgsUsesPulsarContainerIfAvailable () {
133
134
var topic = testTopic ("no-arg" );
134
135
IntStream .range (0 , 2 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
135
- var msgs = PulsarConsumerTestUtil .<String >consumeMessages ()
136
+ var consumerTestUtil = PulsarConsumerTestUtil .<String >consumeMessages ()
136
137
.fromTopic (topic )
137
138
.withSchema (Schema .STRING )
138
139
.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 );
142
143
}
143
144
144
145
@ Test
145
146
void consumeMessagesWithNoArgsUsesDefaultUrlWhenPulsarContainerNotAvailable () {
147
+ // @formatter::off
146
148
try (MockedStatic <PulsarTestContainerSupport > containerSupport = Mockito
147
149
.mockStatic (PulsarTestContainerSupport .class )) {
148
150
containerSupport .when (PulsarTestContainerSupport ::isContainerStarted ).thenReturn (false );
@@ -155,32 +157,46 @@ void consumeMessagesWithNoArgsUsesDefaultUrlWhenPulsarContainerNotAvailable() {
155
157
.get ())
156
158
.withStackTraceContaining ("Connection refused: localhost" );
157
159
}
160
+ // @formatter:on
158
161
}
159
162
160
163
@ Test
161
164
void consumeMessagesWithBrokerUrl () {
162
165
var topic = testTopic ("url-arg" );
163
166
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 ())
165
169
.fromTopic (topic )
166
170
.withSchema (Schema .STRING )
167
171
.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 );
171
175
}
172
176
173
177
@ Test
174
178
void consumeMessagesWithPulsarClient () {
175
179
var topic = testTopic ("client-arg" );
176
180
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 )
178
182
.fromTopic (topic )
179
183
.withSchema (Schema .STRING )
180
184
.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 );
184
200
}
185
201
186
202
@ Test
0 commit comments