Search in sources :

Example 1 with StrandFactoryBuilder

use of co.paralleluniverse.strands.StrandFactoryBuilder in project quasar by puniverse.

the class ActorTest method testSpawnWithStrandFactory.

@Test
public void testSpawnWithStrandFactory() throws Exception {
    final AtomicBoolean run = new AtomicBoolean(false);
    Actor<Message, Integer> actor = new BasicActor<Message, Integer>(mailboxConfig) {

        @Override
        protected Integer doRun() throws SuspendExecution, InterruptedException {
            run.set(true);
            return 3;
        }
    };
    ActorRef a = actor.spawn(new StrandFactoryBuilder().setFiber(null).setNameFormat("my-fiber-%d").build());
    Strand s = LocalActor.getStrand(a);
    assertTrue(s.isFiber());
    assertThat(s.getName(), equalTo("my-fiber-0"));
    assertThat((Integer) LocalActor.get(a), equalTo(3));
    assertThat(run.get(), is(true));
    run.set(false);
    actor = new BasicActor<Message, Integer>(mailboxConfig) {

        @Override
        protected Integer doRun() throws SuspendExecution, InterruptedException {
            run.set(true);
            return 3;
        }
    };
    a = actor.spawn(new StrandFactoryBuilder().setThread(false).setNameFormat("my-thread-%d").build());
    s = LocalActor.getStrand(a);
    assertTrue(!s.isFiber());
    assertThat(s.getName(), equalTo("my-thread-0"));
    LocalActor.join(a);
    assertThat(run.get(), is(true));
    run.set(false);
    Actor<Message, Integer> actor2 = new BasicActor<Message, Integer>("coolactor", mailboxConfig) {

        @Override
        protected Integer doRun() throws SuspendExecution, InterruptedException {
            run.set(true);
            return 3;
        }
    };
    a = actor2.spawn(new StrandFactoryBuilder().setFiber(null).setNameFormat("my-fiber-%d").build());
    s = LocalActor.getStrand(a);
    assertTrue(s.isFiber());
    assertThat(s.getName(), equalTo("coolactor"));
    assertThat((Integer) LocalActor.get(a), equalTo(3));
    assertThat(run.get(), is(true));
    run.set(false);
    actor2 = new BasicActor<Message, Integer>("coolactor", mailboxConfig) {

        @Override
        protected Integer doRun() throws SuspendExecution, InterruptedException {
            run.set(true);
            return 3;
        }
    };
    a = actor2.spawn(new StrandFactoryBuilder().setThread(false).setNameFormat("my-thread-%d").build());
    s = LocalActor.getStrand(a);
    assertTrue(!s.isFiber());
    assertThat(s.getName(), equalTo("coolactor"));
    LocalActor.join(a);
    assertThat(run.get(), is(true));
    run.set(false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) StrandFactoryBuilder(co.paralleluniverse.strands.StrandFactoryBuilder) Strand(co.paralleluniverse.strands.Strand) Test(org.junit.Test)

Aggregations

SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)1 Strand (co.paralleluniverse.strands.Strand)1 StrandFactoryBuilder (co.paralleluniverse.strands.StrandFactoryBuilder)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1