@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(); }
@Test public void testChannelGroupReceive() throws Exception { final Channel<String> channel1 = newChannel(); final Channel<String> channel2 = newChannel(); final Channel<String> channel3 = newChannel(); final ReceivePortGroup<String> group = new ReceivePortGroup<String>(channel1, channel2, channel3); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { String m1 = group.receive(); String m2 = channel2.receive(); assertThat(m1, equalTo("hello")); assertThat(m2, equalTo("world!")); } }) .start(); Thread.sleep(100); channel3.send("hello"); Thread.sleep(100); if (policy != OverflowPolicy.BLOCK) { channel1.send("goodbye"); // TransferChannel will block here Thread.sleep(100); } channel2.send("world!"); fib.join(); }
@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(); }
@Test public void testTopic() throws Exception { final Channel<String> channel1 = newChannel(); final Channel<String> channel2 = newChannel(); final Channel<String> channel3 = newChannel(); final Topic<String> topic = new Topic<String>(); topic.subscribe(channel1); topic.subscribe(channel2); topic.subscribe(channel3); Fiber f1 = new Fiber( fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { assertThat(channel1.receive(), equalTo("hello")); assertThat(channel1.receive(), equalTo("world!")); } }) .start(); Fiber f2 = new Fiber( fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { assertThat(channel2.receive(), equalTo("hello")); assertThat(channel2.receive(), equalTo("world!")); } }) .start(); Fiber f3 = new Fiber( fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { assertThat(channel3.receive(), equalTo("hello")); assertThat(channel3.receive(), equalTo("world!")); } }) .start(); Thread.sleep(100); topic.send("hello"); Thread.sleep(100); topic.send("world!"); f1.join(); f2.join(); f3.join(); }
@Ignore @Test public void whenReceiveNotCalledFromOwnerThenThrowException2() throws Exception { assumeTrue(Debug.isAssertionsEnabled()); final Channel<String> ch = newChannel(); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { String m = ch.receive(); assertThat(m, equalTo("a message")); } }) .start(); Thread.sleep(50); ch.send("a message"); boolean thrown = false; try { ch.receive(); } catch (Throwable e) { thrown = true; } assertTrue(thrown); fib.join(); }
@Override public String call(Integer i) throws Exception { ClassLoader ccl = Thread.currentThread().getContextClassLoader(); InputStream in = ccl.getResourceAsStream("test.resource"); byte[] bytes = ByteStreams.toByteArray(in); in.close(); return new String(bytes, 0, bytes.length, "UTF-8"); }
@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 testChannelGroupReceiveWithTimeout() throws Exception { final Channel<String> channel1 = newChannel(); final Channel<String> channel2 = newChannel(); final Channel<String> channel3 = newChannel(); final ReceivePortGroup<String> group = new ReceivePortGroup<String>(channel1, channel2, channel3); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { String m1 = group.receive(); String m2 = channel2.receive(); String m3 = group.receive(10, TimeUnit.MILLISECONDS); String m4 = group.receive(200, TimeUnit.MILLISECONDS); assertThat(m1, equalTo("hello")); assertThat(m2, equalTo("world!")); assertThat(m3, nullValue()); assertThat(m4, equalTo("foo")); } }) .start(); Thread.sleep(100); channel3.send("hello"); Thread.sleep(100); channel2.send("world!"); Thread.sleep(100); channel1.send("foo"); fib.join(); }
@Override public void failed(Throwable e, Description desc) { System.out.println("FAILED TEST " + desc.getMethodName() + ": " + e.getMessage()); e.printStackTrace(System.err); if (Debug.isDebug() && !(e instanceof OutOfMemoryError)) { Debug.record( 0, "EXCEPTION IN THREAD " + Thread.currentThread().getName() + ": " + e + " - " + Arrays.toString(e.getStackTrace())); Debug.dumpRecorder("~/quasar.dump"); } }
@Test public void testPrimitiveChannelClose() throws Exception { assumeThat(mailboxSize, not(equalTo(0))); final IntChannel ch = Channels.newIntChannel(mailboxSize, policy); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { for (int i = 1; i <= 5; i++) { int m = ch.receiveInt(); assertThat(m, is(i)); } try { int m = ch.receiveInt(); fail("m = " + m); } catch (QueueChannel.EOFException e) { } assertTrue(ch.isClosed()); } }) .start(); Thread.sleep(50); ch.send(1); ch.send(2); ch.send(3); ch.send(4); ch.send(5); 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(); }
@Test public void sendMessageFromThreadToFiber() throws Exception { final Channel<String> ch = newChannel(); Fiber fib = new Fiber( "fiber", fjPool, new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { String m = ch.receive(); assertThat(m, equalTo("a message")); } }) .start(); Thread.sleep(50); ch.send("a message"); fib.join(); }