Example #1
0
    def test_call(self):
        """Start an Actor that starts another Actor and then uses
        call on the Address. Assert that the parent gets a response
        from the child and returns it.
        """
        class CallChild(actor.Actor):
            def main(self):
                pattern, message = self.receive({
                    'call': str,
                    'address': object,
                    'method': str,
                    'message': object
                })
                message['address'].cast({
                    'response': message['call'],
                    'message': 'Hi There'
                })

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('method', {})

        self.assertEquals(actor.spawn(CallParent).wait(), "Hi There")

        class TimeoutCallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('method', {}, 1)

        self.assertEquals(actor.spawn(TimeoutCallParent).wait(), "Hi There")
Example #2
0
 def test_manual_link(self):
     class LinkTest(actor.Actor):
         def main(self):
             child = actor.spawn(foo)
             child.link()
             cancel = gevent.Timeout(0.1)
             result = self.receive(
                 {'exit': object, 'address': object},
                 {'exception': object, 'address': object})
     actor.spawn(LinkTest).wait()
Example #3
0
def build(receive, n):
    ring = []
    for i in range(n):
        if not ring:
            node = actor.spawn(forward, actor.curaddr())
        else:
            node = actor.spawn(forward, ring[-1])
        ring.append(node)
        gevent.sleep()

    ring[-1] | {'text': 'hello around the ring'}
    pat, data = receive()
    return data
Example #4
0
    def test_manual_link(self):
        class LinkTest(actor.Actor):
            def main(self):
                child = actor.spawn(foo)
                child.link()
                cancel = eventlet.Timeout(0.1, eventlet.TimeoutError)
                result = self.receive({
                    'exit': object,
                    'address': object
                }, {
                    'exception': object,
                    'address': object
                })

        actor.spawn(LinkTest).wait()
Example #5
0
 def main(self):
     activea = actor.spawn(ActiveActor)
     cycle1 = activea.call('get_cycle')
     self.sleep(0.001)
     cycle2 = activea.call('get_cycle')
     activea.call('die')
     return cycle2 > cycle1
Example #6
0
    def test_receive_nowait(self):
        """Assert that calling receive with timeout = 0 works.
        """
        class ActiveActor(actor.Actor):
            def main(self):
                self.cycle = 0
                while True:
                    pat, msg = self.receive(actor.CALL_PATTERN, timeout=0)
                    if pat and msg['method'] == 'get_cycle':
                        msg['address'].cast({
                            'response': msg['call'],
                            'message': self.cycle
                        })
                    if pat and msg['method'] == 'die':
                        msg['address'].cast({
                            'response': msg['call'],
                            'message': None
                        })
                        return
                    self.cycle += 1
                    self.cooperate()

        class ActiveActorMonitor(actor.Actor):
            def main(self):
                activea = actor.spawn(ActiveActor)
                cycle1 = activea.call('get_cycle')
                self.sleep(0.001)
                cycle2 = activea.call('get_cycle')
                activea.call('die')
                return cycle2 > cycle1

        self.assertEquals(actor.spawn(ActiveActorMonitor).wait(), True)
Example #7
0
 def main(self):
     child = actor.spawn(foo)
     child.link()
     cancel = gevent.Timeout(0.1)
     result = self.receive(
         {'exit': object, 'address': object},
         {'exception': object, 'address': object})
Example #8
0
    def test_receive_times_out(self):
        """Assert that calling with a timeout > 0.
        """
        class ActiveActor(actor.Actor):
            def main(self):
                self.touts = 0
                while True:
                    pat, msg = self.receive(actor.CALL_PATTERN, timeout=0.01)
                    if pat is None:
                        self.touts += 1
                    else:
                        if msg['method'] == 'die':
                            msg['address'] | {
                                'response': msg['call'],
                                'message': self.touts
                            }
                            return

        class ActiveActorMonitor(actor.Actor):
            def main(self):
                activea = actor.spawn(ActiveActor)
                self.sleep(0.1)
                touts = activea.die()
                return touts > 3

        self.assertEquals(actor.spawn(ActiveActorMonitor).wait(), True)
