[Asterisk-code-review] AMI: Support a reconnecting AMIFactory if it exists (testsuite[master])

Matt Jordan asteriskteam at digium.com
Thu Oct 22 22:51:59 CDT 2015


Matt Jordan has uploaded a new change for review.

  https://gerrit.asterisk.org/1516

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(-)


  git pull ssh://gerrit.asterisk.org:29418/testsuite refs/changes/16/1516/1

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: newchange
Gerrit-Change-Id: Ifc9becf8ae263dc594f3b3600f6ea09948dd0749
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