use of mantis.io.reactivex.netty.protocol.http.sse.ServerSentEvent in project mantis by Netflix.
the class HttpSourceImplTest method testStreamingErrorFromServerWillNotCompleteSource.
@Test
public void testStreamingErrorFromServerWillNotCompleteSource() throws Exception {
int eventCount = 20;
HttpSourceImpl<ByteBuf, ServerSentEvent, ServerContext<ServerSentEvent>> source = createStreamingSource("test/finiteStream?count=" + eventCount);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> ex = new AtomicReference<>();
final ConcurrentHashMap<ServerInfo, Set<String>> items = new ConcurrentHashMap<>();
Observable.merge(source.call(new Context(), new Index(1, 1))).subscribe(new Subscriber<ServerContext<ServerSentEvent>>() {
@Override
public void onCompleted() {
latch.countDown();
}
@Override
public void onError(Throwable e) {
ex.set(e);
latch.countDown();
}
@Override
public void onNext(ServerContext<ServerSentEvent> pair) {
items.putIfAbsent(pair.getServer(), new HashSet<String>());
items.get(pair.getServer()).add(pair.getValue().contentAsString());
}
});
if (latch.await(5, TimeUnit.SECONDS)) {
fail("The test case should not finish at all");
}
Assert.assertNull("The timeout error should be captured by the client so it does not surface to the source", ex.get());
for (Server server : localServerProvider.getServers()) {
ServerInfo serverInfo = toServerInfo(server);
assertEquals("There should be no source level error", 0, sourceObserver.getErrorCount());
assertEquals("There should be one connection attempt per server", 1, sourceObserver.getCount(serverInfo, EventType.CONNECTION_ATTEMPTED));
assertEquals("There should be one established connection per server ", 1, sourceObserver.getCount(serverInfo, EventType.CONNECTION_ESTABLISHED));
assertEquals("There should no subscribed server because of read timeout", 1, sourceObserver.getCount(serverInfo, EventType.SUBSCRIPTION_ESTABLISHED));
assertEquals(String.format("There should be %d item before simulated error per server", eventCount), eventCount, items.get(serverInfo).size());
}
}
use of mantis.io.reactivex.netty.protocol.http.sse.ServerSentEvent in project mantis by Netflix.
the class HttpSourceImplTest method testRemovedServerWillBeUnsubscribed.
@Test
public void testRemovedServerWillBeUnsubscribed() throws Exception {
HttpSourceImpl<ByteBuf, ServerSentEvent, ServerContext<ServerSentEvent>> source = createStreamingSource("test/infStream");
final AtomicInteger counter = new AtomicInteger();
final CountDownLatch done = new CountDownLatch(1);
final ConcurrentHashMap<String, AtomicInteger> result = new ConcurrentHashMap<>();
final ConcurrentMap<ServerInfo, CountDownLatch> serverRemovalLatch = new ConcurrentHashMap<>();
for (Server server : localServerProvider.getServers()) {
serverRemovalLatch.put(toServerInfo(server), new CountDownLatch(1));
}
Observable.merge(source.call(new Context(), new Index(1, 1))).doOnNext(new Action1<ServerContext<ServerSentEvent>>() {
@Override
public void call(ServerContext<ServerSentEvent> pair) {
try {
assertTrue(pair.getValue().contentAsString().contains("line"));
counter.incrementAndGet();
String msg = pair.getValue().contentAsString();
result.putIfAbsent(msg, new AtomicInteger());
result.get(msg).incrementAndGet();
} finally {
serverRemovalLatch.get(pair.getServer()).countDown();
}
}
}).doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
fail("Unexpected failure: " + throwable);
}
}).doAfterTerminate(new Action0() {
@Override
public void call() {
done.countDown();
}
}).subscribe();
for (Server server : localServerProvider.getServers()) {
serverRemovalLatch.get(toServerInfo(server)).await();
localServerProvider.removeServer(toServerInfo(server));
}
long waitSeconds = 5;
boolean timedout = !done.await(waitSeconds, TimeUnit.SECONDS);
if (timedout) {
fail(String.format("Waited at least %d seconds for the test to finish. Connection to at least one server is not unsubscribed.", waitSeconds));
}
assertTrue(String.format("Each server should emit at least one event before being canceled. Expected counter >= %d, actual counter: %d", localServerProvider.serverSize(), counter.get()), counter.get() >= localServerProvider.serverSize());
for (Server server : localServerProvider.getServers()) {
ServerInfo serverInfo = toServerInfo(server);
assertEquals("There should be one completion per server", 1, sourceObserver.getCount(serverInfo, EventType.SOURCE_COMPLETED));
assertEquals("There should be one un-subscription per server", 1, sourceObserver.getCount(serverInfo, EventType.CONNECTION_UNSUBSCRIBED));
assertEquals("There should be no error", 0, sourceObserver.getCount(serverInfo, EventType.SUBSCRIPTION_FAILED));
assertEquals("There should be one connection per server", 1, sourceObserver.getCount(serverInfo, EventType.SUBSCRIPTION_ESTABLISHED));
assertEquals(String.format("There should be exactly one cancellation event per server for %d servers. ", localServerProvider.serverSize()), 1, sourceObserver.getCount(serverInfo, EventType.SUBSCRIPTION_CANCELED));
}
assertEquals("The source should emit exactly one completion event", 1, sourceObserver.getCompletionCount());
assertEquals("The server should not have any error event", 0, sourceObserver.getErrorCount());
Set<EventType> events = sourceObserver.getEvents();
assertEquals("Each server should have one occurrence per event type except server failure event", (EventType.values().length - 1), events.size());
for (EventType event : events) {
assertEquals("Each event should be recorded exactly once", localServerProvider.serverSize(), sourceObserver.getEventCount(event));
}
}
Aggregations