示例#1
0
def test_attribute_access():
    class A(ThreadedActor):
        def act(self):
            self.receive(echo=self.echo)
        def echo(self, msg):
            with ActorRef(msg.reply_to) as sender:
                sender.reply(x=4)
    with ThreadedActor.spawn(A) as a, ActorRef(a) as a_ref:
        result = a_ref.sync('echo')
        assert result['x'] == 4
示例#2
0
def test_threaded_spawn_with_args_fast():
    class T(ThreadedActor):
        def act(self):
            assert self.x == 0
            self.receive(get_x=self.get_x)
        def get_x(self, msg):
            with ActorRef(msg.reply_to) as sender:
                sender.reply(x=self.x)
    class W(Actor):
        def act(self):
            self.x = 1
            self.receive(
                reply=self.read_value('x'),
                timeout=0.5
            )
            return self.x
    with ThreadedActor.spawn(T, x=0) as t, ActorRef(t) as t_ref, W() as w:
        t_ref.get_x(reply_to=w)
        result = w.act()
        assert result == 0
示例#3
0
def test_threaded_spawn_with_args():
    class T(ThreadedActor):
        def act(self):
            self.receive(
                args = self.args
            )
        def args(self, msg):
            with ActorRef(msg['reply_to']) as sender:
                sender.reply(x=self.x, k=self.k)
    class A(Actor):
        def act(self):
            self.receive(
                reply = self.reply
            )
            return self.x, self.k
        def reply(self, msg):
            self.x = msg['x']
            self.k = msg['k']
    with ThreadedActor.spawn(T, x=5, k='a') as t, ActorRef(t.address()) as t_ref, A() as a:
        assert t_ref.is_alive()
        t_ref.args(reply_to=a.address())
        result = a.act()
        assert result == (5, 'a')
示例#4
0
def test_threaded_spawn():
    class T(ThreadedActor):
        def act(self):
            self.receive()
    with ThreadedActor.spawn(T) as t, ActorRef(t.address()) as t_ref:
        assert t_ref.is_alive()