1
2
3
4
5
6
7 package org.mule.tck.functional;
8
9 import org.mule.api.MuleEventContext;
10 import org.mule.api.lifecycle.InitialisationException;
11 import org.mule.config.i18n.MessageFactory;
12
13 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
14 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
15 import junit.framework.AssertionFailedError;
16
17 public class CountdownCallback implements EventCallback
18 {
19 private CountDownLatch countDown;
20
21 public CountdownCallback(int messagesExpected)
22 {
23 this.countDown = new CountDownLatch(messagesExpected);
24 }
25
26 public void eventReceived(MuleEventContext context, Object Component) throws Exception
27 {
28 synchronized (this)
29 {
30 if (countDown.getCount() > 0)
31 {
32 countDown.countDown();
33 }
34 else
35 {
36 throw new AssertionFailedError("Too many messages received");
37 }
38 }
39 }
40
41 public long getCount() throws InitialisationException
42 {
43 if (countDown != null)
44 {
45 return countDown.getCount();
46 }
47 else
48 {
49 throw new InitialisationException(MessageFactory.createStaticMessage("CountDownLatch has not been initialized."), null);
50 }
51 }
52
53 public boolean await(long timeout) throws InterruptedException
54 {
55 return countDown.await(timeout, TimeUnit.MILLISECONDS);
56 }
57
58 }