Example #9
0
 def test_linked_wait(self):
     """Spawn Supervisor, which calls spawn_link with
     a function that returns 2 + 2 and returns the result extracted
     out of the link exit.
     """
     result = actor.spawn(Supervisor).wait()
     self.assertEquals(result, 4)
Example #10
0
 def main(self):
     activea = actor.spawn(ActiveActor)
     cycle1 = activea.call('get_cycle')
     self.sleep(0.001)
     cycle2 = activea.call('get_cycle')
     activea.call('die')
     return cycle2 > cycle1
Example #11
0
    def test_receive_nowait(self):
        """Assert that calling receive with timeout = 0 works.
        """
        class ActiveActor(actor.Actor):
            def main(self):
                self.cycle = 0
                while True:
                    pat,msg = self.receive(actor.CALL_PATTERN,timeout=0)
                    if pat and msg['method'] == 'get_cycle':
                        msg['address'].cast({'response':msg['call'], 
                                             'message':self.cycle})
                    if pat and msg['method'] == 'die':
                        msg['address'].cast({'response':msg['call'],
                                             'message':None})
                        return
                    self.cycle+=1
                    self.cooperate()
        
        class ActiveActorMonitor(actor.Actor):
            def main(self):
                activea = actor.spawn(ActiveActor)
                cycle1 = activea.call('get_cycle')
                self.sleep(0.001)
                cycle2 = activea.call('get_cycle')
                activea.call('die')
                return cycle2 > cycle1

        self.assertEquals(actor.spawn(ActiveActorMonitor).wait(), True)
Example #12
0
 def test_linked_wait(self):
     """Spawn Supervisor, which calls spawn_link with
     a function that returns 2 + 2 and returns the result extracted
     out of the link exit.
     """
     result = actor.spawn(Supervisor).wait()
     self.assertEquals(result, 4)
Example #13
0
    def test_dead_actor(self):
        class DeadTest(actor.Actor):
            def main(self):
                child = actor.spawn(foo)
                child.wait()
                child.cast({'hello': 'there'})

        self.assertRaises(actor.DeadActor, actor.spawn(DeadTest).wait)
Example #14
0
    def test_dead_actor(self):
        class DeadTest(actor.Actor):
            def main(self):
                child = actor.spawn(foo)
                child.wait()
                child.cast({'hello': 'there'})

        self.assertRaises(actor.DeadActor, actor.spawn(DeadTest).wait)
Example #15
0
            def main(self):
                address = actor.spawn(forever)
                try:
                    address.call('method', {}, 0.1)
                except eventlet.TimeoutError:
                    pass

                eventlet.spawn_after(1, lambda: address.kill())
                return address.wait()
Example #16
0
    def test_unconditional_receive(self):
        """Assert that calling receive with no arguments properly selects
        messages from the Actor's mailbox.
        """
        class UnconditionalSupervisor(Supervisor):
            def do_receive(self, address):
                return self.receive()

        result = actor.spawn(UnconditionalSupervisor).wait()
Example #17
0
    def test_unconditional_receive(self):
        """Assert that calling receive with no arguments properly selects
        messages from the Actor's mailbox.
        """
        class UnconditionalSupervisor(Supervisor):
            def do_receive(self, address):
                return self.receive()

        result = actor.spawn(UnconditionalSupervisor).wait()
Example #18
0
            def main(self):
                address = actor.spawn(forever)
                try:
                    address.call('method', {}, 0.1)
                except gevent.Timeout:
                    pass

                gevent.spawn_later(1, lambda : address.kill())
                return address.wait()
Example #19
0
            def main(self):
                address = actor.spawn(forever)
                try:
                    address.call('method', {}, 0.1)
                except gevent.Timeout:
                    pass

                gevent.spawn_later(1, lambda: address.kill())
                return address.wait()
