@Test public void onTimeoutBeforeHandlerInitialized() throws Exception { Runnable runnable = mock(Runnable.class); this.emitter.onTimeout(runnable); this.emitter.initialize(this.handler); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(this.handler).onTimeout(captor.capture()); verify(this.handler).onCompletion(any()); assertNotNull(captor.getValue()); captor.getValue().run(); verify(runnable).run(); }
@Test public void onCompletionBeforeHandlerInitialized() throws Exception { Runnable runnable = mock(Runnable.class); this.emitter.onCompletion(runnable); this.emitter.initialize(this.handler); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(this.handler).onTimeout(any()); verify(this.handler).onCompletion(captor.capture()); assertNotNull(captor.getValue()); captor.getValue().run(); verify(runnable).run(); }
@Test public void onCompletionAfterHandlerInitialized() throws Exception { this.emitter.initialize(this.handler); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(this.handler).onTimeout(any()); verify(this.handler).onCompletion(captor.capture()); Runnable runnable = mock(Runnable.class); this.emitter.onCompletion(runnable); assertNotNull(captor.getValue()); captor.getValue().run(); verify(runnable).run(); }
@Test public void onTimeoutAfterHandlerInitialized() throws Exception { this.emitter.initialize(this.handler); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(this.handler).onTimeout(captor.capture()); verify(this.handler).onCompletion(any()); Runnable runnable = mock(Runnable.class); this.emitter.onTimeout(runnable); assertNotNull(captor.getValue()); captor.getValue().run(); verify(runnable).run(); }
@Test public void sendDuplicateBeforeHandlerInitialized() throws Exception { this.emitter.send("foo", MediaType.TEXT_PLAIN); this.emitter.send("foo", MediaType.TEXT_PLAIN); this.emitter.complete(); verifyNoMoreInteractions(this.handler); this.emitter.initialize(this.handler); verify(this.handler, times(2)).send("foo", MediaType.TEXT_PLAIN); verify(this.handler).complete(); verifyNoMoreInteractions(this.handler); }
@Test public void sendWithError() throws Exception { this.emitter.initialize(this.handler); verify(this.handler).onTimeout(any()); verify(this.handler).onError(any()); verify(this.handler).onCompletion(any()); verifyNoMoreInteractions(this.handler); IOException failure = new IOException(); doThrow(failure).when(this.handler).send("foo", MediaType.TEXT_PLAIN); try { this.emitter.send("foo", MediaType.TEXT_PLAIN); fail("Expected exception"); } catch (IOException ex) { // expected } verify(this.handler).send("foo", MediaType.TEXT_PLAIN); verifyNoMoreInteractions(this.handler); }
@Test public void sendBeforeHandlerInitializedWithError() throws Exception { IllegalStateException ex = new IllegalStateException(); this.emitter.send("foo", MediaType.TEXT_PLAIN); this.emitter.send("bar", MediaType.TEXT_PLAIN); this.emitter.completeWithError(ex); verifyNoMoreInteractions(this.handler); this.emitter.initialize(this.handler); verify(this.handler).send("foo", MediaType.TEXT_PLAIN); verify(this.handler).send("bar", MediaType.TEXT_PLAIN); verify(this.handler).completeWithError(ex); verifyNoMoreInteractions(this.handler); }
@Test public void sendAfterHandlerInitialized() throws Exception { this.emitter.initialize(this.handler); verify(this.handler).onTimeout(any()); verify(this.handler).onError(any()); verify(this.handler).onCompletion(any()); verifyNoMoreInteractions(this.handler); this.emitter.send("foo", MediaType.TEXT_PLAIN); this.emitter.send("bar", MediaType.TEXT_PLAIN); this.emitter.complete(); verify(this.handler).send("foo", MediaType.TEXT_PLAIN); verify(this.handler).send("bar", MediaType.TEXT_PLAIN); verify(this.handler).complete(); verifyNoMoreInteractions(this.handler); }
@Test public void sendBeforeHandlerInitialized() throws Exception { this.emitter.send("foo", MediaType.TEXT_PLAIN); this.emitter.send("bar", MediaType.TEXT_PLAIN); this.emitter.complete(); verifyNoMoreInteractions(this.handler); this.emitter.initialize(this.handler); verify(this.handler).send("foo", MediaType.TEXT_PLAIN); verify(this.handler).send("bar", MediaType.TEXT_PLAIN); verify(this.handler).complete(); verifyNoMoreInteractions(this.handler); }
@Test public void sendAfterHandlerInitializedWithError() throws Exception { this.emitter.initialize(this.handler); verify(this.handler).onTimeout(any()); verify(this.handler).onError(any()); verify(this.handler).onCompletion(any()); verifyNoMoreInteractions(this.handler); IllegalStateException ex = new IllegalStateException(); this.emitter.send("foo", MediaType.TEXT_PLAIN); this.emitter.send("bar", MediaType.TEXT_PLAIN); this.emitter.completeWithError(ex); verify(this.handler).send("foo", MediaType.TEXT_PLAIN); verify(this.handler).send("bar", MediaType.TEXT_PLAIN); verify(this.handler).completeWithError(ex); verifyNoMoreInteractions(this.handler); }
emitter.initialize(handler);
emitter.initialize(handler);
@Test public void writeText() throws Exception { EmitterProcessor<String> processor = EmitterProcessor.create(); ResponseBodyEmitter emitter = handleValue(processor, Flux.class, forClass(String.class)); EmitterHandler emitterHandler = new EmitterHandler(); emitter.initialize(emitterHandler); processor.onNext("The quick"); processor.onNext(" brown fox jumps over "); processor.onNext("the lazy dog"); processor.onComplete(); assertEquals("The quick brown fox jumps over the lazy dog", emitterHandler.getValuesAsText()); }
@Test public void writeStreamJson() throws Exception { this.servletRequest.addHeader("Accept", "application/stream+json"); EmitterProcessor<Bar> processor = EmitterProcessor.create(); ResponseBodyEmitter emitter = handleValue(processor, Flux.class, forClass(Bar.class)); EmitterHandler emitterHandler = new EmitterHandler(); emitter.initialize(emitterHandler); ServletServerHttpResponse message = new ServletServerHttpResponse(this.servletResponse); emitter.extendResponse(message); Bar bar1 = new Bar("foo"); Bar bar2 = new Bar("bar"); processor.onNext(bar1); processor.onNext(bar2); processor.onComplete(); assertEquals("application/stream+json", message.getHeaders().getContentType().toString()); assertEquals(Arrays.asList(bar1, "\n", bar2, "\n"), emitterHandler.getValues()); }
emitter.initialize(handler);