[Asterisk-code-review] lib/python/asterisk: Add a pluggable module that checks for ... (testsuite[master])

Matt Jordan asteriskteam at digium.com
Wed Sep 9 19:21:30 CDT 2015


Matt Jordan has uploaded a new change for review.

  https://gerrit.asterisk.org/1189

Change subject: lib/python/asterisk: Add a pluggable module that checks for hangups via ARI
......................................................................

lib/python/asterisk: Add a pluggable module that checks for hangups via ARI

Some ARI tests may be slightly non-deterministic as to when they end. In those
cases, it is often nice to watch channels and - if all channels have hung up -
end the test. This patch adds such a pluggable module, which mimics a similar
pluggable module that does the same using AMI.

Note that since there is no ordering between AMI and ARI events, we need to
have one for ARI for those tests that are explicitly looking to validate ARI.

Change-Id: I7ca5e5f8d5fb50503bac78d2a0fd47a981f1d142
---
M lib/python/asterisk/ari.py
M lib/python/asterisk/pluggable_modules.py
2 files changed, 49 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/testsuite refs/changes/89/1189/1

diff --git a/lib/python/asterisk/ari.py b/lib/python/asterisk/ari.py
index a04696b..1d97868 100644
--- a/lib/python/asterisk/ari.py
+++ b/lib/python/asterisk/ari.py
@@ -72,6 +72,7 @@
 
         self._ws_connection = None
         self._ws_event_handlers = []
+        self._ws_open_handlers = []
         self.timed_out = False
 
         self.asterisk_instances = test_config.get('asterisk-instances', 1)
@@ -119,8 +120,17 @@
         :param protocol The WS Client protocol object
         """
         self._ws_connection = protocol
+        for observer in self._ws_open_handlers:
+            observer(self)
         reactor.callLater(0, self._create_ami_connection)
 
+    def register_ari_observer(self, observer):
+        """Register an observer for ARI WS connection
+
+        :param observer The WS observer. This will be called with this object.
+        """
+        self._ws_open_handlers.append(observer)
+
     def on_ws_closed(self, protocol):
         """Handler for WebSocket Client Protocol closed
 
diff --git a/lib/python/asterisk/pluggable_modules.py b/lib/python/asterisk/pluggable_modules.py
index 66e5ec8..d1265c2 100755
--- a/lib/python/asterisk/pluggable_modules.py
+++ b/lib/python/asterisk/pluggable_modules.py
@@ -238,6 +238,45 @@
             self.channels.remove(obj)
 
 
+class ARIHangupMonitor(object):
+    """A class that monitors for new channels and hungup channels in ARI.
+
+    This is the same as HangupMonitor, except that it listens over ARI
+    to avoid any issue with race conditions. Note that it will implicitly
+    create a global subscription to channels, which may conflict with
+    tests that don't expect to get all those events.
+    """
+
+    def __init__(self, instance_config, test_object):
+        """Constructor"""
+        super(ARIHangupMonitor, self).__init__()
+        self.test_object = test_object
+        self.test_object.register_ari_observer(self._handle_ws_open)
+        self.test_object.register_ws_event_handler(self._handle_ws_event)
+        self.channels = 0
+
+    def _handle_ws_open(self, ari_receiver):
+        """Handle WS connection"""
+
+        LOGGER.info(ari_receiver.apps)
+        for app in ari_receiver.apps.split(','):
+            self.test_object.ari.post('applications/{0}/subscription?eventSource=channel:'.format(app))
+
+    def _handle_ws_event(self, message):
+        """Handle a message received over the WS"""
+
+        message_type = message.get('type')
+        if (message_type == 'ChannelCreated'):
+            LOGGER.info('Tracking channel %s', message.get('channel'))
+            self.channels += 1
+        elif (message_type == 'ChannelDestroyed'):
+            LOGGER.info('Destroyed channel %s', message.get('channel'))
+            self.channels -= 1
+            if (self.channels == 0):
+                LOGGER.info("All channels have hungup; stopping test")
+                self.test_object.stop_reactor()
+
+
 class HangupMonitor(object):
     """A class that monitors for new channels and hungup channels. When all
     channels it has monitored for have hung up, it ends the test.

-- 
To view, visit https://gerrit.asterisk.org/1189
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7ca5e5f8d5fb50503bac78d2a0fd47a981f1d142
Gerrit-PatchSet: 1
Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-Owner: Matt Jordan <mjordan at digium.com>



More information about the asterisk-code-review mailing list