Exemplo n.º 1
0
def test_file(tmpdir):
    logfile = str(tmpdir.ensure("debug.log"))
    stderr = open(logfile, "w+")

    app = App()
    debugger = Debugger(file=stderr)
    debugger.register(app)
    while app:
        app.flush()
    stderr.seek(0)
    stderr.truncate()

    assert debugger._events

    e = Event()
    app.fire(e)
    app.flush()

    stderr.seek(0)
    s = stderr.read().strip()
    assert s == str(e)
    stderr.seek(0)
    stderr.truncate()

    debugger._events = False
    assert not debugger._events

    e = Event()
    app.fire(e)

    stderr.seek(0)
    s = stderr.read().strip()
    assert s == ""
    stderr.seek(0)
    stderr.truncate()
Exemplo n.º 2
0
    def testIgnoreChannels(self):
        """Test Debugger's IgnoreChannels

        Test Debugger's IgnoreChannels
        """

        manager = Manager()
        debugger = Debugger()
        foo = Foo()

        manager += debugger
        manager += foo

        debugger.IgnoreChannels.extend([("*", "bar")])
        debugger.events = True

        e = Event()
        manager.send(e, "foo")
        sys.stderr.seek(0)
        s = sys.stderr.read().strip()
        self.assertEquals(s, str(e))

        sys.stderr.seek(0)
        sys.stderr.truncate()

        e = Event()
        manager.send(e, "bar")
        sys.stderr.seek(0)
        s = sys.stderr.read().strip()
        self.assertEquals(s, "")

        sys.stderr.seek(0)
        sys.stderr.truncate()
        foo.unregister()
Exemplo n.º 3
0
def test_main():
    app = App()
    stderr = StringIO()
    debugger = Debugger(file=stderr)
    debugger.register(app)
    while app:
        app.flush()
    stderr.seek(0)
    stderr.truncate()

    assert debugger._events

    e = Event()
    app.fire(e)
    app.flush()
    stderr.seek(0)
    s = stderr.read().strip()
    assert s == str(e)
    stderr.seek(0)
    stderr.truncate()

    debugger._events = False
    assert not debugger._events

    e = Event()
    app.fire(e)

    stderr.seek(0)
    s = stderr.read().strip()
    assert s == ""
    stderr.seek(0)
    stderr.truncate()
Exemplo n.º 4
0
    def testDebugger(self):
        """Test Debugger

        Test Debugger
        """

        manager = Manager()
        debugger = Debugger()
        foo = Foo()

        manager += debugger
        manager += foo

        debugger.events = True
        e = Event()
        manager.send(e, "foo")
        sys.stderr.seek(0)
        s = sys.stderr.read().strip()
        self.assertEquals(s, str(e))

        sys.stderr.seek(0)
        sys.stderr.truncate()

        debugger.events = False
        e = Event()
        manager.send(e, "foo")
        sys.stderr.seek(0)
        s = sys.stderr.read().strip()
        self.assertEquals(s, "")

        sys.stderr.seek(0)
        sys.stderr.truncate()
Exemplo n.º 5
0
    def runTest(self):

        class ErrorHandler(Component):

            def __init__(self):
                super(ErrorHandler, self).__init__()

                self.type = None
                self.value = None
                self.traceback = None

            def exception(self, type, value, traceback, handler=None):
                self.type = type
                self.value = value
                self.traceback = traceback
                self.handler = handler

        class TestError(Component):

            def test1(self):
                return x

            def test2(self):
                return self.x

            def test3(self):
                raise RuntimeError()

        m = Manager()
        e = ErrorHandler()
        m += e
        t = TestError()
        m += t

        m.push(Event(), "test1")
        m.flush(); m.flush()
        self.assertTrue(e.type is NameError)
        self.assertTrue(isinstance(e.value, NameError))
        self.assertEquals(e.handler, t.test1)

        m.push(Event(), "test2")
        m.flush(); m.flush()
        self.assertTrue(e.type is AttributeError)
        self.assertTrue(isinstance(e.value, AttributeError))
        self.assertEquals(e.handler, t.test2)

        m.push(Event(), "test3")
        m.flush(); m.flush()
        self.assertTrue(e.type is RuntimeError)
        self.assertTrue(isinstance(e.value, RuntimeError))
        self.assertEquals(e.handler, t.test3)
