|
5 | 5 | package zipkin2.reporter.pulsar; |
6 | 6 |
|
7 | 7 | import org.apache.pulsar.client.api.Consumer; |
| 8 | +import org.apache.pulsar.client.api.Message; |
8 | 9 | import org.apache.pulsar.client.api.SubscriptionInitialPosition; |
9 | 10 | import org.junit.jupiter.api.BeforeEach; |
10 | 11 | import org.junit.jupiter.api.Tag; |
@@ -97,6 +98,28 @@ class ITPulsarSender { |
97 | 98 | } |
98 | 99 | } |
99 | 100 |
|
| 101 | + @Test void send_multiple_JSON_messages() throws Exception { |
| 102 | + try (PulsarSender sender = pulsar.newSenderBuilder(testName) |
| 103 | + .encoding(Encoding.JSON) |
| 104 | + .build()) { |
| 105 | + int size = 10; |
| 106 | + for (int i = 0; i < size; i++) { |
| 107 | + send(sender, CLIENT_SPAN); |
| 108 | + } |
| 109 | + for (int i = 0; i < size; i++) { |
| 110 | + assertThat(SpanBytesDecoder.JSON_V2.decodeList(readMessage(sender))) |
| 111 | + .hasSize(1).containsExactly(CLIENT_SPAN); |
| 112 | + } |
| 113 | + |
| 114 | + send(sender, CLIENT_SPAN); |
| 115 | + send(sender, CLIENT_SPAN, CLIENT_SPAN); |
| 116 | + assertThat(SpanBytesDecoder.JSON_V2.decodeList(readMessage(sender))) |
| 117 | + .hasSize(1).containsExactly(CLIENT_SPAN); |
| 118 | + assertThat(SpanBytesDecoder.JSON_V2.decodeList(readMessage(sender))) |
| 119 | + .hasSize(2).containsExactly(CLIENT_SPAN, CLIENT_SPAN); |
| 120 | + } |
| 121 | + } |
| 122 | + |
100 | 123 | @Test void illegalToSendWhenClosed() throws IOException { |
101 | 124 | try (PulsarSender sender = pulsar.newSenderBuilder(testName).build()) { |
102 | 125 | sender.close(); |
@@ -144,27 +167,34 @@ byte[] readMessage(PulsarSender sender) throws Exception { |
144 | 167 | final CountDownLatch countDown = new CountDownLatch(1); |
145 | 168 | final AtomicReference<byte[]> result = new AtomicReference<>(); |
146 | 169 |
|
147 | | - try (Consumer<byte[]> ignored = sender.client.newConsumer() |
| 170 | + Consumer<byte[]> consumer = null; |
| 171 | + Message<byte[]> message = null; |
| 172 | + try { |
| 173 | + consumer = sender.client.newConsumer() |
148 | 174 | .topic(sender.topic) |
149 | 175 | .subscriptionName("zipkin-subscription") |
150 | 176 | .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest) |
151 | | - .messageListener((consumer, message) -> { |
152 | | - try { |
153 | | - result.set(message.getData()); |
154 | | - countDown.countDown(); |
155 | | - consumer.acknowledge(message); |
156 | | - } catch (Exception e) { |
157 | | - consumer.negativeAcknowledge(message); |
158 | | - } |
159 | | - }).subscribe()) { |
160 | | - |
161 | | - assertThat(countDown.await(10, TimeUnit.SECONDS)) |
162 | | - .withFailMessage("Timed out waiting to read message.") |
163 | | - .isTrue(); |
164 | | - assertThat(result) |
165 | | - .withFailMessage("Message data is null in Pulsar consumer.") |
166 | | - .isNotNull(); |
167 | | - return result.get(); |
| 177 | + .subscribe(); |
| 178 | + message = consumer.receive(10, TimeUnit.SECONDS); |
| 179 | + result.set(message.getData()); |
| 180 | + countDown.countDown(); |
| 181 | + consumer.acknowledge(message); |
| 182 | + } catch (Exception e) { |
| 183 | + if (consumer != null) { |
| 184 | + consumer.negativeAcknowledge(message); |
| 185 | + } |
| 186 | + } finally { |
| 187 | + if (consumer != null) { |
| 188 | + consumer.close(); |
| 189 | + } |
168 | 190 | } |
| 191 | + |
| 192 | + assertThat(countDown.await(10, TimeUnit.SECONDS)) |
| 193 | + .withFailMessage("Timed out waiting to read message.") |
| 194 | + .isTrue(); |
| 195 | + assertThat(result) |
| 196 | + .withFailMessage("Message data is null in Pulsar consumer.") |
| 197 | + .isNotNull(); |
| 198 | + return result.get(); |
169 | 199 | } |
170 | 200 | } |
0 commit comments