Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void run() {
if (shimmerMSG.mB instanceof EndThread){
stop=true;
} else {
processMsgFromCallback(shimmerMSG);
processMsgSafely(shimmerMSG);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Expand All @@ -134,8 +134,22 @@ public void run() {

//as a safety precaution, remove all its callbacks
removeSetWaitForDataAll();

};

/** One bad message must not kill the consumer. If this thread died,
* mQueue would stop draining, every producer would block in put() once
* it held 1024 messages, and anything that then sends a callback -
* including an application's shutdown hook - would hang for good. */
private void processMsgSafely(ShimmerMsg shimmerMSG) {
try {
processMsgFromCallback(shimmerMSG);
} catch (RuntimeException e) {
System.err.println(getName() + ": exception processing callback message "
+ shimmerMSG.mIdentifier + "; the consumer thread continues");
e.printStackTrace();
}
}
}

/** This allows the class to receive msgs from the class passed in as an argument
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.shimmerresearch.driver;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

public class BasicProcessWithCallBackTest {

/** Records each message it gets, and throws for identifier 1. */
private static class ThrowingConsumer extends BasicProcessWithCallBack {
final List<Integer> received = new CopyOnWriteArrayList<Integer>();
final CountDownLatch gotLast = new CountDownLatch(1);

@Override
protected void processMsgFromCallback(ShimmerMsg shimmerMSG) {
received.add(shimmerMSG.mIdentifier);
if (shimmerMSG.mIdentifier == 1) {
throw new IllegalStateException("simulated failure in a callback handler");
}
if (shimmerMSG.mIdentifier == 3) {
gotLast.countDown();
}
}
}

/**
* A runtime exception from one message must not kill the consumer thread.
* When it did, the queue stopped draining, producers blocked in put() once it
* held 1024 messages, and Consensys hung at exit inside its shutdown hook.
*/
@Test
public void consumerThreadSurvivesExceptionInHandler() throws Exception {
ThrowingConsumer consumer = new ThrowingConsumer();
consumer.startConsumerThreadIfNull();
try {
consumer.queueMethod(new ShimmerMsg(1, null));
consumer.queueMethod(new ShimmerMsg(2, null));
consumer.queueMethod(new ShimmerMsg(3, null));

assertTrue("message after the exception was never processed",
consumer.gotLast.await(5, TimeUnit.SECONDS));
assertEquals(3, consumer.received.size());
assertTrue(consumer.mGUIConsumerThread.isAlive());
} finally {
consumer.stopConsumerThread();
}
}
}
Loading