Exemplo n.º 1
0
    def do_customEvent(self, reactor_handler, event_root):
        class CustomHandler:
            did_custom = False
            did_init = False

            def __init__(self, *handlers):
                self.handlers = handlers

            def on_reactor_init(self, event):
                self.did_init = True

            def on_custom(self, event):
                self.did_custom = True

        class CustomInvoker(CustomHandler):
            def on_reactor_init(self, event):
                h = event_root(event)
                event.dispatch(h, CUSTOM)
                self.did_init = True

        child = CustomInvoker()
        root = CustomHandler(child)

        reactor = Reactor()

        reactor_handler(reactor, root)
        reactor.run()
        assert root.did_init
        assert child.did_init
        assert root.did_custom
        assert child.did_custom
Exemplo n.º 2
0
    def test_reactorHandlerCycling(self, n=0):
        class CustomHandler(Handler):
            UNSET = 999999999

            def __init__(self):
                self.offset = len(traceback.extract_stack())

            def on_reactor_init(self, event):
                self.depth = len(traceback.extract_stack())

            def reset(self):
                self.depth = self.UNSET

            @property
            def init_depth(self):
                d = self.depth - self.offset
                return d

        custom = CustomHandler()

        reactor = Reactor()
        reactor.handler = custom
        for i in range(n):
            h = reactor.handler
            reactor.handler = h
        custom.reset()
        reactor.run()
        assert custom.init_depth < 50, "Unexpectedly long traceback for a simple handler"
Exemplo n.º 3
0
  def do_customEvent(self, reactor_handler, event_root):

    class CustomHandler:
      did_custom = False
      did_init = False
      def __init__(self, *handlers):
        self.handlers = handlers
      def on_reactor_init(self, event):
        self.did_init = True
      def on_custom(self, event):
        self.did_custom = True

    class CustomInvoker(CustomHandler):
      def on_reactor_init(self, event):
        h = event_root(event)
        event.dispatch(h, CUSTOM)
        self.did_init = True

    child = CustomInvoker()
    root = CustomHandler(child)

    reactor = Reactor()

    reactor_handler(reactor, root)
    reactor.run()
    assert root.did_init
    assert child.did_init
    assert root.did_custom
    assert child.did_custom
Exemplo n.º 4
0
  def protonj_to_protonc(self, count):
    if (not self.proton_j_available):
      raise Skipped("ProtonJ not found")

    rh = ReceiveHandler(count)
    r = Reactor(rh)
    r.run()

    rh.java_thread.join()
    assert(rh.java_thread.result == 0)

    for i in range(1, count):
      assert(rh.messages[i-1] == ("message-" + str(i)))
Exemplo n.º 5
0
    def protonj_to_protonc(self, count):
        if (not self.proton_j_available):
            raise Skipped("ProtonJ not found")

        rh = ReceiveHandler(count)
        r = Reactor(rh)
        r.run()

        rh.java_thread.join()
        assert (rh.java_thread.result == 0)

        for i in range(1, count):
            assert (rh.messages[i - 1] == ("message-" + str(i)))
Exemplo n.º 6
0
class SourceTest(TestCase):

    def setUp(self):
        self.source = Source("test-%s")
        self.server = Server(self.source)
        self.reactor = Reactor(self.server)
        
    def tearDown(self):
      pass

    def testReceiver(self, count=1):
        self.server.max_connections = 1
        self.source.limit = count

        oself = self

        class Counter:
            def __init__(self):
                self.received = 0
                self.receiver = None
                self.open_event = False

            def on_link_remote_open(self, event):
                self.open_event = True

            def on_message(self, event):
                assert event.message.body == oself.source.template % self.received
                self.received += 1
                if self.received == count:
                    self.receiver.stop(event.reactor)

        counter = Counter()
        rcv = Receiver("//localhost:%s" % PORT, Processor(counter))
        counter.receiver = rcv
        self.reactor.handler.add(rcv)
        rcv.start(self.reactor)
        self.reactor.run()
        assert counter.received == count
        assert counter.open_event

    def testReceiver150(self):
        self.testReceiver(150)

    # This doesn't seem to work for any value > 150! This is a proton
    # bug that needs to be isolated and pushed upstream. Enable and
    # run under valgrind for more details.
    def XXXtestReceiver151(self):
        self.testReceiver(151)