Example #20
0
    def test_linked_exception(self):
        """Spawn an Actor which calls spawn_link with a function that
        raises an exception. The ExceptionSupervisor will get a link message
        when the exception occurs, at which point it returns the
        EXCEPTION_MARKER.
        """
        class ExceptionSupervisor(Supervisor):
            child_type = property(lambda self: exception)

        result = actor.spawn(ExceptionSupervisor).wait()
        self.assertEquals(result, EXCEPTION_MARKER)
Example #21
0
    def test_linked_exception(self):
        """Spawn an Actor which calls spawn_link with a function that
        raises an exception. The ExceptionSupervisor will get a link message
        when the exception occurs, at which point it returns the
        EXCEPTION_MARKER.
        """
        class ExceptionSupervisor(Supervisor):
            child_type = property(lambda self: exception)

        result = actor.spawn(ExceptionSupervisor).wait()
        self.assertEquals(result, EXCEPTION_MARKER)
Example #22
0
 def main(self):
     child = actor.spawn(foo)
     child.link()
     cancel = eventlet.Timeout(0.1, eventlet.TimeoutError)
     result = self.receive({
         'exit': object,
         'address': object
     }, {
         'exception': object,
         'address': object
     })
Example #23
0
 def test_call_getattr(self):
     """Test addr.method(...) call pattern
     """
     class CallChild(actor.Actor):
         def main(self):
             pat,msg = self.receive({'call':str, 'address':object,
                                     'method':str, 'message':object})
             if msg['method'] == 'method':
                 self.respond(msg,'my response')
     class CallParent(actor.Actor):
         def main(self):
             return actor.spawn(CallChild).method()
     self.assertEquals(actor.spawn(CallParent).wait(), 'my response')
Example #24
0
    def test_receive_timeout_no_patterns(self):
        """Assert that calling with a timeout > 0 and no patterns
        """
        class TimingOutActor(actor.Actor):
            def main(self):
                self.touts = 0
                while True:
                    self.receive(timeout=0.01)
                    self.touts += 1
                    if self.touts == 3:
                        return True

        self.assertEquals(actor.spawn(TimingOutActor).wait(), True)
Example #25
0
 def test_call_invalid_method(self):
     """Start an Actor that starts another Actor and then
     uses call on the Address but using an invalid method. An
     invalid method response should be returned.
     """
     class CallChild(actor.Actor):
         def main(self):
             pat,msg = self.receive(actor.CALL_PATTERN)
             self.respond_invalid_method(msg,msg['method'])
     class CallParent(actor.Actor):
         def main(self):
             return actor.spawn(CallChild).call('invalmeth')
     self.assertRaises(actor.RemoteAttributeError, actor.spawn(CallParent).wait)
Example #26
0
 def test_binary_handling(self):
     """Assert that handling of binary blobs in messages works
     """
     class BinaryReceiver(actor.Actor):
         def main(self):
             pat,msg = self.receive()
             return msg
     class BinarySupervisor(actor.Actor):
         def main(self):
             receiver = actor.spawn(BinaryReceiver)
             receiver | actor.Binary('\x00\xffaa')
             return receiver.wait()
     self.assertEquals(actor.spawn(BinarySupervisor).wait(), actor.Binary('\x00\xffaa'))
Example #27
0
    def test_receive_timeout_no_patterns(self):
        """Assert that calling with a timeout > 0 and no patterns
        """
        class TimingOutActor(actor.Actor):
            def main(self):
                self.touts = 0
                while True:
                    self.receive(timeout=0.01)
                    self.touts += 1
                    if self.touts == 3:
                        return True

        self.assertEquals(actor.spawn(TimingOutActor).wait(), True)
Example #28
0
    def test_call(self):
        """Start an Actor that starts another Actor and then uses
        call on the Address. Assert that the parent gets a response
        from the child and returns it.
        """
        class CallChild(actor.Actor):
            def main(self):
                pattern, message = self.receive(
                    {'call': str, 'address': object, 'method': str, 'message': object})
                message['address'].cast(
                    {'response': message['call'], 'message': 'Hi There'})

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('method', {})

        self.assertEquals(actor.spawn(CallParent).wait(), "Hi There")

        class TimeoutCallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('method', {}, 1)

        self.assertEquals(actor.spawn(TimeoutCallParent).wait(), "Hi There")
