[Asterisk-code-review] Fix AMI exten state list test. (testsuite[master])

Mark Michelson asteriskteam at digium.com
Wed Feb 17 14:37:17 CST 2016


Mark Michelson has uploaded a new change for review.

  https://gerrit.asterisk.org/2268

Change subject: Fix AMI exten_state_list test.
......................................................................

Fix AMI exten_state_list test.

This test would fail from time to time because an out-of-date device
state would be sent when we would request the ExtensionStateList from
AMI.

This was happening because the test would send device state changes and
then immediately request the ExtensionStateList. Unfortunately, the
device state changes had not propagated entirely through the system yet,
and so we were retrieving stale data.

The test has been modified to request the ExtensionStateList after
receiving all ExtensionStatus and PresenceStatus events. This way, we
can be sure the system is completely updated and we are getting timely
data.

Change-Id: I762a614f6bc6bf7cc72ce9710786fadd69f13572
---
M tests/manager/exten_state_list/ami_exten_state_list.py
1 file changed, 57 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/testsuite refs/changes/68/2268/1

diff --git a/tests/manager/exten_state_list/ami_exten_state_list.py b/tests/manager/exten_state_list/ami_exten_state_list.py
index 9cdc682..c9449f4 100755
--- a/tests/manager/exten_state_list/ami_exten_state_list.py
+++ b/tests/manager/exten_state_list/ami_exten_state_list.py
@@ -44,11 +44,63 @@
         self.received_events = []
         self.test_object = test_object
         self.state_pos = 0
+        self.exten_state_changes = 0
+        self.presence_state_changes = 0
 
         self.list_complete_token = self.test_object.create_fail_token(
             'ExtensionStateListComplete event received')
 
         self.test_object.register_ami_observer(self.ami_connect_handler)
+
+    def on_extension_status(self, ami, event):
+        '''Event callback for ExtensionStatus
+
+        ExtensionStatus events can happen either because we have requested an
+        ExtensionStateList or because the extension state of an extension has
+        changed. For our purposes, we are only concerned with the latter case.
+        We can tell the cases apart by checking for the presence of an actionid
+        on the event.
+        '''
+        if 'actionid' in event:
+            return
+
+        self.exten_state_changes += 1
+        self.execute_query(ami)
+
+    def on_presence_state_change(self, ami, event):
+        '''Event callback for PresenceStatus
+
+        We do not care about the contents of the event, just the fact that the
+        event occurred.
+        '''
+        self.presence_state_changes += 1
+        self.execute_query(ami)
+
+    def execute_query(self, ami):
+        """Called when all presence state values are set
+
+        Keyword Arguments:
+        ami The AMIProtocol object
+        """
+
+        if (self.exten_state_changes != len(DEVICE_STATES) or
+            self.presence_state_changes != len(PRESENCE_STATES)):
+            return
+
+        deferred = ami.collectDeferred({'Action': 'ExtensionStateList'},
+                                       'ExtensionStateListComplete')
+        deferred.addCallbacks(self.extension_state_list_success,
+                              self.action_failed)
+
+    def action_failed(self, result):
+        """Called if the AMI action failed
+
+        Keyword Arguments:
+        result The result of all of the AMI actions or a single action.
+        """
+        LOGGER.error("An action failed with result: %s" % str(result))
+        self.test_object.set_passed(False)
+        self.test_object.stop_reactor()
 
     def ami_connect_handler(self, ami):
         """Handle AMI connection from the test object
@@ -57,28 +109,8 @@
         ami The AMIProtocol instance that just connected
         """
 
-        def _action_failed(result):
-            """Called if the AMI action failed
-
-            Keyword Arguments:
-            result The result of all of the AMI actions or a single action.
-            """
-            LOGGER.error("An action failed with result: %s" % str(result))
-            self.test_object.set_passed(False)
-            self.test_object.stop_reactor()
-
-        def _execute_query(result, ami):
-            """Called when all presence state values are set
-
-            Keyword Arguments:
-            result The result of all of the deferreds
-            ami The AMIProtocol object
-            """
-
-            deferred = ami.collectDeferred({'Action': 'ExtensionStateList'},
-                                           'ExtensionStateListComplete')
-            deferred.addCallbacks(self.extension_state_list_success,
-                                  _action_failed)
+        ami.registerEvent('ExtensionStatus', self.on_extension_status)
+        ami.registerEvent('PresenceStateChange', self.on_presence_state_change)
 
         # Create a few state values
         resp_list = []
@@ -95,8 +127,7 @@
             resp_list.append(deferred)
 
         defer_list = defer.DeferredList(resp_list)
-        defer_list.addCallback(_execute_query, ami)
-        defer_list.addErrback(_action_failed)
+        defer_list.addErrback(self.action_failed)
 
     def extension_state_list_success(self, result):
         """Handle the completion of the ExtensionStateList action
@@ -130,8 +161,8 @@
         actual = event.get(parameter)
         expected = EXPECTED_STATES[self.state_pos][parameter]
         if actual != expected:
-            LOGGER.error("Unexpected {0} received. Expected {1} but got \
-                         {2}".format(parameter, expected, actual))
+            LOGGER.error("Unexpected {0} received. Expected {1} but got "
+                         "{2}".format(parameter, expected, actual))
             self.test_object.set_passed(False)
 
     def handle_exten_status_event(self, event):

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I762a614f6bc6bf7cc72ce9710786fadd69f13572
Gerrit-PatchSet: 1
Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-Owner: Mark Michelson <mmichelson at digium.com>



More information about the asterisk-code-review mailing list