Author: mjordan Date: Wed Dec 24 06:53:10 2014 New Revision: 6134 URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=6134 Log: Add a test for PJSIP keep alive packets for connection oriented transports
This patch adds a test for the PJSIP keep_alive_interval option. It sets the keep_alive_interval to 2 seconds, makes a TCP client connection to the PJSIP stack, and then waits for 5 keep alives. Upon receiving 5 packets, it disconnects and stops the test. In all cases, the test verifies on stopping: * That 5 total packets were received * That the time deltas between packets were received within approximately 2 seconds of each other * That the keep alive values are all '\r\n\r\n' Review: https://reviewboard.asterisk.org/r/4293/ Added: asterisk/trunk/tests/channels/pjsip/keep_alive/ asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ast1/ asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ast1/pjsip.conf (with props) asterisk/trunk/tests/channels/pjsip/keep_alive/keep_alive.py (with props) asterisk/trunk/tests/channels/pjsip/keep_alive/test-config.yaml (with props) Modified: asterisk/trunk/tests/channels/pjsip/tests.yaml Added: asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ast1/pjsip.conf URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ast1/pjsip.conf?view=auto&rev=6134 ============================================================================== --- asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ast1/pjsip.conf (added) +++ asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ast1/pjsip.conf Wed Dec 24 06:53:10 2014 @@ -1,0 +1,8 @@ +[global] +type=global +keep_alive_interval=2 + +[transport-tcp] +type=transport +protocol=tcp +bind=0.0.0.0 Propchange: asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ast1/pjsip.conf ------------------------------------------------------------------------------ svn:eol-style = native Propchange: asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ast1/pjsip.conf ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: asterisk/trunk/tests/channels/pjsip/keep_alive/configs/ast1/pjsip.conf ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: asterisk/trunk/tests/channels/pjsip/keep_alive/keep_alive.py URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/channels/pjsip/keep_alive/keep_alive.py?view=auto&rev=6134 ============================================================================== --- asterisk/trunk/tests/channels/pjsip/keep_alive/keep_alive.py (added) +++ asterisk/trunk/tests/channels/pjsip/keep_alive/keep_alive.py Wed Dec 24 06:53:10 2014 @@ -1,0 +1,122 @@ +"""Keep alive verification + +This module provides a pluggable module for the Asterisk Test Suite that +verifies keep-alive packets (CRLF) received over a TCP connection with the +PJSIP stack in Asterisk. + +Copyright (C) 2014, Digium, Inc. +Matt Jordan <[email protected]> + +This program is free software, distributed under the terms of +the GNU General Public License Version 2. +""" + +import logging +from datetime import datetime + +from twisted.internet import reactor, protocol + +LOGGER = logging.getLogger(__name__) + +received_packets = [] + + +class KeepAliveProtocol(protocol.Protocol): + """Twisted protocol for Asterisk PJSIP keep alives""" + + def __init__(self, test_object): + """Constructor + + Keyword Arguments: + test_object The one and only test object + """ + self.test_object = test_object + + def dataReceived(self, data): + LOGGER.debug('Received packet: {}'.format(data)) + received_packets.append((datetime.utcnow(), data)) + if len(received_packets) == 5: + self.transport.loseConnection() + + +class KeepAliveFactory(protocol.ClientFactory): + """Twisted protocol factory for KeepAliveProtocol""" + + def __init__(self, test_object): + """Constructor + + Keyword Arguments: + test_object The one and only test object + """ + self.test_object = test_object + + def buildProtocol(self, addr): + """Build a KeepAliveProtocol""" + return KeepAliveProtocol(self.test_object) + + def clientConnectionFailed(self, connector, reason): + """twisted callback for a failed client connection""" + LOGGER.warn('Failed to connect to Asterisk on port 5060: {}'.format( + reason)) + self.test_object.set_passed(False) + self.test_object.stop_reactor() + + def clientConnectionLost(self, connector, reason): + """twisted callback for a lost client connection""" + LOGGER.info('Client connection dropped: {}'.format(reason)) + self.test_object.stop_reactor() + + +class KeepAliveReceiver(object): + """A pluggable module for verifying the keep_alive_interval option""" + + def __init__(self, module_config, test_object): + """Constructor + + Keyword Arguments: + module_config The configuration for this pluggable module + test_object The one and only test object + """ + + # Use the AMI callback to know for sure we are fully booted + self.test_object = test_object + test_object.register_ami_observer(self.ami_connect_cb) + test_object.register_stop_observer(self.stop_cb) + + def ami_connect_cb(self, ami): + """Callback called when AMI connects + + Keyword Arguments: + ami The AMI manager object for our Asterisk instance + """ + reactor.connectTCP('localhost', 5060, + KeepAliveFactory(self.test_object)) + + def stop_cb(self, result): + """Deferred callback called when Asterisk is stopped + + Used to verify that we got our packets. + """ + if len(received_packets) != 5: + LOGGER.warn('Failed to get 5 packets: got {} instead'.format( + len(received_packets))) + self.test_object.set_passed(False) + return result + + deltas = [round((j[0] - i[0]).total_seconds()) for i, j in + zip(received_packets[:-1], received_packets[1:])] + if not all([d == 2 for d in deltas]): + LOGGER.warn('Failed to get expected deltas between keep-alives') + LOGGER.warn('Deltas: {}'.format(deltas)) + LOGGER.warn('Received packets: {}'.format(received_packets)) + self.test_object.set_passed(False) + return result + + if not all([p[1] == '\r\n\r\n' for p in received_packets]): + LOGGER.warn('Failed to get expected keep-alive values') + LOGGER.warn('Received packets: {}'.format(received_packets)) + self.test_object.set_passed(False) + return result + + self.test_object.set_passed(True) + return result Propchange: asterisk/trunk/tests/channels/pjsip/keep_alive/keep_alive.py ------------------------------------------------------------------------------ svn:eol-style = native Propchange: asterisk/trunk/tests/channels/pjsip/keep_alive/keep_alive.py ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: asterisk/trunk/tests/channels/pjsip/keep_alive/keep_alive.py ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: asterisk/trunk/tests/channels/pjsip/keep_alive/test-config.yaml URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/channels/pjsip/keep_alive/test-config.yaml?view=auto&rev=6134 ============================================================================== --- asterisk/trunk/tests/channels/pjsip/keep_alive/test-config.yaml (added) +++ asterisk/trunk/tests/channels/pjsip/keep_alive/test-config.yaml Wed Dec 24 06:53:10 2014 @@ -1,0 +1,35 @@ +testinfo: + summary: 'Verify the keep_alive_interval setting for the PJSIP stack' + description: | + This test verifies the keep_alive_interval setting for the PJSIP stack + in Asterisk. It sets the keep_alive_interval to 2 seconds, makes a + TCP client connection to the PJSIP stack, and then waits for 5 keep + alives. Upon receiving 5 packets, it disconnects and stops the test. + In all cases, the test verifies on stopping: + * That 5 total packets were received + * That the time deltas between packets were received within + approximately 2 seconds of each other + * That the keep alive values are all '\r\n\r\n' + +test-modules: + add-test-to-search-path: True + test-object: + config-section: test-object-config + typename: 'test_case.TestCaseModule' + modules: + - + typename: 'keep_alive.KeepAliveReceiver' + config-section: dummy-config + +test-object-config: + asterisk-instances: 1 + connect-ami: True + +dummy-config: + +properties: + minversion: '13.2.0' + dependencies: + - asterisk : 'res_pjsip' + tags: + - pjsip Propchange: asterisk/trunk/tests/channels/pjsip/keep_alive/test-config.yaml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: asterisk/trunk/tests/channels/pjsip/keep_alive/test-config.yaml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: asterisk/trunk/tests/channels/pjsip/keep_alive/test-config.yaml ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: asterisk/trunk/tests/channels/pjsip/tests.yaml URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/channels/pjsip/tests.yaml?view=diff&rev=6134&r1=6133&r2=6134 ============================================================================== --- asterisk/trunk/tests/channels/pjsip/tests.yaml (original) +++ asterisk/trunk/tests/channels/pjsip/tests.yaml Wed Dec 24 06:53:10 2014 @@ -30,3 +30,4 @@ - dir: 'optimistic_srtp' - test: 'in_dialog_invite_replaces' - test: 'dtmf_incompatible' + - test: 'keep_alive' -- _____________________________________________________________________ -- Bandwidth and Colocation Provided by http://www.api-digital.com -- svn-commits mailing list To UNSUBSCRIBE or update options visit: http://lists.digium.com/mailman/listinfo/svn-commits
