Created
January 30, 2012 14:51
-
-
Save johndemic/1704805 to your computer and use it in GitHub Desktop.
Using a CountdownLatch to Test Flows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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