Example #29
0
 def test_call_response_method(self):
     """Start an Actor that starts another Actor and then uses
     call on the Address. Response is send back using the response() method. 
     Assert that the  parent gets a response from the child and returns it.
     """
     class CallChild(actor.Actor):
         def main(self):
             pat,msg = self.receive({'call':str, 'address':object,
                                     'method':str, 'message':object})
             if msg['method'] == 'method':
                 self.respond(msg,'Hi There')
     class CallParent(actor.Actor):
         def main(self):
             return actor.spawn(CallChild).call('method')
     self.assertEquals(actor.spawn(CallParent).wait(), 'Hi There')
Example #30
0
    def test_actor_linked_to_actor(self):
        """Start an Actor which calls spawn_link on another actor. When
        ChildSupervisor gets the link exit message, it returns the result.
        Assert that calling wait on ChildSupervisor results in the return
        result of Child.
        """
        class Child(actor.Actor):
            def main(self):
                return "Hi There"

        class ChildSupervisor(Supervisor):
            child_type = Child

        result = actor.spawn(ChildSupervisor).wait()
        self.assertEquals(result, "Hi There")
Example #31
0
    def test_binary_handling(self):
        """Assert that handling of binary blobs in messages works
        """
        class BinaryReceiver(actor.Actor):
            def main(self):
                pat, msg = self.receive()
                return msg

        class BinarySupervisor(actor.Actor):
            def main(self):
                receiver = actor.spawn(BinaryReceiver)
                receiver | actor.Binary('\x00\xffaa')
                return receiver.wait()

        self.assertEquals(
            actor.spawn(BinarySupervisor).wait(), actor.Binary('\x00\xffaa'))
Example #32
0
    def test_actor_linked_to_actor(self):
        """Start an Actor which calls spawn_link on another actor. When
        ChildSupervisor gets the link exit message, it returns the result.
        Assert that calling wait on ChildSupervisor results in the return
        result of Child.
        """
        class Child(actor.Actor):
            def main(self):
                return "Hi There"
        
        
        class ChildSupervisor(Supervisor):
            child_type = Child

        result = actor.spawn(ChildSupervisor).wait()
        self.assertEquals(result, "Hi There")
Example #33
0
    def test_timeout(self):
        """Start an Actor that starts another Actor that accepts a call and
        never responds. The parent calls the child with a small timeout value.
        Assert that waiting for the parent raises a TimeoutError.
        """
        class TimeoutChild(actor.Actor):
            def main(self):
                pattern, message = self.receive(
                    {'call': str, 'address': object, 'message': object})
                # Don't respond
        
        class TimeoutParent(actor.Actor):
            def main(self):
                return actor.spawn(TimeoutChild).call('method', {}, timeout=0.1)

        self.assertRaises(gevent.Timeout, actor.spawn(TimeoutParent).wait)
Example #34
0
    def test_call_invalid_method(self):
        """Start an Actor that starts another Actor and then
        uses call on the Address but using an invalid method. An
        invalid method response should be returned.
        """
        class CallChild(actor.Actor):
            def main(self):
                pat, msg = self.receive(actor.CALL_PATTERN)
                self.respond_invalid_method(msg, msg['method'])

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('invalmeth')

        self.assertRaises(actor.RemoteAttributeError,
                          actor.spawn(CallParent).wait)
Example #35
0
    def test_kill(self):
        def forever(receive):
            eventlet.sleep(5000)

        class KillTest(actor.Actor):
            def main(self):
                address = actor.spawn(forever)
                try:
                    address.call('method', {}, 0.1)
                except eventlet.TimeoutError:
                    pass

                eventlet.spawn_after(1, lambda: address.kill())
                return address.wait()

        self.assertRaises(actor.Killed, actor.spawn(KillTest).wait)
