@Test public void testCurrentThreadCanCreateAndUseSubject() { @SuppressWarnings("unchecked") List<String> subject = ThreadConfinedProxy.newProxyInstance(List.class, new ArrayList<String>(), ThreadConfinedProxy.Strictness.VALIDATE); subject.add(testString); assertEquals(testString, Iterables.getOnlyElement(subject)); }
@Test public void testChildThreadCanDelegateBackToMainThread() throws InterruptedException { final AtomicReference<List<String>> inputReference = new AtomicReference<List<String>>(null); final AtomicInteger outputReference = new AtomicInteger(0); final Thread mainThread = Thread.currentThread(); Thread childThread = new Thread(() -> { outputReference.compareAndSet(0, 1); List<String> subjectInChildThread = inputReference.get(); ThreadConfinedProxy.changeThread(subjectInChildThread, mainThread, Thread.currentThread()); subjectInChildThread.add(testString); if (Iterables.getOnlyElement(subjectInChildThread).equals(testString)) { outputReference.compareAndSet(1, 2); } ThreadConfinedProxy.changeThread(subjectInChildThread, Thread.currentThread(), mainThread); }); @SuppressWarnings("unchecked") List<String> subject = ThreadConfinedProxy.newProxyInstance(List.class, new ArrayList<String>(), ThreadConfinedProxy.Strictness.VALIDATE); inputReference.set(subject); childThread.start(); childThread.join(10000); assertEquals(2, outputReference.get()); // We got delegated back, so we can use subject again assertEquals(testString, Iterables.getOnlyElement(subject)); }
childThread.join(10000); assertEquals(2, outputReference.get()); assertEquals(3, outputReference.get()); assertEquals(4, outputReference.get()); outputReference.compareAndSet(4, 5); assertEquals(5, outputReference.get());
childThread.join(10000); assertEquals(2, outputReference.get()); assertEquals(testString, Iterables.getOnlyElement(subject));
@Test public void testPropagateExceptions() throws SQLException { IThingThatThrows thing = ThreadConfinedProxy.newProxyInstance(IThingThatThrows.class, new ThingThatThrows(), ThreadConfinedProxy.Strictness.VALIDATE); assertEquals(1, thing.doStuff(IThingThatThrows.Behavior.RETURN_ONE)); try { thing.doStuff(IThingThatThrows.Behavior.THROW_RUNTIME); fail("Should throw Runtime Exception"); } catch (RuntimeException e) { // OK } try { thing.doStuff(IThingThatThrows.Behavior.THROW_SQL); fail("Should throw SQL Exception"); } catch (SQLException e) { // OK } }