@Test public void testBlockingChannelSendingThread() throws Exception { assumeThat(policy, is(OverflowPolicy.BLOCK)); final Channel<Integer> ch = newChannel(); Fiber<Integer> fib = new Fiber<Integer>( fjPool, new SuspendableCallable<Integer>() { @Override public Integer run() throws SuspendExecution, InterruptedException { int i = 0; while (ch.receive() != null) { i++; Fiber.sleep(50); } return i; } }) .start(); for (int i = 0; i < 10; i++) ch.send(i); ch.close(); assertThat(fib.get(), is(10)); }
@Test public void testChannelCloseWithSleep() throws Exception { final Channel<Integer> ch = newChannel(); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { for (int i = 1; i <= 5; i++) { Integer m = ch.receive(); assertThat(m, equalTo(i)); } Integer m = ch.receive(); assertThat(m, nullValue()); assertTrue(ch.isClosed()); } }) .start(); Thread.sleep(50); ch.send(1); ch.send(2); ch.send(3); ch.send(4); ch.send(5); Thread.sleep(50); ch.close(); ch.send(6); ch.send(7); fib.join(); }
@Test public void whenChannelClosedThenBlockedSendsComplete() throws Exception { assumeThat(policy, is(OverflowPolicy.BLOCK)); final Channel<Integer> ch = newChannel(); final SuspendableRunnable r = new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { for (int i = 1; i <= 100; i++) { ch.send(i); } } }; Fiber fib1 = new Fiber("fiber", fjPool, r).start(); Fiber fib2 = new Fiber("fiber", fjPool, r).start(); Thread.sleep(500); ch.close(); fib1.join(); fib2.join(); }