Example #36
0
    def test_kill(self):
        def forever(receive):
            gevent.sleep(5000)

        class KillTest(actor.Actor):
            def main(self):
                address = actor.spawn(forever)
                try:
                    address.call('method', {}, 0.1)
                except gevent.Timeout:
                    pass

                gevent.spawn_later(1, lambda : address.kill())
                return address.wait()

        self.assertRaises(actor.Killed, actor.spawn(KillTest).wait)
Example #37
0
    def test_call_getattr(self):
        """Test addr.method(...) call pattern
        """
        class CallChild(actor.Actor):
            def main(self):
                pat, msg = self.receive({
                    'call': str,
                    'address': object,
                    'method': str,
                    'message': object
                })
                if msg['method'] == 'method':
                    self.respond(msg, 'my response')

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).method()

        self.assertEquals(actor.spawn(CallParent).wait(), 'my response')
Example #38
0
    def test_call_response_method(self):
        """Start an Actor that starts another Actor and then uses
        call on the Address. Response is send back using the response() method. 
        Assert that the  parent gets a response from the child and returns it.
        """
        class CallChild(actor.Actor):
            def main(self):
                pat, msg = self.receive({
                    'call': str,
                    'address': object,
                    'method': str,
                    'message': object
                })
                if msg['method'] == 'method':
                    self.respond(msg, 'Hi There')

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('method')

        self.assertEquals(actor.spawn(CallParent).wait(), 'Hi There')
Example #39
0
    def test_wait_all(self):
        class WaitAll(actor.Actor):
            def main(self):
                def foo(receive):
                    return 1
                def bar(receive):
                    return 2
                def baz(receive):
                    return 3
                result1 = list(actor.wait_all(foo, bar, baz))
                result2 = list(actor.wait_all([foo, bar, baz]))
                return result1, result2

        cancel = gevent.Timeout(1)
        result1, result2 = actor.spawn(WaitAll).wait()
        cancel.cancel()

        result1 = [x.get('exit') for x in result1]
        result2 = [x.get('exit') for x in result2]
        self.assertEquals([1,2,3], result1)
        self.assertEquals([1,2,3], result2)
Example #40
0
    def test_timeout(self):
        """Start an Actor that starts another Actor that accepts a call and
        never responds. The parent calls the child with a small timeout value.
        Assert that waiting for the parent raises a TimeoutError.
        """
        class TimeoutChild(actor.Actor):
            def main(self):
                pattern, message = self.receive({
                    'call': str,
                    'address': object,
                    'message': object
                })
                # Don't respond

        class TimeoutParent(actor.Actor):
            def main(self):
                return actor.spawn(TimeoutChild).call('method', {},
                                                      timeout=0.1)

        self.assertRaises(eventlet.TimeoutError,
                          actor.spawn(TimeoutParent).wait)
Example #41
0
    def test_wait_all(self):
        class WaitAll(actor.Actor):
            def main(self):
                def foo(receive):
                    return 1

                def bar(receive):
                    return 2

                def baz(receive):
                    return 3

                result1 = list(actor.wait_all(foo, bar, baz))
                result2 = list(actor.wait_all([foo, bar, baz]))
                return result1, result2

        cancel = eventlet.Timeout(1, eventlet.TimeoutError)
        result1, result2 = actor.spawn(WaitAll).wait()
        cancel.cancel()

        result1 = [x.get('exit') for x in result1]
        result2 = [x.get('exit') for x in result2]
        self.assertEquals([1, 2, 3], result1)
        self.assertEquals([1, 2, 3], result2)
