use of uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy in project microservice_framework by CJSCommonPlatform.
the class ConsecutiveEventBufferServiceTest method shoulCloseSourceStreamOnConsecutiveStreamClose.
@Test
public void shoulCloseSourceStreamOnConsecutiveStreamClose() {
final UUID streamId = randomUUID();
final String source = "source";
final String eventName = "source.event.name";
when(bufferInitialisationStrategy.initialiseBuffer(eq(streamId), eq(source))).thenReturn(2L);
final StreamCloseSpy sourceStreamSpy = new StreamCloseSpy();
when(streamBufferRepository.findStreamByIdAndSource(streamId, source)).thenReturn(Stream.of(new StreamBufferEvent(streamId, 4L, "someEventContent4", source), new StreamBufferEvent(streamId, 8L, "someEventContent8", source)).onClose(sourceStreamSpy));
final JsonEnvelope bufferedEvent4 = mock(JsonEnvelope.class);
when(jsonObjectEnvelopeConverter.asEnvelope("someEventContent4")).thenReturn(bufferedEvent4);
final JsonEnvelope incomingEvent = envelope().with(metadataWithDefaults().withName(eventName).withStreamId(streamId).withVersion(3L)).build();
final Stream<JsonEnvelope> returnedEvents = bufferService.currentOrderedEventsWith(incomingEvent);
returnedEvents.close();
assertThat(sourceStreamSpy.streamClosed(), is(true));
}
use of uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy in project microservice_framework by CJSCommonPlatform.
the class EventBufferInterceptorTest method ensureStreamCLosedIfExceptionOccurs.
@Test
public void ensureStreamCLosedIfExceptionOccurs() throws Exception {
final Deque<Interceptor> interceptors = new LinkedList<>();
interceptors.add(eventBufferInterceptor);
interceptors.add(new ExceptionThrowingInterceptor());
target = new TestTarget();
interceptorChain = new DefaultInterceptorChain(interceptors, target);
final InterceptorContext inputContext = interceptorContextWithInput(envelope_1);
final StreamCloseSpy streamSpy = new StreamCloseSpy();
final Stream<JsonEnvelope> envelopes = Stream.of(this.envelope_1, envelope_2).onClose(streamSpy);
when(eventBufferService.currentOrderedEventsWith(this.envelope_1)).thenReturn(envelopes);
try {
interceptorChain.processNext(inputContext);
} catch (TestException expected) {
// do nothing
}
assertThat(streamSpy.streamClosed(), is(true));
}
use of uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy in project microservice_framework by CJSCommonPlatform.
the class DefaultInterceptorChainTest method shouldCloseStreamIfExceptionOccurs.
@Test
public void shouldCloseStreamIfExceptionOccurs() throws Exception {
interceptors.offer(new ExceptionThrowingInterceptor());
final InterceptorContext interceptorContext_1 = mock(InterceptorContext.class);
final InterceptorContext interceptorContext_2 = mock(InterceptorContext.class);
final StreamCloseSpy streamSpy = new StreamCloseSpy();
final Stream<InterceptorContext> interceptorContexts = Stream.of(interceptorContext_1, interceptorContext_2).onClose(streamSpy);
try {
interceptorChain.processNext(interceptorContexts);
} catch (TestException e) {
}
assertThat(streamSpy.streamClosed(), is(true));
}
use of uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy in project microservice_framework by CJSCommonPlatform.
the class DefaultEventRepositoryTest method shouldCloseAllStreamsOnCloseOfStreamOfStreams.
@Test
public void shouldCloseAllStreamsOnCloseOfStreamOfStreams() throws Exception {
final UUID streamId1 = UUID.fromString("4b4e80a0-76f7-476c-b75b-527e38fb251e");
final UUID streamId2 = UUID.fromString("4b4e80a0-76f7-476c-b75b-527e38fb252e");
final UUID streamId3 = UUID.fromString("4b4e80a0-76f7-476c-b75b-527e38fb253e");
final Event event1 = eventOf(streamId1);
final Event event2 = eventOf(streamId2);
final Event event3 = eventOf(streamId3);
final JsonEnvelope envelope1 = mock(JsonEnvelope.class);
final JsonEnvelope envelope2 = mock(JsonEnvelope.class);
final JsonEnvelope envelope3 = mock(JsonEnvelope.class);
StreamCloseSpy streamCloseSpy1 = new StreamCloseSpy();
StreamCloseSpy streamCloseSpy2 = new StreamCloseSpy();
StreamCloseSpy streamCloseSpy3 = new StreamCloseSpy();
StreamCloseSpy streamCloseSpy4 = new StreamCloseSpy();
when(eventJdbcRepository.getStreamIds()).thenReturn(Stream.of(streamId1, streamId2, streamId3).onClose(streamCloseSpy1));
when(eventJdbcRepository.findByStreamIdOrderByPositionAsc(streamId1)).thenReturn(Stream.of(event1).onClose(streamCloseSpy2));
when(eventJdbcRepository.findByStreamIdOrderByPositionAsc(streamId2)).thenReturn(Stream.of(event2).onClose(streamCloseSpy3));
when(eventJdbcRepository.findByStreamIdOrderByPositionAsc(streamId3)).thenReturn(Stream.of(event3).onClose(streamCloseSpy4));
when(eventConverter.envelopeOf(event1)).thenReturn(envelope1);
when(eventConverter.envelopeOf(event2)).thenReturn(envelope2);
when(eventConverter.envelopeOf(event3)).thenReturn(envelope3);
final Stream<Stream<JsonEnvelope>> streamOfStreams = defaultEventRepository.getStreamOfAllEventStreams();
streamOfStreams.collect(toList());
streamOfStreams.close();
assertThat(streamCloseSpy1.streamClosed(), is(true));
assertThat(streamCloseSpy4.streamClosed(), is(true));
assertThat(streamCloseSpy2.streamClosed(), is(true));
assertThat(streamCloseSpy3.streamClosed(), is(true));
}
Aggregations