summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java137
-rw-r--r--java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java27
2 files changed, 98 insertions, 66 deletions
diff --git a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java
index 745240bd14..17197a7221 100644
--- a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java
+++ b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java
@@ -31,19 +31,19 @@ import org.osoa.sca.annotations.Service;
@Service(CallBackSeparateThreadClient.class)
public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadClient, EventProcessorCallBack {
/**
- * Used to sleep for 60 seconds.
+ * Max time to wait to receive events. If not all the events are received then timeout.
*/
- private static final int SIXTY_SECONDS = 60 * 1000;
+ private static final int TIMEOUT = 30 * 1000;
/**
- * Counts the number of one second call backs
+ * Counts the number of fast call backs.
*/
- private static final AtomicInteger oneSecondCallbackCount = new AtomicInteger();
+ private static final AtomicInteger fastCallbackCount = new AtomicInteger();
/**
- * Counts the number of five second call backs
+ * Counts the number of slow call backs.
*/
- private static final AtomicInteger fiveSecondCallbackCount = new AtomicInteger();
+ private static final AtomicInteger slowCallbackCount = new AtomicInteger();
/**
* This is our injected reference to the EventProcessorService
@@ -55,92 +55,115 @@ public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadC
* This tests call back patterns using separate threads.
*/
public void runTests() {
- // Register for 1 second call back
- registerFor1SecondCallback();
-
- // Wait for a few 1 second call backs
- System.out.println("Waiting for some 1 second calls");
- waitForSome1SecondCallbacks();
-
- // Register for 5 second call back
- registerFor5SecondCallback();
-
- // Wait for a few 1 second call backs
- System.out.println("Waiting for some 1 second calls");
- waitForSome1SecondCallbacks();
-
- // Wait for a few 5 second call backs
- System.out.println("Waiting for some 5 second calls");
- waitForSome5SecondCallbacks();
-
- System.out.println("Done");
+ try {
+ // Register for fast call back
+ registerForFastCallback();
+
+ // Wait for a few fast call backs
+ System.out.println("Waiting for some fast call backs");
+ waitForSomeFastCallbacks();
+
+ try {
+ // Register for slow call back
+ registerForSlowCallback();
+
+ // Wait for a few fast call backs
+ System.out.println("Waiting for some fast calls");
+ waitForSomeFastCallbacks();
+
+ // Wait for a few slow call backs
+ System.out.println("Waiting for some slow calls");
+ waitForSomeSlowCallbacks();
+ } finally {
+ unregisterForSlowCallback();
+ }
+
+ System.out.println("Done");
+ } finally {
+ unregisterForFastCallback();
+ }
}
/**
- * Waits for some one second call backs to be fired
+ * Waits for some fast call backs to be fired
*/
- private void waitForSome1SecondCallbacks() {
- // Reset the one second call back count
- oneSecondCallbackCount.set(0);
+ private void waitForSomeFastCallbacks() {
+ // Reset the fast call back count
+ fastCallbackCount.set(0);
- // Wait until we have 10 1 second call backs or 60 seconds has passed
+ // Wait until we have 10 fast call backs or timeout occurs
final long start = System.currentTimeMillis();
do {
- if (oneSecondCallbackCount.get() >= 10) {
- System.out.println("Received enough 1 second notifications");
+ if (fastCallbackCount.get() >= 10) {
+ System.out.println("Received enough fast notifications");
return;
}
try {
- Thread.sleep(500);
+ Thread.sleep(5);
} catch (InterruptedException e) {
Assert.fail("Unexpeceted exception " + e);
}
- } while (System.currentTimeMillis() - start < SIXTY_SECONDS);
+ } while (System.currentTimeMillis() - start < TIMEOUT);
// If we get to here then we did not receive enough events
- Assert.fail("Did not receive enough 1 second events");
+ Assert.fail("Did not receive enough fast events");
}
/**
- * Waits for some five second call backs to be fired
+ * Waits for some slow call backs to be fired
*/
- private void waitForSome5SecondCallbacks() {
- // Reset the five second call back count
- fiveSecondCallbackCount.set(0);
+ private void waitForSomeSlowCallbacks() {
+ // Reset the slow call back count
+ slowCallbackCount.set(0);
- // Wait until we have 4 5 second call backs or 60 seconds has passed
+ // Wait until we have 4 slow call backs or timeout
final long start = System.currentTimeMillis();
do {
- if (fiveSecondCallbackCount.get() >= 4) {
- System.out.println("Received enough 5 second notifications");
+ if (slowCallbackCount.get() >= 4) {
+ System.out.println("Received enough slow notifications");
return;
}
try {
- Thread.sleep(500);
+ Thread.sleep(5);
} catch (InterruptedException e) {
Assert.fail("Unexpeceted exception " + e);
}
- } while (System.currentTimeMillis() - start < SIXTY_SECONDS);
+ } while (System.currentTimeMillis() - start < TIMEOUT);
// If we get to here then we did not receive enough events
- Assert.fail("Did not receive enough 5 second events");
+ Assert.fail("Did not receive enough slow events");
+ }
+
+ /**
+ * Register to receive fast call backs
+ */
+ private void registerForFastCallback() {
+ aCallBackService.registerForEvent("FAST");
+ return;
+ }
+
+ /**
+ * Register to receive slow call backs
+ */
+ private void registerForSlowCallback() {
+ aCallBackService.registerForEvent("SLOW");
}
/**
- * Register to receive one second call backs
+ * Unregister to receive fast call backs
*/
- private void registerFor1SecondCallback() {
- aCallBackService.registerForEvent("ONE");
+ private void unregisterForFastCallback() {
+ aCallBackService.unregisterForEvent("FAST");
return;
}
/**
- * Register to receive five second call backs
+ * Unregister to receive slow call backs
*/
- private void registerFor5SecondCallback() {
- aCallBackService.registerForEvent("FIVE");
+ private void unregisterForSlowCallback() {
+ aCallBackService.unregisterForEvent("SLOW");
}
/**
@@ -152,12 +175,12 @@ public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadC
public void eventNotification(String aEventName, Object aEventData) {
// System.out.println("Received Event : " + aEventName + " " + aEventData);
- if (aEventName.equals("ONE")) {
- final int newValue = oneSecondCallbackCount.incrementAndGet();
- //System.out.println("Received total of " + newValue + " 1 second call backs");
- } else if (aEventName.equals("FIVE")) {
- final int newValue = fiveSecondCallbackCount.incrementAndGet();
- //System.out.println("Received total of " + newValue + " 5 second call backs");
+ if (aEventName.equals("FAST")) {
+ final int newValue = fastCallbackCount.incrementAndGet();
+ //System.out.println("Received total of " + newValue + " fast call backs");
+ } else if (aEventName.equals("SLOW")) {
+ final int newValue = slowCallbackCount.incrementAndGet();
+ //System.out.println("Received total of " + newValue + " slow call backs");
} else
System.out.println("Unknown event type of " + aEventName);
}
diff --git a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java
index 9b8692549a..fd34b74517 100644
--- a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java
+++ b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java
@@ -62,8 +62,8 @@ public class EventProcessorServiceImpl implements EventProcessorService {
// We will simulate an Event generator
allEventGenerators = new EventGenerator[2];
- allEventGenerators[0] = new EventGenerator("ONE", 1); // Generate the SECOND event every second
- allEventGenerators[1] = new EventGenerator("FIVE", 5); // Generate the FIVE event every 5 seconds
+ allEventGenerators[0] = new EventGenerator("FAST", 10); // Generate the FAST event every 10ms
+ allEventGenerators[1] = new EventGenerator("SLOW", 50); // Generate the SLOW event every 50ms
}
/**
@@ -137,22 +137,29 @@ public class EventProcessorServiceImpl implements EventProcessorService {
private final Timer timer = new Timer();
/**
+ * Lock object to ensure that we can cancel the timer cleanly.
+ */
+ private final Object lock = new Object();
+
+ /**
* Constructor
*
* @param aEventName The name of the Event to generate
- * @param frequencyInSeconds How frequently we should generate the Events
+ * @param frequencyInMilliseconds How frequently we should generate the Events
*/
- private EventGenerator(String aEventName, int frequencyInSeconds) {
+ private EventGenerator(String aEventName, int frequencyInMilliseconds) {
timer.schedule(new EventGeneratorTimerTask(aEventName),
- frequencyInSeconds * 1000,
- frequencyInSeconds * 1000);
+ frequencyInMilliseconds,
+ frequencyInMilliseconds);
}
/**
* Stop this Event Generator
*/
private void stop() {
- timer.cancel();
+ synchronized (lock) {
+ timer.cancel();
+ }
}
/**
@@ -178,8 +185,10 @@ public class EventProcessorServiceImpl implements EventProcessorService {
*/
public void run() {
- // System.out.println("Generating new event " + eventName);
- receiveEvent(eventName, "Separate Thread Notification: " + UUID.randomUUID().toString());
+ synchronized(lock) {
+ // System.out.println("Generating new event " + eventName);
+ receiveEvent(eventName, "Separate Thread Notification: " + UUID.randomUUID().toString());
+ }
}
}
}