Example #42
0
    def test_receive_times_out(self):
        """Assert that calling with a timeout > 0.
        """
        class ActiveActor(actor.Actor):
            def main(self):
                self.touts = 0
                while True:
                    pat,msg = self.receive(actor.CALL_PATTERN,timeout=0.01)
                    if pat is None:
                        self.touts += 1
                    else:
                        if msg['method'] == 'die':
                            msg['address'] | {'response':msg['call'],
                                              'message':self.touts}
                            return
        
        class ActiveActorMonitor(actor.Actor):
            def main(self):
                activea = actor.spawn(ActiveActor)
                self.sleep(0.1)
                touts = activea.die()
                return touts > 3

        self.assertEquals(actor.spawn(ActiveActorMonitor).wait(), True)
Example #43
0
class TestActor(unittest.TestCase):
    def test_basic_actor(self):
        self.assertRaises(NotImplementedError, actor.spawn(actor.Actor).wait)

    def test_wait(self):
        """Call spawn with a function that returns 2 + 2.
        Assert that wait returns the value the function returns.
        """
        result = actor.spawn(foo).wait()
        self.assertEquals(result, 4)

    def test_linked_wait(self):
        """Spawn Supervisor, which calls spawn_link with
        a function that returns 2 + 2 and returns the result extracted
        out of the link exit.
        """
        result = actor.spawn(Supervisor).wait()
        self.assertEquals(result, 4)

    def test_wait_exception(self):
        """Call spawn with a function that raises an exception, and assert
        that calling wait raises the same exception.
        """
        self.assertRaises(RuntimeError, actor.spawn(exception).wait)

    def test_linked_exception(self):
        """Spawn an Actor which calls spawn_link with a function that
        raises an exception. The ExceptionSupervisor will get a link message
        when the exception occurs, at which point it returns the
        EXCEPTION_MARKER.
        """
        class ExceptionSupervisor(Supervisor):
            child_type = property(lambda self: exception)

        result = actor.spawn(ExceptionSupervisor).wait()
        self.assertEquals(result, EXCEPTION_MARKER)

    def test_actor_linked_to_actor(self):
        """Start an Actor which calls spawn_link on another actor. When
        ChildSupervisor gets the link exit message, it returns the result.
        Assert that calling wait on ChildSupervisor results in the return
        result of Child.
        """
        class Child(actor.Actor):
            def main(self):
                return "Hi There"

        class ChildSupervisor(Supervisor):
            child_type = Child

        result = actor.spawn(ChildSupervisor).wait()
        self.assertEquals(result, "Hi There")

    def test_unconditional_receive(self):
        """Assert that calling receive with no arguments properly selects
        messages from the Actor's mailbox.
        """
        class UnconditionalSupervisor(Supervisor):
            def do_receive(self, address):
                return self.receive()

        result = actor.spawn(UnconditionalSupervisor).wait()

    def test_cast_syntax_sugar(self):
        """Test | as cast operator.
        """
        class Replier(actor.Actor):
            def main(self):
                pat, msg = self.receive({'addr': object})
                reqaddr = msg['addr']
                reqaddr | "quux"

        class Requester(actor.Actor):
            def main(self):
                repladdr = Replier.spawn()
                repladdr | {"addr": self.address}
                pat, msg = self.receive(str)
                return msg

        result = Requester.spawn().wait()
        self.assertEquals(result, "quux")

    def test_cast_object_json_protocol(self):
        """Test _as_json_obj() method for casting
        """
        class Msg1(object):
            def __init__(self, x, addr):
                self.x = x
                self.addr = addr

            def _as_json_obj(self):
                return {'x': self.x, 'addr': self.addr}

        class Replier(actor.Actor):
            def main(self):
                pat, msg = self.receive({'x': int, 'addr': object})
                msg['addr'] | "ok"

        class Requester(actor.Actor):
            def main(self):
                repladdr = Replier.spawn()
                repladdr | Msg1(91, self.address)
                pat, msg = self.receive(str)
                return msg

        result = Requester.spawn().wait()
        self.assertEquals(result, "ok")

    def test_receive_nowait(self):
        """Assert that calling receive with timeout = 0 works.
        """
        class ActiveActor(actor.Actor):
            def main(self):
                self.cycle = 0
                while True:
                    pat, msg = self.receive(actor.CALL_PATTERN, timeout=0)
                    if pat and msg['method'] == 'get_cycle':
                        msg['address'].cast({
                            'response': msg['call'],
                            'message': self.cycle
                        })
                    if pat and msg['method'] == 'die':
                        msg['address'].cast({
                            'response': msg['call'],
                            'message': None
                        })
                        return
                    self.cycle += 1
                    self.cooperate()

        class ActiveActorMonitor(actor.Actor):
            def main(self):
                activea = actor.spawn(ActiveActor)
                cycle1 = activea.call('get_cycle')
                self.sleep(0.001)
                cycle2 = activea.call('get_cycle')
                activea.call('die')
                return cycle2 > cycle1

        self.assertEquals(actor.spawn(ActiveActorMonitor).wait(), True)

    def test_binary_class(self):
        """Test binary blob creation and comparison
        """
        v = '\x00MM\xff'
        b1 = actor.Binary(v)
        b2 = actor.Binary(v)
        assert b1.value == v
        assert b1 == b2
        assert b1 == v
        assert b1.to_json() == {'_pyact_binary': base64.b64encode(v)}
        assert actor.Binary.from_json({'_pyact_binary':
                                       base64.b64encode(v)}) == b1

    def test_binary_handling(self):
        """Assert that handling of binary blobs in messages works
        """
        class BinaryReceiver(actor.Actor):
            def main(self):
                pat, msg = self.receive()
                return msg

        class BinarySupervisor(actor.Actor):
            def main(self):
                receiver = actor.spawn(BinaryReceiver)
                receiver | actor.Binary('\x00\xffaa')
                return receiver.wait()

        self.assertEquals(
            actor.spawn(BinarySupervisor).wait(), actor.Binary('\x00\xffaa'))

    def test_receive_times_out(self):
        """Assert that calling with a timeout > 0.
        """
        class ActiveActor(actor.Actor):
            def main(self):
                self.touts = 0
                while True:
                    pat, msg = self.receive(actor.CALL_PATTERN, timeout=0.01)
                    if pat is None:
                        self.touts += 1
                    else:
                        if msg['method'] == 'die':
                            msg['address'] | {
                                'response': msg['call'],
                                'message': self.touts
                            }
                            return

        class ActiveActorMonitor(actor.Actor):
            def main(self):
                activea = actor.spawn(ActiveActor)
                self.sleep(0.1)
                touts = activea.die()
                return touts > 3

        self.assertEquals(actor.spawn(ActiveActorMonitor).wait(), True)

    def test_receive_timeout_no_patterns(self):
        """Assert that calling with a timeout > 0 and no patterns
        """
        class TimingOutActor(actor.Actor):
            def main(self):
                self.touts = 0
                while True:
                    self.receive(timeout=0.01)
                    self.touts += 1
                    if self.touts == 3:
                        return True

        self.assertEquals(actor.spawn(TimingOutActor).wait(), True)

    def test_call(self):
        """Start an Actor that starts another Actor and then uses
        call on the Address. Assert that the parent gets a response
        from the child and returns it.
        """
        class CallChild(actor.Actor):
            def main(self):
                pattern, message = self.receive({
                    'call': str,
                    'address': object,
                    'method': str,
                    'message': object
                })
                message['address'].cast({
                    'response': message['call'],
                    'message': 'Hi There'
                })

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('method', {})

        self.assertEquals(actor.spawn(CallParent).wait(), "Hi There")

        class TimeoutCallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('method', {}, 1)

        self.assertEquals(actor.spawn(TimeoutCallParent).wait(), "Hi There")

    def test_call_response_method(self):
        """Start an Actor that starts another Actor and then uses
        call on the Address. Response is send back using the response() method. 
        Assert that the  parent gets a response from the child and returns it.
        """
        class CallChild(actor.Actor):
            def main(self):
                pat, msg = self.receive({
                    'call': str,
                    'address': object,
                    'method': str,
                    'message': object
                })
                if msg['method'] == 'method':
                    self.respond(msg, 'Hi There')

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('method')

        self.assertEquals(actor.spawn(CallParent).wait(), 'Hi There')

    def test_call_getattr(self):
        """Test addr.method(...) call pattern
        """
        class CallChild(actor.Actor):
            def main(self):
                pat, msg = self.receive({
                    'call': str,
                    'address': object,
                    'method': str,
                    'message': object
                })
                if msg['method'] == 'method':
                    self.respond(msg, 'my response')

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).method()

        self.assertEquals(actor.spawn(CallParent).wait(), 'my response')

    def test_call_invalid_method(self):
        """Start an Actor that starts another Actor and then
        uses call on the Address but using an invalid method. An
        invalid method response should be returned.
        """
        class CallChild(actor.Actor):
            def main(self):
                pat, msg = self.receive(actor.CALL_PATTERN)
                self.respond_invalid_method(msg, msg['method'])

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('invalmeth')

        self.assertRaises(actor.RemoteAttributeError,
                          actor.spawn(CallParent).wait)

    def test_call_with_remote_exception(self):
        """Start an Actor that starts another actor and calls a method
        on it. The first actor will respond with an exception.
        """
        class CallChild(actor.Actor):
            def main(self):
                pat, msg = self.receive(actor.CALL_PATTERN)
                try:
                    raise ValueError("testexc")
                except ValueError, e:
                    formatted = exc.format_exc()
                    self.respond_exception(msg, formatted)

        class CallParent(actor.Actor):
            def main(self):
                return actor.spawn(CallChild).call('amethod')

        self.assertRaises(actor.RemoteException, actor.spawn(CallParent).wait)
