RetryingToTakeResourceExceptionHandler extends ExceptionHandlingReceiver { @Override public void receive(final IMessageProcessor processor) throws MessageReceiveException { try { IObject context = processor.getContext(); OutOfResourceException exception = (OutOfResourceException) getException(context); processor.getSequence().goTo(getCauseLevel(context), getCauseStep(context)); processor.pauseProcess(); exception.getSource().onAvailable(() -> { try { processor.continueProcess(null); } catch (AsynchronousOperationException e) { throw new ActionExecuteException(e); } }); } catch (ReadValueException | InvalidArgumentException | AsynchronousOperationException e) { throw new MessageReceiveException(e); } } RetryingToTakeResourceExceptionHandler(); @Override void receive(final IMessageProcessor processor);  }
@Test public void Should_enqueueACallbackToResourceSource() throws Exception { AsynchronousOperationException asynchronousOperationException = mock(AsynchronousOperationException.class); when(messageProcessorMock.getSequence()).thenReturn(sequenceMock); when(contextMock.getValue(same(exceptionFieldName))).thenReturn(outOfResourceExceptionMock); when(contextMock.getValue(same(causeLevelFieldName))).thenReturn(135); when(contextMock.getValue(same(causeStepFieldName))).thenReturn(123); IMessageReceiver receiver = new RetryingToTakeResourceExceptionHandler(); receiver.receive(messageProcessorMock); verify(sequenceMock).goTo(eq(135), eq(123)); verify(resourceSourceMock).onAvailable(callbackCaptor.capture()); callbackCaptor.getValue().execute(); verify(messageProcessorMock).continueProcess(null); reset(messageProcessorMock); doThrow(asynchronousOperationException).when(messageProcessorMock).continueProcess(null); try { callbackCaptor.getValue().execute(); fail(); } catch (ActionExecuteException e) { assertSame(asynchronousOperationException, e.getCause()); } }
@Test public void Should_wrapCaughtExceptions() throws Exception { ReadValueException readValueException = mock(ReadValueException.class); when(contextMock.getValue(any())).thenThrow(readValueException); IMessageReceiver receiver = new RetryingToTakeResourceExceptionHandler(); try { receiver.receive(messageProcessorMock); fail(); } catch (MessageReceiveException e) { assertSame(readValueException, e.getCause()); } }