Exemplo n.º 6
0
def load_event(s):
    data = json.loads(s)

    name = bytes_to_str(data["name"].encode("utf-8"))

    args = []
    for arg in data["args"]:
        if isinstance(arg, text_type):
            arg = arg.encode("utf-8")
        args.append(arg)

    kwargs = {}
    for k, v in data["kwargs"].items():
        if isinstance(v, text_type):
            v = v.encode("utf-8")
        kwargs[str(k)] = v

    e = Event.create(name, *args, **kwargs)

    e.success = bool(data["success"])
    e.failure = bool(data["failure"])
    e.notify = bool(data["notify"])
    e.channels = tuple(data["channels"])

    for k, v in dict(data["meta"]).items():
        setattr(e, k, v)

    return e, data["id"]
Exemplo n.º 7
0
def load_event(s):
    data = json.loads(s)

    name = data["name"] if PY3 else data["name"].encode("utf-8")

    args = []
    for arg in data["args"]:
        if isinstance(arg, text_type):
            arg = arg.encode("utf-8")
        args.append(arg)

    kwargs = {}
    for k, v in data["kwargs"].items():
        if isinstance(v, text_type):
            v = v.encode("utf-8")
        kwargs[str(k)] = v

    e = Event.create(name, *args, **kwargs)

    e.success = bool(data["success"])
    e.failure = bool(data["failure"])
    e.notify = bool(data["notify"])
    e.channels = tuple(data["channels"])

    for k, v in dict(data["meta"]).items():
        setattr(e, k, v)

    return e, data["id"]
Exemplo n.º 8
0
def test_IgnoreChannels():
    app = App()
    stderr = StringIO()
    debugger = Debugger(file=stderr)
    debugger.register(app)
    while app:
        app.flush()
    stderr.seek(0)
    stderr.truncate()

    assert debugger._events
    debugger.IgnoreChannels.extend([("*", "test")])

    e = Event()
    app.fire(e)
    app.flush()

    stderr.seek(0)
    s = stderr.read().strip()
    assert s == str(e)
    stderr.seek(0)
    stderr.truncate()

    e = test()
    app.fire(e)
    app.flush()

    stderr.seek(0)
    s = stderr.read().strip()
    assert s == ""
    stderr.seek(0)
    stderr.truncate()
Exemplo n.º 9
0
    def testBridge(self):
        """Test Bridge

        Test Bridge
        """

        m1 = Manager()
        b1 = Bridge(bind=8000, nodes=[("127.0.0.1", 8001)])
        b1.IgnoreChannels.extend(["dummy"])
        foo = Foo()
        m1 += b1
        m1 += foo
        m1.start()

        m2 = Manager()
        b2 = Bridge(bind=8001, nodes=[("127.0.0.1", 8000)])
        b2.IgnoreChannels.extend(["dummy"])
        bar = Bar()
        m2 += b2
        m2 += bar
        m2.start()

        m1.push(Event(), "bar")
        m1.push(Event(), "dummy")
        wait()

        self.assertFalse(foo.flag)
        self.assertTrue(bar.flag)

        m2.push(Event(), "foo")
        m2.push(Event(), "dummy")
        wait()

        self.assertTrue(foo.flag)
        self.assertTrue(bar.flag)

        m1.stop()
        m2.stop()

        bar.unregister()
        b2.unregister()

        foo.unregister()
        b1.unregister()
Exemplo n.º 10
0
    def runTest(self):
        class Foo(Component):

            flag = False

            @handler("foo", "bar")
            def onFOOBAR(self, event, *args, **kwargs):
                self.flag = True

        manager = Manager()
        foo = Foo()
        manager += foo

        manager.send(Event(), "foo")
        self.assertTrue(foo.flag)
        foo.flag = False

        manager.send(Event(), "bar")
        self.assertTrue(foo.flag)
        foo.flag = False

        foo.unregister()
Exemplo n.º 11
0
def test_Logger_debug():
    app = App()
    logger = Logger()
    debugger = Debugger(logger=logger)
    debugger.register(app)
    while app:
        app.flush()

    e = Event()
    app.fire(e)
    app.flush()

    assert logger.debug_msg == repr(e)