Exemplo n.º 7
0
class SourceTest(TestCase):
    def setUp(self):
        self.source = Source("test-%s")
        self.server = Server(self.source)
        self.reactor = Reactor(self.server)

    def tearDown(self):
        pass

    def testReceiver(self, count=1):
        self.server.max_connections = 1
        self.source.limit = count

        oself = self

        class Counter:
            def __init__(self):
                self.received = 0
                self.receiver = None
                self.open_event = False

            def on_link_remote_open(self, event):
                self.open_event = True

            def on_message(self, event):
                assert event.message.body == oself.source.template % self.received
                self.received += 1
                if self.received == count:
                    self.receiver.stop(event.reactor)

        counter = Counter()
        rcv = Receiver("//localhost:%s" % PORT, Processor(counter))
        counter.receiver = rcv
        self.reactor.handler.add(rcv)
        rcv.start(self.reactor)
        self.reactor.run()
        assert counter.received == count
        assert counter.open_event

    def testReceiver150(self):
        self.testReceiver(150)

    # This doesn't seem to work for any value > 150! This is a proton
    # bug that needs to be isolated and pushed upstream. Enable and
    # run under valgrind for more details.
    def XXXtestReceiver151(self):
        self.testReceiver(151)
Exemplo n.º 8
0
  def protonc_to_protonj(self, count):
    if (not self.proton_j_available):
      raise Skipped("ProtonJ not found")

    port = free_tcp_port()
    java_thread = JavaThread("recv", port, count)
    java_thread.start()
    # Give the Java thread time to spin up a JVM and start listening
    # XXX: would be better to parse the stdout output for a message
    time.sleep(1)

    sh = SendHandler('127.0.0.1:' + str(port), count)
    r = Reactor(sh)
    r.run()

    java_thread.join()
    assert(java_thread.result == 0)
Exemplo n.º 9
0
    def protonc_to_protonj(self, count):
        if (not self.proton_j_available):
            raise Skipped("ProtonJ not found")

        port = free_tcp_port()
        java_thread = JavaThread("recv", port, count)
        java_thread.start()
        # Give the Java thread time to spin up a JVM and start listening
        # XXX: would be better to parse the stdout output for a message
        time.sleep(1)

        sh = SendHandler('127.0.0.1:' + str(port), count)
        r = Reactor(sh)
        r.run()

        java_thread.join()
        assert (java_thread.result == 0)
Exemplo n.º 10
0
class AgentTest(TestCase):
    def setUp(self):
        fake_tether = Tether(None, "", None)
        self.agent = Agent(fake_tether, self)
        self.server = Server(self.agent)
        self.reactor = Reactor(self.server)
        self.samples = 0

    def tearDown(self):
        if self.server.acceptor:
            self.server.acceptor.close()

    def sample(self, stats):
        stats["samples"] = self.samples
        self.samples += 1

    def testAgent(self, count=1, frequency=10):
        self.server.max_connections = 1
        self.agent.sampler.frequency = frequency

        class Counter(Timeout):
            def __init__(self):
                self.received = 0

            def on_message(self, event):
                m = event.message
                assert event.message.body["samples"] == self.received, (
                    event.message.body, self.received)
                self.received += 1
                if self.received == count:
                    rcv.stop(event.reactor)
                    self.cancel()

            def on_timer_task(self, event):
                rcv.stop(event.reactor)

        counter = Counter()
        rcv = Receiver("//localhost:%s" % PORT, Processor(counter))
        rcv.start(self.reactor)
        counter.set_timeout(self.reactor, 20)
        self.reactor.run()
        assert counter.cancelled, "Sampling timed out"

    def testAgent10M100F(self):
        self.testAgent(10, 100)
