Search in sources :

Example 1 with SuspendExecution

use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.

the class ActorTest method testWatchGC.

@Test
public void testWatchGC() throws Exception {
    Assume.assumeFalse(Debug.isDebug());
    final Actor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {

        @Override
        protected Void doRun() throws SuspendExecution, InterruptedException {
            Fiber.sleep(120000);
            return null;
        }
    });
    System.out.println("actor1 is " + actor);
    WeakReference wrActor2 = new WeakReference(spawnActor(new BasicActor<Message, Void>(mailboxConfig) {

        @Override
        protected Void doRun() throws SuspendExecution, InterruptedException {
            Fiber.sleep(10);
            final Object watch = watch(actor.ref());
            //                unwatch(actor, watch);
            return null;
        }
    }));
    System.out.println("actor2 is " + wrActor2.get());
    for (int i = 0; i < 10; i++) {
        Thread.sleep(10);
        System.gc();
    }
    Thread.sleep(2000);
    assertEquals(null, wrActor2.get());
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Example 2 with SuspendExecution

use of co.paralleluniverse.fibers.SuspendExecution 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)

Example 3 with SuspendExecution

use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.

the class ActorTest method testWatch.

@Test
public void testWatch() throws Exception {
    Actor<Message, Void> actor1 = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {

        @Override
        protected Void doRun() throws SuspendExecution, InterruptedException {
            Fiber.sleep(100);
            return null;
        }
    });
    final AtomicBoolean handlerCalled = new AtomicBoolean(false);
    Actor<Message, Void> actor2 = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {

        @Override
        protected Void doRun() throws SuspendExecution, InterruptedException {
            Message m = receive(200, TimeUnit.MILLISECONDS);
            assertThat(m, is(nullValue()));
            return null;
        }

        @Override
        protected Message handleLifecycleMessage(LifecycleMessage m) {
            super.handleLifecycleMessage(m);
            handlerCalled.set(true);
            return null;
        }
    });
    actor2.watch(actor1.ref());
    actor1.join();
    actor2.join();
    assertThat(handlerCalled.get(), is(true));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 4 with SuspendExecution

use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.

the class ProxyServerTest method testCallOnVoidMethod.

@Test
public void testCallOnVoidMethod() throws Exception {
    final AtomicInteger called = new AtomicInteger(0);
    final Server<?, ?, ?> a = spawnServer(true, new A() {

        public int foo(String str, int x) {
            throw new UnsupportedOperationException();
        }

        @Suspendable
        public void bar(int x) {
            try {
                Strand.sleep(100);
                called.set(x);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (SuspendExecution e) {
                throw new AssertionError(e);
            }
        }
    });
    ((A) a).bar(15);
    assertThat(called.get(), is(15));
    a.shutdown();
    LocalActor.join(a, 100, TimeUnit.MILLISECONDS);
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Suspendable(co.paralleluniverse.fibers.Suspendable) Test(org.junit.Test)

Example 5 with SuspendExecution

use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.

the class ServerTest method whenCalledFromThreadThenDeferredResultIsReturned.

@Test
public void whenCalledFromThreadThenDeferredResultIsReturned() throws Exception {
    final Server<Message, Integer, Message> s = new ServerActor<Message, Integer, Message>() {

        private int a, b;

        private ActorRef<?> from;

        private Object id;

        private boolean received;

        @Override
        public void init() throws SuspendExecution {
            setTimeout(50, TimeUnit.MILLISECONDS);
        }

        @Override
        public Integer handleCall(ActorRef<?> from, Object id, Message m) {
            // save for later
            this.a = m.a;
            this.b = m.b;
            this.from = from;
            this.id = id;
            this.received = true;
            return null;
        }

        @Override
        protected void handleTimeout() throws SuspendExecution {
            if (received) {
                reply(from, id, a + b);
                shutdown();
            }
        }
    }.spawn();
    int res = s.call(new Message(3, 4));
    assertThat(res, is(7));
    LocalActor.join(s, 100, TimeUnit.MILLISECONDS);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) Matchers.anyObject(org.mockito.Matchers.anyObject) Test(org.junit.Test)

Aggregations

SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)37 Test (org.junit.Test)28 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)24 Fiber (co.paralleluniverse.fibers.Fiber)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Suspendable (co.paralleluniverse.fibers.Suspendable)5 Strand (co.paralleluniverse.strands.Strand)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Channel (co.paralleluniverse.strands.channels.Channel)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 Matchers.anyObject (org.mockito.Matchers.anyObject)2 Actor (co.paralleluniverse.actors.Actor)1 ActorRef (co.paralleluniverse.actors.ActorRef)1 BasicActor (co.paralleluniverse.actors.BasicActor)1 LocalActor (co.paralleluniverse.actors.LocalActor)1 MailboxConfig (co.paralleluniverse.actors.MailboxConfig)1 MessageProcessor (co.paralleluniverse.actors.MessageProcessor)1 AbstractServerHandler (co.paralleluniverse.actors.behaviors.AbstractServerHandler)1