Exemplo n.º 12
0
def test_filename(tmpdir):
    if "__pypy__" in sys.modules:
        pytest.skip("Broken on pypy")

    logfile = str(tmpdir.ensure("debug.log"))
    stderr = open(logfile, "r+")

    app = App()
    debugger = Debugger(file=logfile)
    debugger.register(app)
    while app:
        app.flush()
    stderr.seek(0)
    stderr.truncate()

    assert debugger._events

    e = Event()
    app.fire(e)
    app.flush()

    stderr.seek(0)
    s = stderr.read().strip()
    assert s == str(e)
    stderr.seek(0)
    stderr.truncate()

    debugger._events = False
    assert not debugger._events

    e = Event()
    app.fire(e)

    stderr.seek(0)
    s = stderr.read().strip()
    assert s == ""
    stderr.seek(0)
    stderr.truncate()
Exemplo n.º 13
0
    def runTest(self):
        class Foo(Component):

            channel = "foo"

            flag = False

            @handler(filter=True)
            def onALL(self, event, *args, **kwargs):
                self.flag = True
                return True

            @handler("foo")
            def onFOO(self):
                self.flag = False

        class Bar(Component):

            flag = False

            @handler("bar")
            def onBAR(self):
                self.flag = True

        manager = Manager()
        foo = Foo()
        bar = Bar()
        manager += foo
        manager += bar

        manager.send(Event(), "foo", foo.channel)
        self.assertTrue(foo.flag)
        manager.send(Event(), "bar")
        self.assertFalse(bar.flag) ### (NEW) Behavioural Change

        foo.unregister()
        bar.unregister()
Exemplo n.º 14
0
    def runTest(self):

        class A(Component):

            channel = "A"

            flag = False

            @handler("foo")
            def onFOO(self, event, *args, **kwargs):
                self.flag = True

        class B(Component):

            channel = "B"

            flag = False

            @handler("foo")
            def onFOO(self, event, *args, **kwargs):
                self.flag = True

        class C(Component):

            flag = False

            @handler("foo")
            def onFOO(self, event, *args, **kwargs):
                self.flag = True

        x = Manager()
        a = A()
        b = B()
        c = C()

        x += a
        x += b
        x += c

        x.send(Event(), "*")
        self.assertFalse(a.flag)
        self.assertFalse(b.flag)
        self.assertTrue(c.flag)

        x -= a
        x -= b
        x -= c
Exemplo n.º 15
0
    def runTest(self):
        foo = Foo()
        bar = Bar()
        foo + bar

        self.assertTrue(foo.onFOO in foo._handlers)
        self.assertTrue(bar.onBAR in foo._handlers)
        self.assertTrue(foo.onGOTBAR in foo._handlers)

        foo.send(Event(), "foo")
        self.assertTrue(foo.gotbar)

        foo - bar

        self.assertTrue(foo.onFOO in foo._handlers)
        self.assertTrue(bar.onBAR not in foo._handlers)
        self.assertTrue(foo.onGOTBAR in foo._handlers)
Exemplo n.º 16
0
    def runTest(self):
        class Foo(Component):

            flag = False
            channel = "foo"

            @handler("foo")
            def onFOO(self):
                self.flag = True

        class Bar(Component):

            flag = False
            channel = "bar"

            @handler("foo", target="*")
            def onFOO2(self):
                self.flag = True

        class FooBar(Component):

            foo = False

            @handler("foo", target="*")
            def onFOO(self):
                self.foo = True

        manager = Manager()

        foo = Foo()
        bar = Bar()
        foobar = FooBar()

        manager += foo
        manager += bar
        manager += foobar

        manager.send(Event(), "foo", "foo")

        self.assertTrue(foo.flag)
        self.assertTrue(bar.flag)
        self.assertTrue(foobar.foo)

        foo.unregister()
        bar.unregister()
        foobar.unregister()
Exemplo n.º 17
0
    def testLogger(self):
        """Test Debugger Logger

        Test Debugger using dummy Logger-like
        object (normally logging.Logger)
        """

        manager = Manager()
        logger = Logger()
        foo = Foo()

        debugger = Debugger(logger=logger)
        manager += debugger
        manager += foo

        e = Event()
        manager.push(e, "foo")
        manager.flush()
        manager.flush()
        self.assertEquals(logger.msg, repr(e))

        foo.unregister()
Exemplo n.º 18
0
import json

from circuits.core import Event
from circuits.six import bytes_to_str, text_type

META_EXCLUDE = set(dir(Event()))
META_EXCLUDE.add("node_call_id")
META_EXCLUDE.add("node_sock")
META_EXCLUDE.add("node_without_result")
META_EXCLUDE.add("success_channels")


