Пример #1
0
def test_setup(pool):
    from switchy.apps.bert import Bert
    from switchy import utils
    pool.evals('listener.unsubscribe("CALL_UPDATE")')
    assert not any(pool.evals('listener.connected()'))
    pool.evals('listener.connect()')
    assert all(pool.evals('listener.connected()'))
    # listener connects the client's con implicitly
    assert all(pool.evals('client.connected()'))
    pool.evals('client.load_app(Bert)', Bert=Bert)
    name = utils.get_name(Bert)
    assert all(False for apps in pool.evals('client._apps')
               if name not in apps)
    pool.evals('listener.start()')
    assert all(pool.evals('listener.is_alive()'))
    pool.evals('listener.disconnect()')
    assert not all(pool.evals('listener.is_alive()'))
Пример #2
0
def test_setup(pool):
    from switchy.apps.bert import Bert
    from switchy import utils

    pool.evals('listener.unsubscribe("CALL_UPDATE")')
    assert not any(pool.evals("listener.connected()"))
    pool.evals("listener.connect()")
    assert all(pool.evals("listener.connected()"))
    # listener connects the client's con implicitly
    assert all(pool.evals("client.connected()"))
    pool.evals("client.load_app(Bert)", Bert=Bert)
    name = utils.get_name(Bert)
    assert all(False for apps in pool.evals("client._apps") if name not in apps)
    pool.evals("listener.start()")
    assert all(pool.evals("listener.is_alive()"))
    pool.evals("listener.disconnect()")
    assert not all(pool.evals("listener.is_alive()"))
Пример #3
0
    def add(self, app, name=None, operators={}, **kwargs):
        name = name or utils.get_name(app)
        prepost = getattr(app, 'prepost', None)
        if not prepost:
            raise AttributeError(
                "'{}' must define a `prepost` method".format(name))
        args, ppkwargs = utils.get_args(app.prepost)
        if 'storer' not in ppkwargs:
            raise TypeError("'{}' must define a 'storer' kwarg".format(
                app.prepost))
        ppkwargs = {key: kwargs.pop(key) for key in ppkwargs if key in kwargs}

        # acquire storer factory
        factory = getattr(app, 'new_storer', None)
        storer_kwargs = getattr(app, 'storer_kwargs', {})
        storer_kwargs.update(kwargs)
        storer_kwargs.setdefault('storetype', self.storetype)
        # app may not define a storer factory method
        storer = storage.DataStorer(
            name, dtype=app.fields, **
            storer_kwargs) if not factory else factory()

        self._apps[name] = Measurer(app, ppkwargs, storer, {})
        # provide attr access off `self.stores`
        self._stores[name] = storer
        setattr(
            self.stores.__class__,
            name,
            # make instance lookups access the `data` attr
            property(partial(storer.__class__.data.__get__, storer)))
        # add any app defined operator functions
        ops = getattr(app, 'operators', {})
        ops.update(operators)
        for opname, func in ops.items():
            self.add_operator(name, func, opname=opname)

        return name
Пример #4
0
    def test_apps(self, client, el):
        """Test app loading, unloading
        """
        from switchy.apps.players import TonePlay
        from switchy.apps.bert import Bert
        from switchy.marks import get_callbacks, event_callback
        from switchy import utils
        with pytest.raises(AttributeError):
            client.load_app(TonePlay)
        client.listener = el  # assign listener manually
        assert client.listener is el

        # loading
        client.load_app(TonePlay)
        name = utils.get_name(TonePlay)
        assert name in client._apps
        # app should be an instance of the app type
        app = client._apps[name]
        assert isinstance(app, TonePlay)
        # reloading the same app shouldn't overwrite the original
        client.load_app(TonePlay)
        assert app is client._apps[name]

        # check that callbacks are registered with listener
        cbmap = client.listener.consumers[app.cid]
        for evname, cbtype, obj in get_callbacks(app):
            assert evname in cbmap
            reg_cb = cbmap[evname][0]
            # WTF?? 'is' doesn't work on methods?
            assert obj == reg_cb
            # check methods are bound to the same instance
            assert obj.im_self is reg_cb.im_self

        # add a 2nd app
        # (Bert adds event handlers so a `connect` is necessary)
        client.listener.connect()
        bid = client.load_app(Bert)

        # verify unload
        client.unload_app(TonePlay)
        assert TonePlay not in client._apps
        with pytest.raises(KeyError):
            client.listener.consumers[app.cid]

        # 2nd should still be there
        assert utils.get_name(Bert) in client._apps
        cbs = client.listener.consumers[client.apps.Bert.cid]
        assert cbs
        cbcount = len(cbs)

        # app reject due to mal-typed cb
        class DumbApp(object):
            @event_callback('CHANNEL_ANSWER')
            def h0(self, sess):
                pass

            @event_callback('CHANNEL_HANGUP')
            def h1(self, sess):
                pass

            class noncb(object):
                pass

            # non-function marked obj
            fack = event_callback('CHANNEL_PARK')(noncb)

        with pytest.raises(TypeError):
            client.load_app(DumbApp, ident=bid)
        name = utils.get_name(DumbApp)
        assert name not in client._apps
        # Bert cbs should still be active
        assert len(client.listener.consumers[bid]) == cbcount
