use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testTakeThreadToFibers.
@Test
@SuppressWarnings("null")
public void testTakeThreadToFibers() throws Exception {
// TODO Reorg to try with blocking channel at least meaningful parts
assumeThat(mailboxSize, greaterThan(0));
final Channel<Object> takeSourceCh = newChannel();
// Test 2 fibers failing immediately on take 0 of 1
final ReceivePort<Object> take0RP = Channels.take((ReceivePort<Object>) takeSourceCh, 0);
final SuspendableRunnable take0SR = new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
assertThat(take0RP.receive(), is(nullValue()));
assertThat(take0RP.tryReceive(), is(nullValue()));
long start = System.nanoTime();
assertThat(take0RP.receive(10, TimeUnit.SECONDS), is(nullValue()));
long end = System.nanoTime();
// Should be immediate
assertThat(end - start, lessThan(new Long(5 * 1000 * 1000 * 1000)));
start = System.nanoTime();
assertThat(take0RP.receive(new Timeout(10, TimeUnit.SECONDS)), is(nullValue()));
end = System.nanoTime();
// Should be immediate
assertThat(end - start, lessThan(new Long(5 * 1000 * 1000 * 1000)));
}
};
final Fiber take0Of1Fiber1 = new Fiber("take-0-of-1_fiber1", scheduler, take0SR).start();
final Fiber take0Of1Fiber2 = new Fiber("take-0-of-1_fiber2", scheduler, take0SR).start();
takeSourceCh.send(new Object());
take0Of1Fiber1.join();
take0Of1Fiber2.join();
// 1 left in source, check and cleanup
assertThat(takeSourceCh.receive(), is(notNullValue()));
// Test tryReceive failing immediately when fiber blocked in receive on take 1 of 2
final ReceivePort<Object> take1Of2RP = Channels.take((ReceivePort<Object>) takeSourceCh, 1);
final Fiber timeoutSucceedingTake1Of2 = new Fiber("take-1-of-2_timeout_success", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
final long start = System.nanoTime();
assertThat(take1Of2RP.receive(1, TimeUnit.SECONDS), is(notNullValue()));
final long end = System.nanoTime();
assertThat(end - start, lessThan(new Long(500 * 1000 * 1000)));
}
}).start();
// Let the fiber blocks in receive before starting the try
Thread.sleep(100);
final Fiber tryFailingTake1Of2 = new Fiber("take-1-of-2_try_fail", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
final long start = System.nanoTime();
assertThat(take1Of2RP.tryReceive(), is(nullValue()));
final long end = System.nanoTime();
// Should be immediate
assertThat(end - start, lessThan(new Long(500 * 1000 * 1000)));
}
}).start();
Thread.sleep(100);
// Make messages available
takeSourceCh.send(new Object());
takeSourceCh.send(new Object());
timeoutSucceedingTake1Of2.join();
tryFailingTake1Of2.join();
// 1 left in source, check and cleanup
assertThat(takeSourceCh.receive(), is(notNullValue()));
// Comprehensive take + contention test:
//
// - 1 message available immediately, 2 messages available in a burst on the source after 1s
// - take 2
// - 5 fibers competing on the take source (1 in front)
//
// - one front fiber receiving with 200ms timeout => immediate success
// - one more front fiber receiving with 200ms timeout => fail
// - 3rd fiber taking over, receiving with 200ms timeout => fail
// - 4th fiber taking over, receiving with 1s timeout => success
// - 5th fiber asking untimed receive, waiting in monitor, will bail out because of take threshold
final ReceivePort<Object> take2Of3RPComprehensive = Channels.take((ReceivePort<Object>) takeSourceCh, 2);
final Function2<Long, Integer, Fiber> take1SRFun = new Function2<Long, Integer, Fiber>() {
@Override
public Fiber apply(final Long timeoutMS, final Integer position) {
return new Fiber("take-1-of-2_comprehensive_receiver_" + (timeoutMS >= 0 ? timeoutMS : "unlimited") + "ms-" + position, scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
final long start = System.nanoTime();
final Object res = (timeoutMS >= 0 ? take2Of3RPComprehensive.receive(timeoutMS, TimeUnit.MILLISECONDS) : take2Of3RPComprehensive.receive());
final long end = System.nanoTime();
switch(position) {
case 1:
assertThat(res, is(notNullValue()));
assertThat(end - start, lessThan(new Long(300 * 1000 * 1000)));
break;
case 2:
assertThat(res, is(nullValue()));
assertThat(end - start, greaterThan(new Long(300 * 1000 * 1000)));
break;
case 3:
assertThat(res, is(nullValue()));
assertThat(end - start, greaterThan(new Long(200 * 1000 * 1000)));
break;
case 4:
assertThat(res, is(notNullValue()));
assertThat(end - start, lessThan(new Long(1000 * 1000 * 1000)));
break;
case 5:
assertThat(res, is(nullValue()));
// Should be almost instantaneous
assertThat(end - start, lessThan(new Long(1000 * 1000 * 1000)));
break;
default:
fail();
break;
}
}
});
}
};
final Fiber[] competing = new Fiber[5];
// First front fiber winning first message
competing[0] = take1SRFun.apply(300l, 1).start();
// Make 1 message available immediately for the first front fiber to consume
takeSourceCh.send(new Object());
Thread.sleep(100);
// Second front fiber losing (waiting too little for second message)
competing[1] = take1SRFun.apply(300l, 2).start();
Thread.sleep(100);
// First waiter, will fail (not waiting enough)
competing[2] = take1SRFun.apply(200l, 3).start();
// First waiter takeover
Thread.sleep(300);
// Second waiter, will win second message (waiting enough)
competing[3] = take1SRFun.apply(1000l, 4).start();
// Second waiter takeover
Thread.sleep(300);
// Third waiter, will try after take threshold and will bail out
competing[4] = take1SRFun.apply(-1l, 5).start();
// Make 2 more messages available
takeSourceCh.send(new Object());
takeSourceCh.send(new Object());
// Wait fibers to finsh
for (final Fiber f : competing) f.join();
// 1 left in source, check and cleanup
assertThat(takeSourceCh.receive(), is(notNullValue()));
// Explicit (and uncoupled from source) closing of TakeSP
final ReceivePort<Object> take1Of0ExplicitClose = Channels.take((ReceivePort<Object>) takeSourceCh, 1);
final SuspendableRunnable explicitCloseSR = new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
final long start = System.nanoTime();
final Object ret = take1Of0ExplicitClose.receive();
final long end = System.nanoTime();
assertThat(ret, is(nullValue()));
assertTrue(take1Of0ExplicitClose.isClosed());
assertFalse(takeSourceCh.isClosed());
assertThat(end - start, lessThan(new Long(500 * 1000 * 1000)));
}
};
final Fiber explicitCloseF1 = new Fiber("take-explicit-close-1", scheduler, explicitCloseSR);
final Fiber explicitCloseF2 = new Fiber("take-explicit-close-2", scheduler, explicitCloseSR);
Thread.sleep(100);
take1Of0ExplicitClose.close();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testFlatmapSendWithTimeoutsThreadToFiber.
// @Test
public void testFlatmapSendWithTimeoutsThreadToFiber() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
assertThat(ch.receive(), is(1));
assertThat(ch.receive(30, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.receive(40, TimeUnit.MILLISECONDS), is(20));
assertThat(ch.receive(), is(200));
assertThat(ch.receive(), is(2000));
assertThat(ch.receive(), is(40));
assertThat(ch.receive(), is(400));
assertThat(ch.receive(), is(4000));
assertThat(ch.receive(30, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.receive(40, TimeUnit.MILLISECONDS), is(5));
assertThat(ch.receive(), is(nullValue()));
assertThat(ch.isClosed(), is(true));
}
}).start();
SendPort<Integer> ch1 = Channels.flatMapSend(Channels.<Integer>newChannel(1), ch, new Function<Integer, ReceivePort<Integer>>() {
@Override
public ReceivePort<Integer> apply(Integer x) {
if (x == 3)
return null;
if (x % 2 == 0)
return Channels.toReceivePort(Arrays.asList(new Integer[] { x * 10, x * 100, x * 1000 }));
else
return Channels.singletonReceivePort(x);
}
});
Strand.sleep(50);
ch1.send(1);
Strand.sleep(50);
ch1.send(2);
ch1.send(3);
ch1.send(4);
Strand.sleep(50);
ch1.send(5);
ch1.close();
fib.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class Actor method currentActor.
/**
* Returns the actor currently running in the current strand.
*/
public static <M, V> Actor<M, V> currentActor() {
final Fiber currentFiber = Fiber.currentFiber();
if (currentFiber == null)
return currentActor.get();
final SuspendableCallable target = currentFiber.getTarget();
if (target == null)
return null;
if (target instanceof Actor)
return (Actor<M, V>) target;
if (target instanceof ActorRunner)
return (Actor<M, V>) ((ActorRunner<V>) target).getActor();
return null;
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class PrimitiveChannelRingBenchmark method run.
void run() throws ExecutionException, InterruptedException {
final long start = System.nanoTime();
final IntChannel managerChannel = Channels.newIntChannel(mailboxSize);
IntChannel a = managerChannel;
for (int i = 0; i < N - 1; i++) a = createRelayActor(a);
final IntChannel lastChannel = a;
Fiber<Integer> manager = new Fiber<Integer>() {
@Override
protected Integer run() throws InterruptedException, SuspendExecution {
// start things off
lastChannel.send(1);
int msg = 0;
try {
for (int i = 0; i < M; i++) {
msg = managerChannel.receiveInt();
lastChannel.send(msg + 1);
}
return msg;
} catch (ReceivePort.EOFException e) {
return null;
}
}
};
// managerChannel.setStrand(manager);
manager.start();
int totalCount = manager.get();
final long time = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
System.out.println("Messages: " + totalCount + " Time (ms): " + time);
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class EventSourceTest method spawnActor.
private <T extends Actor<Message, V>, Message, V> T spawnActor(T actor) {
final Fiber fiber = new Fiber<>(actor);
fiber.setUncaughtExceptionHandler(new Strand.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Strand s, Throwable e) {
e.printStackTrace();
throw Exceptions.rethrow(e);
}
});
fiber.start();
return actor;
}
Aggregations