@Test(expected = IllegalStateException.class)
public void testEndingASpanFromInvalidThreadContext() throws Throwable {

    final SpanSink sink = mock(SpanSink.class);
    SpanSinkRegistry.register(sink);
    try (final Span trace = SpanHelper.startTrace("The Trace")) {
        final CountDownLatch successLatch = new CountDownLatch(1);
        final ArrayBlockingQueue<Throwable> expectedException = new ArrayBlockingQueue<>(1);
        // contrived to allow ending a span in a different thread where the spanContext is unavailable.
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    trace.end();
                    successLatch.countDown();
                } catch (Throwable t) {
                    expectedException.offer(t.fillInStackTrace());
                }
            }
        }).start();
        //noinspection ThrowableResultOfMethodCallIgnored
        throw expectedException.poll(100, TimeUnit.MILLISECONDS);
    }
}