def load_event(s):
    data = json.loads(s)

    name = bytes_to_str(data["name"].encode("utf-8"))

    args = []
    for arg in data["args"]:
        if isinstance(arg, text_type):
            arg = arg.encode("utf-8")
        args.append(arg)

    kwargs = {}
    for k, v in data["kwargs"].items():
        if isinstance(v, text_type):
            v = v.encode("utf-8")
        kwargs[str(k)] = v

    e = Event.create(name, *args, **kwargs)
Exemplo n.º 19
0
    def runTest(self):
        #from circuits import Debugger
        x = RunnableComponent()# + Debugger()
        self.assertFalse(x.running)
        self.assertEquals(x.state, "S")

        # Test Running and Started event
        x.start()
        self.assertTrue(x.running)
        self.assertEquals(x.state, "R")
        self.assertEquals(x._mode, "T")

        # Test component registration and Registered event
        a = A()
        self.assertEquals(x, x + a)
        wait()
        self.assertEquals(x._registered, (a, x))

        # Test tick functions
        self.assertTrue(x._count > 0)

        # Test component unregistration and Unregistered event
        self.assertEquals(x, x - a)
        wait()
        self.assertEquals(x._unregistered, (a, x))

        # Test stopping and Stopped event
        x.stop()
        self.assertFalse(x.running)
        self.assertEquals(x.state, "S")
        self.assertEquals(x._mode, None)

        # Test Sleeping
        x.start(sleep=1)
        self.assertTrue(x.running)
        self.assertEquals(x.state, "R")
        self.assertEquals(x._mode, "T")

        # Test tick functions whiel sleeping
        sleep(2)
        self.assertTrue(x._count > 0)
        self.assertTrue(x._count < 3)

        # Test raising exceptions while sleeping
        x.send(Event(TypeError), "blowup")

        x.stop()
        self.assertFalse(x.running)
        self.assertEquals(x.state, "S")
        self.assertEquals(x._mode, None)

        x.start()
        self.assertTrue(x.running)
        self.assertEquals(x.state, "R")
        self.assertEquals(x._mode, "T")

        # Test event pushing and sending
        x.push(Event())
        wait()
        self.assertTrue(x._event)
        x.send(Event())
        self.assertFalse(x._event)

        # Test event pushing and sending (2)
        x.push(Test())
        wait()
        self.assertTrue(x._test)
        x.send(Test())
        self.assertFalse(x._test)

        # Test event pushing and sending (3)
        x.push(Test(), "test")
        wait()
        self.assertTrue(x._test)
        x.send(Test(), "test")
        self.assertFalse(x._test)

        # Test event pushing and sending (4)
        x.push(Test(), "dummy")
        wait()
        self.assertFalse(x._test)
        x.send(Test(), "dummy")
        self.assertFalse(x._test)

        # Test event pushing and sending (5)
        x.push(Test(1, 2, 3, a=1, b=2, c=3))
        wait()
        self.assertTrue(x._test)
        self.assertEquals(x._args, (1, 2, 3))
        self.assertEquals(x._kwargs, {"a": 1, "b": 2, "c": 3})
        x.send(Test(4, 5, 6, x=3, y=2, z=1))
        self.assertFalse(x._test)
        self.assertEquals(x._args, (4, 5, 6))
        self.assertEquals(x._kwargs, {"x": 3, "y": 2, "z": 1})

        x.stop()
        self.assertFalse(x.running)
        self.assertEquals(x.state, "S")
        self.assertEquals(x._mode, None)

        x.start()
        self.assertTrue(x.running)
        self.assertEquals(x.state, "R")
        self.assertEquals(x._mode, "T")

        # Test error logging
        x.push(Event(), "bad")
        wait()
        self.assertEquals(x._etype, NameError)
        self.assertTrue(isinstance(x._evalue, NameError))
        self.assertEquals(x._handler, x.bad)
        x._etype = None
        x._evalue = None
        x._traceback = None
        x._handler = None

        # Test error raising
        x._count = object()
        wait()
        self.assertEquals(x._etype, TypeError)
        self.assertTrue(isinstance(x._evalue, TypeError))
        self.assertEquals(x._handler, None)

        x.stop()
        self.assertFalse(x.running)
        self.assertEquals(x.state, "S")
        self.assertEquals(x._mode, None)