/** If the transformation has at least one step in a transformation, which writes it's data straight to a servlet output we should wait transformation's termination. Otherwise the servlet's response lifecycle may come to an end and the response will be closed by container while the transformation will be still trying writing data into it. */ @VisibleForTesting void finishProcessing( Trans trans, PrintWriter out ) { if ( trans.getSteps().stream().anyMatch( step -> step.meta.passDataToServletOutput() ) ) { trans.waitUntilFinished(); } else { WebResult webResult = new WebResult( WebResult.STRING_OK, "Transformation started", trans.getContainerObjectId() ); out.println( webResult.getXML() ); out.flush(); } }
@Before public void setup() throws Exception { runTransServlet = new RunTransServlet(); outData = new ByteArrayOutputStream(); out = new PrintWriter( outData ); stepList = new ArrayList<>(); for ( int i = 0; i < 5; i++ ) { StepMetaDataCombi stepMetaDataCombi = new StepMetaDataCombi(); StepMetaInterface stepMeta = mock( StepMetaInterface.class ); when( stepMeta.passDataToServletOutput() ).thenReturn( false ); stepMetaDataCombi.meta = stepMeta; stepList.add( stepMetaDataCombi ); } when( trans.getSteps() ).thenReturn( stepList ); when( trans.getContainerObjectId() ).thenReturn( transId ); }
@Test public void testFinishProcessingTransWithServletOutputSteps() throws Exception { StepMetaDataCombi stepMetaDataCombi = new StepMetaDataCombi(); StepMetaInterface stepMeta = mock( StepMetaInterface.class ); when( stepMeta.passDataToServletOutput() ).thenReturn( true ); stepMetaDataCombi.meta = stepMeta; stepList.add( stepMetaDataCombi ); doAnswer( new Answer<Void>() { @Override public Void answer( InvocationOnMock invocation ) throws Throwable { Thread.currentThread().sleep( 2000 ); return null; } } ).when( trans ).waitUntilFinished(); runTransServlet.finishProcessing( trans, out ); assertTrue( outData.toString().isEmpty() ); }