use of com.github.jamesnetherton.zulip.client.api.stream.StreamService in project zulip-java-client by jamesnetherton.
the class ZulipEventIT method messageEvents.
@Test
public void messageEvents() throws Exception {
CountDownLatch latch = new CountDownLatch(3);
List<String> messages = new ArrayList<>();
String streamName = UUID.randomUUID().toString().split("-")[0];
StreamSubscriptionRequest subscriptionRequest = StreamSubscriptionRequest.of(streamName, streamName);
StreamService streamService = zulip.streams();
streamService.subscribe(subscriptionRequest).execute();
for (int i = 0; i < 10; i++) {
List<Stream> streams = streamService.getAll().execute();
List<Stream> matches = streams.stream().filter(stream -> stream.getName().equals(streamName)).collect(Collectors.toList());
if (matches.size() == 1) {
break;
}
Thread.sleep(500);
}
EventPoller eventPoller = zulip.events().captureMessageEvents(new MessageEventListener() {
@Override
public void onEvent(Message event) {
messages.add(event.getContent());
latch.countDown();
}
});
try {
eventPoller.start();
MessageService messageService = zulip.messages();
for (int i = 0; i < 3; i++) {
messageService.sendStreamMessage("Test Content " + i, streamName, "testtopic").execute();
}
assertTrue(latch.await(5, TimeUnit.SECONDS));
for (int i = 0; i < 3; i++) {
assertEquals("Test Content " + i, messages.get(i));
}
} catch (ZulipClientException e) {
e.printStackTrace();
throw e;
} finally {
eventPoller.stop();
}
}
Aggregations