|
| 1 | +package edu.wpi.grip.util; |
| 2 | + |
| 3 | + |
| 4 | +import com.google.common.eventbus.SubscriberExceptionContext; |
| 5 | +import edu.wpi.grip.core.GRIPCoreModule; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.ConcurrentLinkedQueue; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 13 | + |
| 14 | +/** |
| 15 | + * This GRIPCoreTestModule is designed to be used in tests. |
| 16 | + * Any thread that uses the DefaultUncaughtExceptionHandler will have exceptions |
| 17 | + * dumped into this class. Exceptions thrown by the event bus will also be |
| 18 | + * collected using this handler as well. |
| 19 | + * When {@link #tearDown()} is called the exceptions that have been accrued |
| 20 | + * during the test will be rethrown. This should be done in a tests |
| 21 | + * {@link org.junit.After} annotated method. |
| 22 | + * If tear down is not called and another TestThrowablesHandler is created |
| 23 | + * this class will throw an assertion error. |
| 24 | + * This is done to ensure that exceptions always get dumped for the test |
| 25 | + * that has just run. |
| 26 | + */ |
| 27 | +public class GRIPCoreTestModule extends GRIPCoreModule { |
| 28 | + private static volatile boolean instanceAlive = false; |
| 29 | + |
| 30 | + private final ConcurrentLinkedQueue<ThreadThrowablePair> threadExceptions = new ConcurrentLinkedQueue<>(); |
| 31 | + private final ConcurrentLinkedQueue<SubscriberThrowablePair> subscriberExceptions = new ConcurrentLinkedQueue<>(); |
| 32 | + private boolean setUp = false; |
| 33 | + |
| 34 | + public GRIPCoreTestModule() { |
| 35 | + super(); |
| 36 | + assert !instanceAlive : "There is a GRIPCoreTestModule that did not have it's `tearDown` method called."; |
| 37 | + } |
| 38 | + |
| 39 | + public void setUp() { |
| 40 | + instanceAlive = true; |
| 41 | + setUp = true; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Rethrows any exceptions that were thrown by other threads or the event bus. |
| 46 | + * This must be called in the {@link org.junit.After} method for any test that |
| 47 | + * uses this class. If this is not called then the next test that tries |
| 48 | + * to use an instance of this class will throw an exception. |
| 49 | + */ |
| 50 | + public void tearDown() { |
| 51 | + try { |
| 52 | + final List<Throwable> throwables = new ArrayList<>(2); |
| 53 | + if (!threadExceptions.isEmpty()) { |
| 54 | + throwables.add(MultiException.create(threadExceptions.stream().map(ThreadThrowablePair::getThrowable).collect(Collectors.toList()))); |
| 55 | + } |
| 56 | + if (!subscriberExceptions.isEmpty()) { |
| 57 | + throwables.add(MultiException.create(subscriberExceptions.stream().map(SubscriberThrowablePair::getThrowable).collect(Collectors.toList()))); |
| 58 | + } |
| 59 | + if (!throwables.isEmpty()) { |
| 60 | + throw MultiException.create(throwables); |
| 61 | + } |
| 62 | + } finally { |
| 63 | + instanceAlive = false; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void configure() { |
| 70 | + assert setUp : "The GRIPCoreTestModule handler was not set up. Call 'setUp' before passing the injector"; |
| 71 | + super.configure(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected void onThreadException(Thread thread, Throwable exception) { |
| 76 | + // Do not call super, we don't want the default functionality |
| 77 | + threadExceptions.add(new ThreadThrowablePair(thread, exception)); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void onSubscriberException(Throwable exception, SubscriberExceptionContext exceptionContext) { |
| 82 | + // Do not call super, we don't want the default functionality |
| 83 | + subscriberExceptions.add(new SubscriberThrowablePair(exception, exceptionContext)); |
| 84 | + } |
| 85 | + |
| 86 | + private static final class ThreadThrowablePair { |
| 87 | + private final Thread thread; |
| 88 | + private final Throwable throwable; |
| 89 | + |
| 90 | + private ThreadThrowablePair(Thread thread, Throwable throwable) { |
| 91 | + this.thread = checkNotNull(thread, "Thread cannot be null"); |
| 92 | + this.throwable = checkNotNull(throwable, "Throwable cannot be null"); |
| 93 | + } |
| 94 | + |
| 95 | + private Throwable getThrowable() { |
| 96 | + return throwable; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static final class SubscriberThrowablePair { |
| 101 | + private final Throwable exception; |
| 102 | + private final SubscriberExceptionContext exceptionContext; |
| 103 | + |
| 104 | + private SubscriberThrowablePair(Throwable exception, SubscriberExceptionContext exceptionContext) { |
| 105 | + this.exception = checkNotNull(exception, "Throwable cannot be null"); |
| 106 | + this.exceptionContext = checkNotNull(exceptionContext, "SubscriberExceptionContext cannot be null"); |
| 107 | + } |
| 108 | + |
| 109 | + private Throwable getThrowable() { |
| 110 | + return exception; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments