use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class ChannelTest method testChannelClose.
@Test
public void testChannelClose() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber(scheduler, 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);
ch.close();
ch.send(6);
ch.send(7);
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class ChannelTest method testPrimitiveChannelClose.
@Test
public void testPrimitiveChannelClose() throws Exception {
assumeThat(mailboxSize, not(equalTo(0)));
final IntChannel ch = Channels.newIntChannel(mailboxSize, policy);
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
try {
for (int i = 1; i <= 5; i++) {
int m = ch.receiveInt();
assertThat(m, is(i));
}
} catch (QueueChannel.EOFException e) {
fail();
}
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();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransferSelectorTest method testSelectSend1.
@Test
public void testSelectSend1() throws Exception {
final Channel<String> channel1 = newChannel();
final Channel<String> channel2 = newChannel();
final Channel<String> channel3 = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
SelectAction<String> sa1 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
SelectAction<String> sa2 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
assertThat(sa1.index(), is(1));
assertThat(sa2.index(), is(0));
}
}).start();
Thread.sleep(200);
String m1 = channel2.receive();
assertThat(m1, equalTo("hi2"));
Thread.sleep(200);
String m2 = channel1.receive();
assertThat(m2, equalTo("hi1"));
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransferSelectorTest method testSelectReceiveWithTimeoutChannel.
@Test
public void testSelectReceiveWithTimeoutChannel() throws Exception {
final Channel<String> channel1 = newChannel();
final Channel<String> channel2 = newChannel();
final Channel<String> channel3 = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
SelectAction<String> sa1 = select(receive(channel1), receive(channel2), receive(channel3), receive(TimeoutChannel.<String>timeout(1, TimeUnit.MILLISECONDS)));
SelectAction<String> sa2 = select(receive(channel1), receive(channel2), receive(channel3), receive(TimeoutChannel.<String>timeout(300, TimeUnit.MILLISECONDS)));
String m2 = sa2.message();
assertThat(sa1.index(), is(3));
assertThat(sa2.index(), is(0));
assertThat(m2, equalTo("hello"));
}
}).start();
Thread.sleep(200);
channel1.send("hello");
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransferSelectorTest method testSelectSendWithTimeoutChannel.
@Test
public void testSelectSendWithTimeoutChannel() throws Exception {
final Channel<String> channel1 = newChannel();
final Channel<String> channel2 = newChannel();
final Channel<String> channel3 = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
SelectAction<String> sa1 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"), receive(TimeoutChannel.<String>timeout(1, TimeUnit.MILLISECONDS)));
SelectAction<String> sa2 = select(send(channel1, "bye1"), send(channel2, "bye2"), send(channel3, "bye3"), receive(TimeoutChannel.<String>timeout(300, TimeUnit.MILLISECONDS)));
assertThat(sa1.index(), is(3));
assertThat(sa2.index(), is(2));
}
}).start();
Thread.sleep(200);
String m1 = channel3.receive();
// the first send is cancelled
assertThat(m1, equalTo("bye3"));
fib.join();
}
Aggregations