Exemplo n.º 11
0
class AgentTest(TestCase):

    def setUp(self):
        fake_tether = Tether(None, "", None)
        self.agent = Agent(fake_tether, self)
        self.server = Server(self.agent)
        self.reactor = Reactor(self.server)
        self.samples = 0

    def tearDown(self):
        if self.server.acceptor:
            self.server.acceptor.close()

    def sample(self, stats):
        stats["samples"] = self.samples
        self.samples += 1

    def testAgent(self, count=1, frequency=10):
        self.server.max_connections = 1
        self.agent.sampler.frequency = frequency
        class Counter(Timeout):
            def __init__(self):
                self.received = 0
            def on_message(self, event):
                m = event.message
                assert event.message.body["samples"] == self.received, (event.message.body, self.received)
                self.received += 1
                if self.received == count:
                    rcv.stop(event.reactor)
                    self.cancel()
            def on_timer_task(self, event):
              rcv.stop(event.reactor)
        
        counter = Counter()
        rcv = Receiver("//localhost:%s" % PORT, Processor(counter))
        rcv.start(self.reactor)
        counter.set_timeout(self.reactor, 20)
        self.reactor.run()
        assert counter.cancelled, "Sampling timed out"

    def testAgent10M100F(self):
        self.testAgent(10, 100)
Exemplo n.º 12
0
class AgentTest:

    def __init__(self):
        class FakeTether:
            def __init__(self):
                self.address = None
                self.agent = None
                self.agent_type = None
        self.agent = Agent(FakeTether(), self)
        self.server = Server(self.agent)
        self.reactor = Reactor(self.server)
        self.samples = 0

    def teardown(self):
        self.server.acceptor.close()

    def sample(self, stats):
        stats["samples"] = self.samples
        self.samples += 1

    def testAgent(self, count=1, frequency=10):
        self.server.max_connections = 1
        self.agent.sampler.frequency = frequency
        class Counter:
            def __init__(self):
                self.received = 0
            def on_message(self, event):
                assert event.message.body["samples"] == self.received, (event.message.body, self.received)
                self.received += 1
                if self.received == count:
                    rcv.stop(event.reactor)
        rcv = Receiver("//localhost:%s" % PORT, Processor(Counter()))
        rcv.start(self.reactor)
        self.reactor.run()

    def testAgent10M100F(self):
        self.testAgent(10, 100)
Exemplo n.º 13
0
  def test_reactorHandlerCycling(self, n=0):

    class CustomHandler(Handler):
      UNSET = 999999999
      def __init__(self):
        self.offset = len(traceback.extract_stack())
      def on_reactor_init(self, event):
        self.depth = len(traceback.extract_stack())
      def reset(self):
        self.depth = self.UNSET
      @property
      def init_depth(self):
        d = self.depth - self.offset
        return d
    custom = CustomHandler()

    reactor = Reactor()
    reactor.handler = custom
    for i in range(n):
      h = reactor.handler
      reactor.handler = h
    custom.reset()
    reactor.run()
    assert custom.init_depth < 50, "Unexpectedly long traceback for a simple handler"
Exemplo n.º 14
0
from proton.reactor import Reactor

class Program:

    def on_reactor_init(self, event):
        self.start = time.time()
        print "Hello, World!"

        # We can schedule a task event for some point in the future.
        # This will cause the reactor to stick around until it has a
        # chance to process the event.

        # The first argument is the delay. The second argument is the
        # handler for the event. We are just using self for now, but
        # we could pass in another object if we wanted.
        task = event.reactor.schedule(1.0, self)

        # We can ignore the task if we want to, but we can also use it
        # to pass stuff to the handler.
        task.something_to_say = "Yay"

    def on_timer_task(self, event):
        task = event.context # xxx: don't have a task property on event yet
        print task.something_to_say, "my task is complete!"

    def on_reactor_final(self, event):
        print "Goodbye, World! (after %s long seconds)" % (time.time() - self.start)

