use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransferSelectorTest method testSelectSendTimeout.
@Test
public void testSelectSendTimeout() 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(1, TimeUnit.MILLISECONDS, send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
SelectAction<String> sa2 = select(300, TimeUnit.MILLISECONDS, send(channel1, "bye1"), send(channel2, "bye2"), send(channel3, "bye3"));
assertThat(sa1, is(nullValue()));
assertThat(sa2.index(), is(2));
}
}).start();
Thread.sleep(200);
String m1 = channel3.receive();
// the first send is cancelled
assertThat(m1, equalTo("bye3"));
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TwoSidedTest method twoSidedTest.
@Test
public void twoSidedTest() throws Exception {
// Publisher
final Channel<Integer> publisherChannel = Channels.newChannel(random() ? 0 : 5, OverflowPolicy.BLOCK);
final Strand publisherStrand = new Fiber<Void>(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
for (long i = 0; i < ELEMENTS; i++) publisherChannel.send((int) (i % 1000));
publisherChannel.close();
}
}).start();
final Publisher publisher = ReactiveStreams.toPublisher(publisherChannel);
// Subscriber
final ReceivePort<Integer> subscriberChannel = ReactiveStreams.subscribe(buffer, overflowPolicy, publisher);
final Strand subscriberStrand = new Fiber<Void>(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
long count = 0;
for (; ; ) {
Integer x = subscriberChannel.receive();
if (x == null)
break;
assertEquals(count % 1000, x.longValue());
count++;
}
subscriberChannel.close();
assertEquals(ELEMENTS, count);
}
}).start();
subscriberStrand.join(5, TimeUnit.SECONDS);
publisherStrand.join(5, TimeUnit.SECONDS);
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransferSelectorTest method testSelectReceiveTimeout.
@Test
public void testSelectReceiveTimeout() 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(1, TimeUnit.MILLISECONDS, receive(channel1), receive(channel2), receive(channel3));
SelectAction<String> sa2 = select(300, TimeUnit.MILLISECONDS, receive(channel1), receive(channel2), receive(channel3));
String m2 = sa2.message();
assertThat(sa1, is(nullValue()));
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 testSelectReceiveWithClose1.
@Test
public void testSelectReceiveWithClose1() 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));
String m1 = sa1.message();
SelectAction<String> sa2 = select(receive(channel1), receive(channel2), receive(channel3));
String m2 = sa2.message();
assertThat(sa1.index(), is(2));
assertThat(m1, nullValue());
assertThat(m2, nullValue());
}
}).start();
Thread.sleep(200);
channel3.close();
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testFlatmapWithTimeoutsThreadToFiber.
@Test
public void testFlatmapWithTimeoutsThreadToFiber() throws Exception {
final Channel<Integer> ch1 = newChannel();
final Channel<Object> sync = Channels.newChannel(0);
final Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
final ReceivePort<Integer> ch = Channels.flatMap(ch1, new Function<Integer, ReceivePort<Integer>>() {
@Override
public ReceivePort<Integer> apply(Integer x) {
if (x == 3)
// Discard
return null;
if (x % 2 == 0)
return Channels.toReceivePort(Arrays.asList(new Integer[] { x * 10, x * 100, x * 1000 }));
else
return Channels.singletonReceivePort(x);
}
});
// 0
sync.receive();
assertThat(ch.receive(200, TimeUnit.MILLISECONDS), is(1));
// 1
sync.receive();
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.receive(190, TimeUnit.MILLISECONDS), is(20));
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(200));
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(2000));
// 2
sync.receive();
assertThat(ch.receive(200, TimeUnit.MILLISECONDS), is(40));
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(400));
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(4000));
// 3
sync.receive();
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.receive(190, TimeUnit.MILLISECONDS), is(5));
// 4
sync.receive();
assertThat(ch.receive(30, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.isClosed(), is(true));
}
}).start();
// 0
sync.send(GO);
Strand.sleep(50);
ch1.send(1, 10, TimeUnit.SECONDS);
// 1
sync.send(GO);
Strand.sleep(100);
ch1.send(2, 10, TimeUnit.SECONDS);
// 2
sync.send(GO);
// Discarded
ch1.send(3, 10, TimeUnit.SECONDS);
ch1.send(4, 10, TimeUnit.SECONDS);
// 3
sync.send(GO);
Strand.sleep(50);
ch1.send(5, 10, TimeUnit.SECONDS);
// 4
sync.send(GO);
ch1.close();
fib.join();
}
Aggregations