Skip to content

Instantly share code, notes, and snippets.

@johndemic
Created January 30, 2012 14:51
Show Gist options
  • Save johndemic/1704805 to your computer and use it in GitHub Desktop.
Save johndemic/1704805 to your computer and use it in GitHub Desktop.
Using a CountdownLatch to Test Flows
import org.mule.api.client.MuleClient;
import org.mule.api.context.notification.EndpointMessageNotificationListener;
import org.mule.api.context.notification.ServerNotification;
import org.mule.context.notification.EndpointMessageNotification;
import org.mule.tck.FunctionalTestCase;
import org.mule.util.FileUtils;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class LatchFunctionalTestCase extends FunctionalTestCase {
@Override
protected String getConfigResources() {
return "src/main/app/mule-test-config.xml";
}
// This latch has a count of one and will be decremented once the file is written
CountDownLatch fileEndpointHit;
public void doSetUp() throws Exception {
fileEndpointHit = new CountDownLatch(1);
muleContext.registerListener(new EndpointMessageNotificationListener() {
@Override
public void onNotification(ServerNotification n) {
// We're listening here for a notification that the file:outbound-endpoint has finished writing.
// Once we see it is we countdown the latch.
EndpointMessageNotification notification = (EndpointMessageNotification) n;
if (notification.getEndpoint().equals("file:///tmp/foo") && n.getActionName().equals("end dispatch")) {
fileEndpointHit.countDown();
}
}
});
}
public void testFileCreated() throws Exception {
MuleClient client = muleContext.getClient();
client.dispatch("vm://foo", "Hello, world", null);
// We wait 5 seconds here for the latch to be counted down.
assertTrue("File hasn't been created", fileEndpointHit.await(5, TimeUnit.SECONDS));
assertEquals("File content differs from what was dispatched",
"Hello, world", FileUtils.readFileToString(new File("/tmp/foo/test.txt")));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/3.2/mule-file.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.2/mule-vm.xsd
">
<!-- Enable dynamic notification listeners for the ENDPOINT-MESSAGE event. Note you'll want to disable this
for production -->
<notifications dynamic="true">
<notification event="ENDPOINT-MESSAGE"/>
</notifications>
<flow name="file.flow">
<vm:inbound-endpoint path="foo"/>
<file:outbound-endpoint path="/tmp/foo" outputPattern="test.txt"/>
</flow>
</mule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment