def proxy_dp(ael): """Provision listener with a 'proxy' dialplan app """ # define a chan park callback def bridge2dest(sess): '''bridge to the dest specified in the req uri ''' if sess['Call-Direction'] == 'inbound': sess.bridge(dest_url="${sip_req_uri}") # dest_url="${sip_req_user}@${sip_req_host}:${sip_req_port}") # print(sess.show()) ev = "CHANNEL_PARK" # add a failover callback to provide the dialplan ael.add_callback(ev, 'default', bridge2dest) # ensure callback was registered assert bridge2dest in ael.consumers['default'][ev] # attempt to add measurement collection try: from switchy.apps.measure import Metrics except ImportError: print("WARNING: numpy measurements not available") else: from switchy import marks # manually insert the metrics app metrics = Metrics(listener=ael) for ev_type, cb_type, obj in marks.get_callbacks(metrics): if cb_type == 'callback': assert ael.add_callback(ev_type, 'default', obj) assert obj in ael.consumers['default']['CHANNEL_HANGUP'] ael.metrics = metrics # sanity assert ael.connected() assert ael.is_alive() return ael
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
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