Пример #5
0
    def test_apps(self, client, el):
        """Test app loading, unloading
        """
        from switchy.apps.players import TonePlay
        from switchy.apps.bert import Bert
        from switchy.marks import get_callbacks, event_callback
        from switchy import utils
        with pytest.raises(AttributeError):
            client.load_app(TonePlay)
        client.listener = el  # assign listener manually
        assert client.listener is el

        # loading
        client.load_app(TonePlay)
        name = utils.get_name(TonePlay)
        assert name in client._apps
        # app should be an instance of the app type
        app = client._apps[name]
        assert isinstance(app, TonePlay)
        # reloading the same app shouldn't overwrite the original
        client.load_app(TonePlay)
        assert app is client._apps[name]

        # check that callbacks are registered with listener
        cbmap = client.listener.consumers[app.cid]
        for evname, cbtype, obj in get_callbacks(app):
            assert evname in cbmap
            reg_cb = cbmap[evname][0]
            # WTF?? 'is' doesn't work on methods?
            assert obj == reg_cb
            # check methods are bound to the same instance
            assert obj.im_self is reg_cb.im_self

        # add a 2nd app
        # (Bert adds event handlers so a `connect` is necessary)
        client.listener.connect()
        bid = client.load_app(Bert)

        # verify unload
        client.unload_app(TonePlay)
        assert TonePlay not in client._apps
        with pytest.raises(KeyError):
            client.listener.consumers[app.cid]

        # 2nd should still be there
        assert utils.get_name(Bert) in client._apps
        cbs = client.listener.consumers[client.apps.Bert.cid]
        assert cbs
        cbcount = len(cbs)

        # app reject due to mal-typed cb
        class DumbApp(object):
            @event_callback('CHANNEL_ANSWER')
            def h0(self, sess):
                pass

            @event_callback('CHANNEL_HANGUP')
            def h1(self, sess):
                pass

            class noncb(object):
                pass

            # non-function marked obj
            fack = event_callback('CHANNEL_PARK')(noncb)

        with pytest.raises(TypeError):
            client.load_app(DumbApp, ident=bid)
        name = utils.get_name(DumbApp)
        assert name not in client._apps
        # Bert cbs should still be active
        assert len(client.listener.consumers[bid]) == cbcount
Пример #6
0
    def test_apps(self, client, el):
        """Test app loading, unloading
        """
        from switchy.apps.players import TonePlay
        from switchy.apps.bert import Bert
        from switchy.marks import get_callbacks, event_callback
        from switchy import utils
        with pytest.raises(AttributeError):
            # need an observer assigned first
            client.load_app(TonePlay)
        client.listener = el  # assign observer manually
        assert client.listener is el

        # loading
        client.load_app(TonePlay)
        name = utils.get_name(TonePlay)
        # group id is by default the name of the first app
        assert name in client._apps
        # app should be an instance of the app type
        app = client._apps[name][name]
        assert isinstance(app, TonePlay)

        # reloading the same app is not allowed without specifying
        # a new `on_value` / group id
        with pytest.raises(utils.ConfigurationError):
            client.load_app(TonePlay)
        # load with an alt id
        client.load_app(TonePlay, 'TonePlay2')
        # and therefore shouldn't overwrite the original
        assert app is client._apps[name][name]
        assert app is not client._apps['TonePlay2'][name]

        # check that callbacks are registered with listener
        cbmap = client.listener.consumers[app.cid]
        for evname, cbtype, obj in get_callbacks(app):
            assert evname in cbmap
            reg_cb = cbmap[evname][0]
            # WTF?? 'is' doesn't work on methods?
            assert obj == reg_cb
            # check methods are bound to the same instance
            assert obj.__self__ is reg_cb.__self__

        # add a 2nd app
        # (Bert adds event handlers so a `connect` is necessary)
        client.connect()
        client.listener.connect()
        bid = client.load_app(Bert)

        # verify unload
        client.unload_app(app.cid)
        assert app.cid not in client._apps
        with pytest.raises(KeyError):
            client.listener.consumers[app.cid]

        # Bert should still be there
        assert bid in client._apps
        cbs = client.listener.consumers[client.apps.Bert['Bert'].cid]
        assert cbs
        cbcount = len(cbs)

        # app reject due to mal-typed cb
        class DumbApp(object):
            @event_callback('CHANNEL_ANSWER')
            def h0(self, sess):
                pass

            @event_callback('CHANNEL_HANGUP')
            def h1(self, sess):
                pass

            # non-function marked obj
            @event_callback('CHANNEL_PARK')
            class noncb(object):
                pass

        with pytest.raises(TypeError):
            client.load_app(DumbApp, ident=bid)
        name = utils.get_name(DumbApp)
        assert name not in client._apps
        assert name not in client.apps.Bert
        # Bert cbs should still be active
        assert len(client.listener.consumers[bid]) == cbcount