@Test
public void testCallingBroadcastFromAtLeastTwoThreadsFails() throws InterruptedException {

    WithinAppNotificationRouter router = WithinAppNotificationRouter.get();
    NotificationBus<Notification> bus = new TestBus();
    router.registerExecutionLayer(new NonExecutingEventScheduler(TestSchedulerType.TEST_SCHEDULER_TYPE, TimeProvider.NULL), bus);
    OuterNotificationHandler outerHandler = new OuterNotificationHandler();
    router.addHandler(outerHandler);
    int numberOfBogusThreads = 10;  // should be at least 1
    List<LocalBroadcastRunnable> notifications = new ArrayList<>();
    try {
        for (int i = 0; i < numberOfBogusThreads + 1; i++) {
            LocalBroadcastRunnable r = new LocalBroadcastRunnable(router);
            notifications.add(r);
            new Thread(r).start();
        }
        for (LocalBroadcastRunnable r : notifications) {
            r.waitForRunning();
        }
        Thread.sleep(50);
        for (LocalBroadcastRunnable r : notifications) {
            r.stop = true;
        }
    } finally {
        boolean success = false;
        int exceptionCount = 0;
        for (LocalBroadcastRunnable r : notifications) {
            Throwable t = r.waitForCompletion();
            if (t != null) {
                ++exceptionCount;
                Assertions.assertTrue(t instanceof IllegalStateException);
            } else {
                Assertions.assertFalse(success, "At most one thread should succeed");
                success = true;
            }
        }
        Assertions.assertTrue(success, "At least one thread should succeed");
        Assertions.assertEquals(numberOfBogusThreads, exceptionCount, "All bogus threads should have failed");
    }
}