Example #44
0
 def main(self):
     return actor.spawn(CallChild).call('amethod')
Example #45
0
 def test_basic_actor(self):
     self.assertRaises(NotImplementedError, actor.spawn(actor.Actor).wait)
Example #46
0
 def main(self):
     child = actor.spawn(foo)
     child.wait()
     child.cast({'hello': 'there'})
Example #47
0
 def main(self):
     activea = actor.spawn(ActiveActor)
     self.sleep(0.1)
     touts = activea.die()
     return touts > 3
Example #48
0
 def main(self):
     return actor.spawn(TimeoutChild).call('method', {},
                                           timeout=0.1)
Example #49
0
 def main(self):
     return actor.spawn(CallChild).call('amethod')
Example #50
0
 def main(self):
     return actor.spawn(CallChild).call('invalmeth')
Example #51
0
 def main(self):
     return actor.spawn(CallChild).call('invalmeth')
Example #52
0
 def test_wait_exception(self):
     """Call spawn with a function that raises an exception, and assert
     that calling wait raises the same exception.
     """
     self.assertRaises(RuntimeError, actor.spawn(exception).wait)
Example #53
0
 def test_basic_actor(self):
     self.assertRaises(NotImplementedError, actor.spawn(actor.Actor).wait)
Example #54
0
 def test_wait(self):
     """Call spawn with a function that returns 2 + 2.
     Assert that wait returns the value the function returns.
     """
     result = actor.spawn(foo).wait()
     self.assertEquals(result, 4)
Example #55
0
 def main(self):
     child = actor.spawn(foo)
     child.wait()
     child.cast({'hello': 'there'})
Example #56
0
 def test_wait_exception(self):
     """Call spawn with a function that raises an exception, and assert
     that calling wait raises the same exception.
     """
     self.assertRaises(RuntimeError, actor.spawn(exception).wait)
Example #57
0
 def test_wait(self):
     """Call spawn with a function that returns 2 + 2.
     Assert that wait returns the value the function returns.
     """
     result = actor.spawn(foo).wait()
     self.assertEquals(result, 4)
Example #58
0
 def main(self):
     return actor.spawn(TimeoutChild).call('method', {}, timeout=0.1)
Example #59
0
 def main(self):
     return actor.spawn(CallChild).method()