@Test public void sendMessageFromThreadToThread() throws Exception { final Channel<String> ch = newChannel(); Thread thread = new Thread( new Runnable() { @Override public void run() { try { Thread.sleep(100); ch.send("a message"); } catch (InterruptedException | SuspendExecution ex) { throw new AssertionError(ex); } } }); thread.start(); String m = ch.receive(); assertThat(m, equalTo("a message")); thread.join(); }
@Ignore @Test public void whenReceiveNotCalledFromOwnerThenThrowException4() throws Exception { assumeTrue(Debug.isAssertionsEnabled()); final Channel<String> ch = newChannel(); Thread thread = new Thread( new Runnable() { @Override public void run() { try { ch.receive(); } catch (InterruptedException ex) { throw new AssertionError(ex); } catch (SuspendExecution e) { throw new AssertionError(e); } } }); thread.start(); Thread.sleep(100); ch.send("a message"); boolean thrown = false; try { ch.receive(); } catch (Throwable e) { thrown = true; } assertTrue(thrown); thread.join(); }
// Do some simple concurrent testing public void testConcurrentSimple() throws InterruptedException { final NonBlockingIdentityHashMap<String, String> nbhm = new NonBlockingIdentityHashMap<String, String>(); final String[] keys = new String[20000]; for (int i = 0; i < 20000; i++) keys[i] = "k" + i; // In 2 threads, add & remove even & odd elements concurrently Thread t1 = new Thread() { public void run() { work_helper(nbhm, "T1", 1, keys); } }; t1.start(); work_helper(nbhm, "T0", 0, keys); t1.join(); // In the end, all members should be removed StringBuffer buf = new StringBuffer(); buf.append("Should be emptyset but has these elements: {"); boolean found = false; for (String x : nbhm.keySet()) { buf.append(" ").append(x); found = true; } if (found) System.out.println(buf + " }"); assertThat("concurrent size=0", nbhm.size(), is(0)); for (String x : nbhm.keySet()) { assertTrue("No elements so never get here", false); } }