|
| 1 | +package org.apache.client.sse; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.apache.client.sse.utils.TestUtils; |
| 5 | +import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; |
| 6 | +import org.apache.http.impl.nio.client.HttpAsyncClients; |
| 7 | +import org.testng.Assert; |
| 8 | +import org.testng.annotations.Test; |
| 9 | + |
| 10 | +import java.util.concurrent.BlockingQueue; |
| 11 | +import java.util.concurrent.ExecutionException; |
| 12 | +import java.util.concurrent.Executors; |
| 13 | +import java.util.concurrent.Future; |
| 14 | + |
| 15 | +@Slf4j |
| 16 | +public class SseTest { |
| 17 | + |
| 18 | + private final String SSEUri = "http://abrandao.com/lab/Javascript/Javascript_SSEvents/sserver_cpu.php"; |
| 19 | + private final int MAX_CONCURRENT_SSE_STREAMS = 6; |
| 20 | + private CloseableHttpAsyncClient asyncClient; |
| 21 | + private ApacheHttpSseClient sseClient; |
| 22 | + |
| 23 | + @Test(groups = { "system" }) |
| 24 | + public void testSSEClient() throws ExecutionException, InterruptedException { |
| 25 | + //Initialize client |
| 26 | + asyncClient = HttpAsyncClients.createDefault(); |
| 27 | + asyncClient.start(); |
| 28 | + sseClient = new ApacheHttpSseClient(asyncClient, Executors.newFixedThreadPool(MAX_CONCURRENT_SSE_STREAMS)); |
| 29 | + |
| 30 | + //Initialize request |
| 31 | + SseRequest sseRequest = new SseRequest(SSEUri); |
| 32 | + Future<SseResponse> sseResponseFuture = sseClient.execute(sseRequest); |
| 33 | + |
| 34 | + //Add a 10 second delay for some events to be processed by the streaming client |
| 35 | + TestUtils.sleep(10); |
| 36 | + |
| 37 | + //Validate events exists and log them |
| 38 | + SseResponse response = sseResponseFuture.get(); |
| 39 | + SseEntity responseEntity = response.getEntity(); |
| 40 | + Assert.assertTrue(responseEntity.hasMoreEvents(), "Expected event stream to have some events"); |
| 41 | + BlockingQueue<Event> eventList = responseEntity.getEvents(); |
| 42 | + for (Event eachEvent: eventList) { |
| 43 | + TestUtils.logEvent(eachEvent); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | +} |
0 commit comments