Loading

Mule SDK | How to avoid "Cannot open a new cursor on a closed stream error" when your flow returns a stream

Data pubblicazione: Mar 2, 2024
Risoluzione

SYMPTOM

When a custom java mule application is created and it has the operations which are required to return InputStream. The test cases would fail when we try to assert the response content in JUnit test cases.
Below is the example code which fails with the error "java.lang.IllegalStateException: Cannot open a new cursor on a closed stream"

@Test
public void executeRetrieveInfoOperation() throws Exception {
	Event event = flowRunner("retrieveInfoFlow").run();
	InputStream inputStream = ((CursorStreamProvider) event.getMessage().getPayload().getValue()).openCursor();	
	System.out.println(inputStream);
    String payloadValue = IOUtils.toString(inputStream);
    assertThat(payloadValue, is("Using Configuration [configId] with Connection id [aValue:100]"));
}
 

CAUSE

The stream is already closed. Normal runtime behavior is to close all streams when the flow ends.

SOLUTION

Update the code to the below one.

@Test
public void executeRetrieveInfoOperation() throws Exception {
	Event event = flowRunner("retrieveInfoFlow").keepStreamsOpen().run();
	InputStream inputStream = ((CursorStreamProvider) event.getMessage().getPayload().getValue()).openCursor();	
	System.out.println(inputStream);
    String payloadValue = IOUtils.toString(inputStream);
    assertThat(payloadValue, is("Using Configuration [configId] with Connection id [aValue:100]"));
}
The key part is the method keepStreamsOpen() which prevents the flowRunner from closing the streams.
Numero articolo Knowledge

001121949

 
Caricamento
Salesforce Help | Article