[asterisk-commits] AMI: Support a reconnecting AMIFactory if it exists (testsuite[master])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Oct 23 07:56:59 CDT 2015


Anonymous Coward #1000019 has submitted this change and it was merged.

Change subject: AMI: Support a reconnecting AMIFactory if it exists
......................................................................


AMI: Support a reconnecting AMIFactory if it exists

A PR has been up against starpy that would make the AMIFactory reconnect
on a loss of connection. This is useful if we ever want to test restarts of
Asterisk. However, for this to occur, the consumers of the AMIFactory must be
able to be aware that a reconnect occurred so that they can drop their
reference to the AMIProtocol and obtain the new one.

This patch makes it so that ami/test_case will attempt to use the reconnecting
version of the factory by providing an on_reconnect handler when creating the
AMIFactory. If it does not exist or throws an exception, they fall back to the
older version of AMIFactory.

Change-Id: Ifc9becf8ae263dc594f3b3600f6ea09948dd0749
---
M lib/python/asterisk/ami.py
M lib/python/asterisk/test_case.py
2 files changed, 20 insertions(+), 2 deletions(-)

Approvals:
  Anonymous Coward #1000019: Verified
  Matt Jordan: Looks good to me, approved
  Joshua Colp: Looks good to me, but someone else must approve



diff --git a/lib/python/asterisk/ami.py b/lib/python/asterisk/ami.py
index f0583c8..abed882 100644
--- a/lib/python/asterisk/ami.py
+++ b/lib/python/asterisk/ami.py
@@ -614,7 +614,12 @@
         self._attempts = 0
         self._start = None
         self.ami = None
-        self.ami_factory = manager.AMIFactory(user, secret)
+        try:
+            self.ami_factory = manager.AMIFactory(user, secret,
+                                                  on_reconnect=self.on_reconnect)
+        except:
+            # Try without reconnects
+            self.ami_factory = manager.AMIFactory(user, secret)
 
     def login(self):
         """Start the login process"""
@@ -626,6 +631,11 @@
         deferred = self.ami_factory.login(self.host, self.port)
         deferred.addCallbacks(self.on_login_success, self.on_login_error)
 
+    def on_reconnect(self, login_deferred):
+        """Called when an AMI instance reconnects"""
+        LOGGER.debug('AMI client reconnecting...')
+        login_deferred.addCallbacks(self.on_login_success, self.on_login_error)
+
     def on_login_success(self, ami):
         """Deferred callback when login succeeds
 
diff --git a/lib/python/asterisk/test_case.py b/lib/python/asterisk/test_case.py
index 9541fa0..40f253e 100644
--- a/lib/python/asterisk/test_case.py
+++ b/lib/python/asterisk/test_case.py
@@ -259,11 +259,19 @@
         port     The port to connect over
         """
 
+        def on_reconnect(login_deferred):
+            """Called if the connection is lost and re-made"""
+            login_deferred.addCallbacks(self._ami_connect, self.ami_login_error)
+
         for i in range(count):
             host = "127.0.0.%d" % (i + 1)
             self.ami.append(None)
             LOGGER.info("Creating AMIFactory %d" % (i + 1))
-            ami_factory = manager.AMIFactory(username, secret, i)
+            try:
+                ami_factory = manager.AMIFactory(username, secret, i,
+                                                 on_reconnect=on_reconnect)
+            except:
+                ami_factory = manager.AMIFactory(username, secret, i)
             deferred = ami_factory.login(ip=host, port=port)
             deferred.addCallbacks(self._ami_connect, self.ami_login_error)
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ifc9becf8ae263dc594f3b3600f6ea09948dd0749
Gerrit-PatchSet: 1
Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-Owner: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Matt Jordan <mjordan at digium.com>



More information about the asterisk-commits mailing list