r = Reactor(Program())
r.run()
Exemplo n.º 15
0
class ExceptionTest(Test):
    def setup(self):
        self.reactor = Reactor()

    def test_reactor_final(self):
        self.reactor.global_handler = BarfOnFinal()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_global_set(self):
        self.reactor.global_handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_global_add(self):
        self.reactor.global_handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_reactor_set(self):
        self.reactor.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_reactor_add(self):
        self.reactor.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection(self):
        self.reactor.connection(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection_set(self):
        c = self.reactor.connection()
        c.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection_add(self):
        c = self.reactor.connection()
        c.handler = object()
        c.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_session_set(self):
        c = self.reactor.connection()
        s = c.session()
        s.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_session_add(self):
        c = self.reactor.connection()
        s = c.session()
        s.handler = object()
        s.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_link_set(self):
        c = self.reactor.connection()
        s = c.session()
        l = s.sender("xxx")
        l.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_link_add(self):
        c = self.reactor.connection()
        s = c.session()
        l = s.sender("xxx")
        l.handler = object()
        l.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_schedule(self):
        self.reactor.schedule(0, BarfOnTask())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_schedule_many_nothings(self):
        class Nothing:
            results = []

            def on_timer_task(self, event):
                self.results.append(None)

        num = 12345
        for a in range(num):
            self.reactor.schedule(0, Nothing())
        self.reactor.run()
        assert len(Nothing.results) == num

    def test_schedule_many_nothing_refs(self):
        class Nothing:
            results = []

            def on_timer_task(self, event):
                self.results.append(None)

        num = 12345
        tasks = []
        for a in range(num):
            tasks.append(self.reactor.schedule(0, Nothing()))
        self.reactor.run()
        assert len(Nothing.results) == num

    def test_schedule_many_nothing_refs_cancel_before_run(self):
        class Nothing:
            results = []

            def on_timer_task(self, event):
                self.results.append(None)

        num = 12345
        tasks = []
        for a in range(num):
            tasks.append(self.reactor.schedule(0, Nothing()))
        for task in tasks:
            task.cancel()
        self.reactor.run()
        assert len(Nothing.results) == 0

    def test_schedule_cancel(self):
        barf = self.reactor.schedule(10, BarfOnTask())

        class CancelBarf:
            def __init__(self, barf):
                self.barf = barf

            def on_timer_task(self, event):
                self.barf.cancel()
                pass

        self.reactor.schedule(0, CancelBarf(barf))
        now = self.reactor.mark()
        try:
            self.reactor.run()
            elapsed = self.reactor.mark() - now
            assert elapsed < 10, "expected cancelled task to not delay the reactor by %s" % elapsed
        except Barf:
            assert False, "expected barf to be cancelled"

    def test_schedule_cancel_many(self):
        num = 12345
        barfs = set()
        for a in range(num):
            barf = self.reactor.schedule(10 * (a + 1), BarfOnTask())

            class CancelBarf:
                def __init__(self, barf):
                    self.barf = barf

                def on_timer_task(self, event):
                    self.barf.cancel()
                    barfs.discard(self.barf)
                    pass

            self.reactor.schedule(0, CancelBarf(barf))
            barfs.add(barf)
        now = self.reactor.mark()
        try:
            self.reactor.run()
            elapsed = self.reactor.mark() - now
            assert elapsed < num, "expected cancelled task to not delay the reactor by %s" % elapsed
            assert not barfs, "expected all barfs to be discarded"
        except Barf:
            assert False, "expected barf to be cancelled"
Exemplo n.º 16
0
class ExceptionTest(Test):

    def setUp(self):
        self.reactor = Reactor()

    def test_reactor_final(self):
        self.reactor.global_handler = BarfOnFinal()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_global_set(self):
        self.reactor.global_handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_global_add(self):
        self.reactor.global_handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_reactor_set(self):
        self.reactor.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_reactor_add(self):
        self.reactor.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection(self):
        self.reactor.connection(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection_set(self):
        c = self.reactor.connection()
        c.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection_add(self):
        c = self.reactor.connection()
        c.handler = object()
        c.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_session_set(self):
        c = self.reactor.connection()
        s = c.session()
        s.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_session_add(self):
        c = self.reactor.connection()
        s = c.session()
        s.handler = object()
        s.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_link_set(self):
        c = self.reactor.connection()
        s = c.session()
        l = s.sender("xxx")
        l.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_link_add(self):
        c = self.reactor.connection()
        s = c.session()
        l = s.sender("xxx")
        l.handler = object()
        l.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_schedule(self):
        self.reactor.schedule(0, BarfOnTask())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_schedule_many_nothings(self):
        class Nothing:
            results = []
            def on_timer_task(self, event):
                self.results.append(None)
        num = 12345
        for a in range(num):
            self.reactor.schedule(0, Nothing())
        self.reactor.run()
        assert len(Nothing.results) == num

    def test_schedule_many_nothing_refs(self):
        class Nothing:
            results = []
            def on_timer_task(self, event):
                self.results.append(None)
        num = 12345
        tasks = []
        for a in range(num):
            tasks.append(self.reactor.schedule(0, Nothing()))
        self.reactor.run()
        assert len(Nothing.results) == num

    def test_schedule_many_nothing_refs_cancel_before_run(self):
        class Nothing:
            results = []
            def on_timer_task(self, event):
                self.results.append(None)
        num = 12345
        tasks = []
        for a in range(num):
            tasks.append(self.reactor.schedule(0, Nothing()))
        for task in tasks:
            task.cancel()
        self.reactor.run()
        assert len(Nothing.results) == 0

    def test_schedule_cancel(self):
        barf = self.reactor.schedule(10, BarfOnTask())
        class CancelBarf:
            def __init__(self, barf):
                self.barf = barf
            def on_timer_task(self, event):
                self.barf.cancel()
                pass
        self.reactor.schedule(0, CancelBarf(barf))
        now = self.reactor.mark()
        try:
            self.reactor.run()
            elapsed = self.reactor.mark() - now
            assert elapsed < 10, "expected cancelled task to not delay the reactor by %s" % elapsed
        except Barf:
            assert False, "expected barf to be cancelled"

    def test_schedule_cancel_many(self):
        num = 12345
        barfs = set()
        for a in range(num):
            barf = self.reactor.schedule(10*(a+1), BarfOnTask())
            class CancelBarf:
                def __init__(self, barf):
                    self.barf = barf
                def on_timer_task(self, event):
                    self.barf.cancel()
                    barfs.discard(self.barf)
                    pass
            self.reactor.schedule(0, CancelBarf(barf))
            barfs.add(barf)
        now = self.reactor.mark()
        try:
            self.reactor.run()
            elapsed = self.reactor.mark() - now
            assert elapsed < num, "expected cancelled task to not delay the reactor by %s" % elapsed
            assert not barfs, "expected all barfs to be discarded"
        except Barf:
            assert False, "expected barf to be cancelled"
Exemplo n.º 17
0
class HandlerDerivationTest(Test):
    def setUp(self):
        import platform
        if platform.python_implementation() != "Jython":
          # Exception propagation does not work currently for CPython
          raise SkipTest()
        self.reactor = Reactor()

    def wrong_exception(self):
        import sys
        ex = sys.exc_info()
        assert False, " Unexpected exception " + str(ex[1])
    
    def test_reactor_final_derived(self):
        h = BarfOnFinalDerived()
        self.reactor.global_handler = h
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_final_py_child_py(self):
        class APoorExcuseForAHandler:
            def __init__(self):
                self.handlers = [BarfOnFinal()]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_final_py_child_derived(self):
        class APoorExcuseForAHandler:
            def __init__(self):
                self.handlers = [BarfOnFinalDerived()]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_final_derived_child_derived(self):
        class APoorExcuseForAHandler(CHandshaker):
            def __init__(self):
                CHandshaker.__init__(self)
                self.handlers = [BarfOnFinalDerived()]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_final_derived_child_py(self):
        class APoorExcuseForAHandler(CHandshaker):
            def __init__(self):
                CHandshaker.__init__(self)
                self.handlers = [BarfOnFinal()]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_init_derived(self):
        h = BarfOnFinalDerived()
        self.reactor.global_handler = h
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"

    def test_reactor_init_py_child_py(self):
        h = BarfOnFinal()
        class APoorExcuseForAHandler:
            def __init__(self):
                self.handlers = [h]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"

    def test_reactor_init_py_child_derived(self):
        h = BarfOnFinalDerived()
        class APoorExcuseForAHandler:
            def __init__(self):
                self.handlers = [h]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"

    def test_reactor_init_derived_child_derived(self):
        h = BarfOnFinalDerived()
        class APoorExcuseForAHandler(CHandshaker):
            def __init__(self):
                CHandshaker.__init__(self)
                self.handlers = [h]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"

    def test_reactor_init_derived_child_py(self):
        h = BarfOnFinal()
        class APoorExcuseForAHandler(CHandshaker):
            def __init__(self):
                CHandshaker.__init__(self)
                self.handlers = [h]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"
Exemplo n.º 18
0
class ExceptionTest(Test):

    def setup(self):
        self.reactor = Reactor()

    def test_reactor_final(self):
        self.reactor.global_handler = BarfOnFinal()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_global_set(self):
        self.reactor.global_handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_global_add(self):
        self.reactor.global_handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_reactor_set(self):
        self.reactor.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_reactor_add(self):
        self.reactor.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection(self):
        self.reactor.connection(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection_set(self):
        c = self.reactor.connection()
        c.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection_add(self):
        c = self.reactor.connection()
        c.handler = object()
        c.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_session_set(self):
        c = self.reactor.connection()
        s = c.session()
        s.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_session_add(self):
        c = self.reactor.connection()
        s = c.session()
        s.handler = object()
        s.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_link_set(self):
        c = self.reactor.connection()
        s = c.session()
        l = s.sender("xxx")
        l.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_link_add(self):
        c = self.reactor.connection()
        s = c.session()
        l = s.sender("xxx")
        l.handler = object()
        l.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_schedule(self):
        self.reactor.schedule(0, BarfOnTask())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
Exemplo n.º 19
0
class Logger:
    def on_unhandled(self, name, event):
        print("LOG:", name, event)


class Task:
    def on_timer_task(self, event):
        print("Mission accomplished!")


class Program:
    def on_reactor_init(self, event):
        print("Hello, World!")
        event.reactor.schedule(0, Task())

    def on_reactor_final(self, event):
        print("Goodbye, World!")


r = Reactor(Program())

# In addition to having a regular handler, the reactor also has a
# global handler that sees every event. By adding the Logger to the
# global handler instead of the regular handler, we can log every
# single event that occurs in the system regardless of whether or not
# there are specific handlers associated with the objects that are the
# target of those events.
r.global_handler.add(Logger())
r.run()
Exemplo n.º 20
0
class SinkTest(TestCase):

    def setUp(self):
        self.sink = Sink()
        self.server = Server(self.sink)
        self.reactor = Reactor(self.server)
        
    def tearDown(self):
        pass

    def testSender(self, count=1):
        self.server.max_connections = 1
        snd = Sender("//localhost:%s" % PORT)
        self.reactor.handler.add(snd)
        snd.start(self.reactor)
        for i in range(count):
            snd.send("test-%s" % i)
        snd.close()
        self.reactor.run()
        assert len(self.sink.messages) == count
        for i in range(count):
            assert self.sink.messages[i] == "test-%s" % i, self.sink.messages[i]

    def testSender4K(self):
        self.testSender(4*1024)

    def testSampler(self, count=1, frequency=10):
        self.server.max_connections = 1
        oself = self
        class Gen(Timeout):
            def __init__(self):
                self.sent = 0

            def on_sample(self, event):
                event.link.send(Message("test-%s" % self.sent))
                self.sent += 1
                if self.sent >= count:
                    event.link.close()
                    self.cancel()
            def on_timer_task(self, event):
              snd.stop(event.reactor)
        gen = Gen();
        snd = Sender("//localhost:%s" % PORT, Sampler(gen, frequency))
        self.reactor.handler.add(snd)
        gen.set_timeout(self.reactor, 2)
        snd.start(self.reactor)
        self.reactor.run()
        assert gen.cancelled, "Sampling timed out"
        assert len(self.sink.messages) == count, len(self.sink.messages)
        for i in range(count):
            assert self.sink.messages[i] == "test-%s" % i

    def testSampler100M100F(self):
        self.testSampler(100, 100)

    def testLinker(self, address_count=1, message_count=1):
        self.server.max_connections = address_count
        linker = Linker()
        linker.start(self.reactor)
        for i in range(address_count):
            for j in range(message_count):
                snd = linker.sender("//localhost:%s/%s" % (PORT, i))
                assert len(linker.senders) == i + 1
                snd.send(dict(i=i, j=j))
        linker.close()
        self.reactor.run()

        by_addr = dict((i,[]) for i in range(address_count))
        for m in self.sink.messages:
          by_addr[m["i"]].append(m)
        for addr, msgs in by_addr.iteritems():
          self.assertSequenceEqual(msgs, list(sorted(msgs, key=lambda x:(x["i"],x["j"]))))
        self.assertEqual(len(self.sink.messages), address_count*message_count)

    def testLinker2A1M(self):
        self.testLinker(2, 1)

    def testLinker4A1M(self):
        self.testLinker(4, 1)

    def testLinker16A1M(self):
        self.testLinker(16, 1)

    def testLinker1A2M(self):
        self.testLinker(1, 2)

    def testLinker1A4M(self):
        self.testLinker(1, 4)

    def testLinker1A16M(self):
        self.testLinker(1, 16)

    def testLinker2A2M(self):
        self.testLinker(2, 2)

    def testLinker4A4M(self):
        self.testLinker(4, 4)

    def testLinker16A16M(self):
        self.testLinker(16, 16)
Exemplo n.º 21
0
class HandlerDerivationTest(Test):
    def setUp(self):
        import platform
        if platform.python_implementation() != "Jython":
          # Exception propagation does not work currently for CPython
          raise SkipTest()
        self.reactor = Reactor()

    def wrong_exception(self):
        import sys
        ex = sys.exc_info()
        assert False, " Unexpected exception " + str(ex[1])
    
    def test_reactor_final_derived(self):
        h = BarfOnFinalDerived()
        self.reactor.global_handler = h
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_final_py_child_py(self):
        class APoorExcuseForAHandler:
            def __init__(self):
                self.handlers = [BarfOnFinal()]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_final_py_child_derived(self):
        class APoorExcuseForAHandler:
            def __init__(self):
                self.handlers = [BarfOnFinalDerived()]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_final_derived_child_derived(self):
        class APoorExcuseForAHandler(CHandshaker):
            def __init__(self):
                CHandshaker.__init__(self)
                self.handlers = [BarfOnFinalDerived()]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_final_derived_child_py(self):
        class APoorExcuseForAHandler(CHandshaker):
            def __init__(self):
                CHandshaker.__init__(self)
                self.handlers = [BarfOnFinal()]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass
        except:
            self.wrong_exception()

    def test_reactor_init_derived(self):
        h = BarfOnFinalDerived()
        self.reactor.global_handler = h
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"

    def test_reactor_init_py_child_py(self):
        h = BarfOnFinal()
        class APoorExcuseForAHandler:
            def __init__(self):
                self.handlers = [h]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"

    def test_reactor_init_py_child_derived(self):
        h = BarfOnFinalDerived()
        class APoorExcuseForAHandler:
            def __init__(self):
                self.handlers = [h]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"

    def test_reactor_init_derived_child_derived(self):
        h = BarfOnFinalDerived()
        class APoorExcuseForAHandler(CHandshaker):
            def __init__(self):
                CHandshaker.__init__(self)
                self.handlers = [h]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"

    def test_reactor_init_derived_child_py(self):
        h = BarfOnFinal()
        class APoorExcuseForAHandler(CHandshaker):
            def __init__(self):
                CHandshaker.__init__(self)
                self.handlers = [h]
        self.reactor.global_handler = APoorExcuseForAHandler()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except:
            assert h.init, "excpected the init"
Exemplo n.º 22
0
class SinkTest(TestCase):
    def setUp(self):
        self.sink = Sink()
        self.server = Server(self.sink)
        self.reactor = Reactor(self.server)

    def tearDown(self):
        pass

    def testSender(self, count=1):
        self.server.max_connections = 1
        snd = Sender("//localhost:%s" % PORT)
        self.reactor.handler.add(snd)
        snd.start(self.reactor)
        for i in range(count):
            snd.send("test-%s" % i)
        snd.close()
        self.reactor.run()
        assert len(self.sink.messages) == count
        for i in range(count):
            assert self.sink.messages[i] == "test-%s" % i, self.sink.messages[i]

    def testSender4K(self):
        self.testSender(4 * 1024)

    def testSampler(self, count=1, frequency=10):
        self.server.max_connections = 1
        oself = self

        class Gen(Timeout):
            def __init__(self):
                self.sent = 0

            def on_sample(self, event):
                event.link.send(Message("test-%s" % self.sent))
                self.sent += 1
                if self.sent >= count:
                    event.link.close()
                    self.cancel()

            def on_timer_task(self, event):
                snd.stop(event.reactor)

        gen = Gen()
        snd = Sender("//localhost:%s" % PORT, Sampler(gen, frequency))
        self.reactor.handler.add(snd)
        gen.set_timeout(self.reactor, 2)
        snd.start(self.reactor)
        self.reactor.run()
        assert gen.cancelled, "Sampling timed out"
        assert len(self.sink.messages) == count, len(self.sink.messages)
        for i in range(count):
            assert self.sink.messages[i] == "test-%s" % i

    def testSampler100M100F(self):
        self.testSampler(100, 100)

    def testLinker(self, address_count=1, message_count=1):
        self.server.max_connections = address_count
        linker = Linker()
        linker.start(self.reactor)
        for i in range(address_count):
            for j in range(message_count):
                snd = linker.sender("//localhost:%s/%s" % (PORT, i))
                assert len(linker.senders) == i + 1
                snd.send(dict(i=i, j=j))
        linker.close()
        self.reactor.run()

        by_addr = dict((i, []) for i in range(address_count))
        for m in self.sink.messages:
            by_addr[m["i"]].append(m)
        for addr, msgs in by_addr.iteritems():
            self.assertSequenceEqual(msgs, list(sorted(msgs, key=lambda x: (x["i"], x["j"]))))
        self.assertEqual(len(self.sink.messages), address_count * message_count)

    def testLinker2A1M(self):
        self.testLinker(2, 1)

    def testLinker4A1M(self):
        self.testLinker(4, 1)

    def testLinker16A1M(self):
        self.testLinker(16, 1)

    def testLinker1A2M(self):
        self.testLinker(1, 2)

    def testLinker1A4M(self):
        self.testLinker(1, 4)

    def testLinker1A16M(self):
        self.testLinker(1, 16)

    def testLinker2A2M(self):
        self.testLinker(2, 2)

    def testLinker4A4M(self):
        self.testLinker(4, 4)

    def testLinker16A16M(self):
        self.testLinker(16, 16)
Exemplo n.º 23
0
class ExceptionTest(Test):
    def setup(self):
        self.reactor = Reactor()

    def test_reactor_final(self):
        self.reactor.global_handler = BarfOnFinal()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_global_set(self):
        self.reactor.global_handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_global_add(self):
        self.reactor.global_handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_reactor_set(self):
        self.reactor.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_reactor_add(self):
        self.reactor.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection(self):
        self.reactor.connection(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection_set(self):
        c = self.reactor.connection()
        c.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_connection_add(self):
        c = self.reactor.connection()
        c.handler = object()
        c.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_session_set(self):
        c = self.reactor.connection()
        s = c.session()
        s.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_session_add(self):
        c = self.reactor.connection()
        s = c.session()
        s.handler = object()
        s.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_link_set(self):
        c = self.reactor.connection()
        s = c.session()
        l = s.sender("xxx")
        l.handler = BarfOnInit()
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_link_add(self):
        c = self.reactor.connection()
        s = c.session()
        l = s.sender("xxx")
        l.handler = object()
        l.handler.add(BarfOnInit())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass

    def test_schedule(self):
        self.reactor.schedule(0, BarfOnTask())
        try:
            self.reactor.run()
            assert False, "expected to barf"
        except Barf:
            pass