import re
import time
import unittest

try:
    from thread import start_new_thread
except ImportError:
    from _thread import start_new_thread

from asterisk.ami import Response, FutureResponse

LINE_REGEX = re.compile(r'\r?\n')


class AMIResponseTest(unittest.TestCase):
    def test_login_response(self):
        login_response = '\r\n'.join([
            'Response: Success',
            'Message: Authentication accepted',
        ]) + '\r\n'
        self.assertTrue(Response.match(login_response))
        response = Response.read(login_response)
        self.assertFalse(response.is_error())
        self.assertFalse(response.follows)
        self.assertEqual(login_response, str(response))

    def test_login_response_fail(self):
        login_response = '\r\n'.join([
            'Response: Error',
            'Message: Authentication failed',
        ]) + '\r\n'
        self.assertTrue(Response.match(login_response))
        response = Response.read(login_response)
        self.assertTrue(response.is_error())
        self.assertFalse(response.follows)
        self.assertEqual(login_response, str(response))

    def test_goodbye_response(self):
        goodbye_response = '\r\n'.join([
            'Response: Goodbye',
            'Message: Thanks for all the fish.'
        ]) + '\r\n'
        self.assertTrue(Response.match(goodbye_response))
        response = Response.read(goodbye_response)
        self.assertFalse(response.is_error())
        self.assertFalse(response.follows)
        self.assertEqual(goodbye_response, str(response))

    def test_with_follows(self):
        follows_response = '\n'.join([
            'Response: Follows\r',
            'Channel (Context Extension Pri ) State Appl. Data',
            '0 active channel(s)',
            '--END COMMAND--',
        ]) + '\r\n'
        self.assertTrue(Response.match(follows_response))
        response = Response.read(follows_response)
        self.assertFalse(response.is_error())
        self.assertIsNotNone(response.follows)
        self.assertListEqual(LINE_REGEX.split(follows_response)[1:-1], response.follows)
        self.assertEqual(follows_response, str(response))

    def test_with_key_value_follows(self):
        """
        Action:  Command
        Command: database show AMPUSER
        """
        follows_response = '\n'.join([
            'Response: Follows\r',
            'Privilege: Command\r',
            '/AMPUSER/*/concurrency_limit                      : 4                        ',
            '/AMPUSER/2000/answermode                          : disabled                 ',
            '/AMPUSER/2000/cfringtimer                         : 0                        ',
            '/AMPUSER/2000/cidname                             : Wainer                   ',
            '/AMPUSER/2000/cidnum                              : 2000                     ',
            '/AMPUSER/2000/concurrency_limit                   : 0                        ',
            '/AMPUSER/2000/device                              : 2000                     ',
            '/AMPUSER/2000/language                            :                          ',
            '/AMPUSER/2000/noanswer                            :                          ',
            '/AMPUSER/2000/outboundcid                         :                          ',
            '/AMPUSER/2000/password                            :                          ',
            '/AMPUSER/2000/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2000/recording                           :                          ',
            '/AMPUSER/2000/recording/in/external               : dontcare                 ',
            '/AMPUSER/2000/recording/in/internal               : dontcare                 ',
            '/AMPUSER/2000/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2000/recording/out/external              : dontcare                 ',
            '/AMPUSER/2000/recording/out/internal              : dontcare                 ',
            '/AMPUSER/2000/recording/priority                  : 0                        ',
            '/AMPUSER/2000/ringtimer                           : 0                        ',
            '/AMPUSER/2000/voicemail                           : novm                     ',
            '/AMPUSER/2001/answermode                          : disabled                 ',
            '/AMPUSER/2001/cfringtimer                         : 0                        ',
            '/AMPUSER/2001/cidname                             : Wainer - Local           ',
            '/AMPUSER/2001/cidnum                              : 2001                     ',
            '/AMPUSER/2001/concurrency_limit                   : 0                        ',
            '/AMPUSER/2001/device                              : 2001                     ',
            '/AMPUSER/2001/language                            :                          ',
            '/AMPUSER/2001/noanswer                            :                          ',
            '/AMPUSER/2001/outboundcid                         :                          ',
            '/AMPUSER/2001/password                            :                          ',
            '/AMPUSER/2001/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2001/recording                           :                          ',
            '/AMPUSER/2001/recording/in/external               : always                   ',
            '/AMPUSER/2001/recording/in/internal               : always                   ',
            '/AMPUSER/2001/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2001/recording/out/external              : always                   ',
            '/AMPUSER/2001/recording/out/internal              : always                   ',
            '/AMPUSER/2001/recording/priority                  : 0                        ',
            '/AMPUSER/2001/ringtimer                           : 0                        ',
            '/AMPUSER/2001/voicemail                           : novm                     ',
            '/AMPUSER/2003/answermode                          : disabled                 ',
            '/AMPUSER/2003/cfringtimer                         : 0                        ',
            '/AMPUSER/2003/cidname                             : Teo                      ',
            '/AMPUSER/2003/cidnum                              : 2003                     ',
            '/AMPUSER/2003/concurrency_limit                   : 0                        ',
            '/AMPUSER/2003/device                              : 2003                     ',
            '/AMPUSER/2003/language                            :                          ',
            '/AMPUSER/2003/noanswer                            :                          ',
            '/AMPUSER/2003/outboundcid                         :                          ',
            '/AMPUSER/2003/password                            :                          ',
            '/AMPUSER/2003/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2003/recording                           :                          ',
            '/AMPUSER/2003/recording/in/external               : always                   ',
            '/AMPUSER/2003/recording/in/internal               : always                   ',
            '/AMPUSER/2003/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2003/recording/out/external              : always                   ',
            '/AMPUSER/2003/recording/out/internal              : always                   ',
            '/AMPUSER/2003/recording/priority                  : 0                        ',
            '/AMPUSER/2003/ringtimer                           : 0                        ',
            '/AMPUSER/2003/voicemail                           : novm                     ',
            '/AMPUSER/2004/answermode                          : disabled                 ',
            '/AMPUSER/2004/cfringtimer                         : 0                        ',
            '/AMPUSER/2004/cidname                             : Gustavo - Suporte        ',
            '/AMPUSER/2004/cidnum                              : 2004                     ',
            '/AMPUSER/2004/concurrency_limit                   : 0                        ',
            '/AMPUSER/2004/device                              : 2004                     ',
            '/AMPUSER/2004/language                            :                          ',
            '/AMPUSER/2004/noanswer                            :                          ',
            '/AMPUSER/2004/outboundcid                         :                          ',
            '/AMPUSER/2004/password                            :                          ',
            '/AMPUSER/2004/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2004/recording                           :                          ',
            '/AMPUSER/2004/recording/in/external               : always                   ',
            '/AMPUSER/2004/recording/in/internal               : always                   ',
            '/AMPUSER/2004/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2004/recording/out/external              : always                   ',
            '/AMPUSER/2004/recording/out/internal              : always                   ',
            '/AMPUSER/2004/recording/priority                  : 0                        ',
            '/AMPUSER/2004/ringtimer                           : 0                        ',
            '/AMPUSER/2004/voicemail                           : novm                     ',
            '/AMPUSER/2005/answermode                          : disabled                 ',
            '/AMPUSER/2005/cfringtimer                         : 0                        ',
            '/AMPUSER/2005/cidname                             : NoteTeo                  ',
            '/AMPUSER/2005/cidnum                              : 2005                     ',
            '/AMPUSER/2005/concurrency_limit                   : 0                        ',
            '/AMPUSER/2005/device                              : 2005                     ',
            '/AMPUSER/2005/language                            :                          ',
            '/AMPUSER/2005/noanswer                            :                          ',
            '/AMPUSER/2005/outboundcid                         :                          ',
            '/AMPUSER/2005/password                            :                          ',
            '/AMPUSER/2005/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2005/recording                           :                          ',
            '/AMPUSER/2005/recording/in/external               : always                   ',
            '/AMPUSER/2005/recording/in/internal               : always                   ',
            '/AMPUSER/2005/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2005/recording/out/external              : always                   ',
            '/AMPUSER/2005/recording/out/internal              : always                   ',
            '/AMPUSER/2005/recording/priority                  : 0                        ',
            '/AMPUSER/2005/ringtimer                           : 0                        ',
            '/AMPUSER/2005/voicemail                           : novm                     ',
            '/AMPUSER/2007/answermode                          : disabled                 ',
            '/AMPUSER/2007/cfringtimer                         : 0                        ',
            '/AMPUSER/2007/cidname                             : Carol - Financeiro       ',
            '/AMPUSER/2007/cidnum                              : 2007                     ',
            '/AMPUSER/2007/concurrency_limit                   : 0                        ',
            '/AMPUSER/2007/device                              : 2007                     ',
            '/AMPUSER/2007/language                            :                          ',
            '/AMPUSER/2007/noanswer                            :                          ',
            '/AMPUSER/2007/outboundcid                         :                          ',
            '/AMPUSER/2007/password                            :                          ',
            '/AMPUSER/2007/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2007/recording                           :                          ',
            '/AMPUSER/2007/recording/in/external               : always                   ',
            '/AMPUSER/2007/recording/in/internal               : always                   ',
            '/AMPUSER/2007/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2007/recording/out/external              : always                   ',
            '/AMPUSER/2007/recording/out/internal              : always                   ',
            '/AMPUSER/2007/recording/priority                  : 0                        ',
            '/AMPUSER/2007/ringtimer                           : 0                        ',
            '/AMPUSER/2007/voicemail                           : novm                     ',
            '/AMPUSER/2008/answermode                          : disabled                 ',
            '/AMPUSER/2008/cfringtimer                         : 0                        ',
            '/AMPUSER/2008/cidname                             : Suporte NoteHP           ',
            '/AMPUSER/2008/cidnum                              : 2008                     ',
            '/AMPUSER/2008/concurrency_limit                   : 0                        ',
            '/AMPUSER/2008/device                              : 2008                     ',
            '/AMPUSER/2008/language                            :                          ',
            '/AMPUSER/2008/noanswer                            :                          ',
            '/AMPUSER/2008/outboundcid                         :                          ',
            '/AMPUSER/2008/password                            :                          ',
            '/AMPUSER/2008/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2008/recording                           :                          ',
            '/AMPUSER/2008/recording/in/external               : always                   ',
            '/AMPUSER/2008/recording/in/internal               : always                   ',
            '/AMPUSER/2008/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2008/recording/out/external              : always                   ',
            '/AMPUSER/2008/recording/out/internal              : always                   ',
            '/AMPUSER/2008/recording/priority                  : 0                        ',
            '/AMPUSER/2008/ringtimer                           : 0                        ',
            '/AMPUSER/2008/voicemail                           : novm                     ',
            '/AMPUSER/2009/answermode                          : disabled                 ',
            '/AMPUSER/2009/cfringtimer                         : 0                        ',
            '/AMPUSER/2009/cidname                             : Fernando                 ',
            '/AMPUSER/2009/cidnum                              : 2009                     ',
            '/AMPUSER/2009/concurrency_limit                   : 0                        ',
            '/AMPUSER/2009/device                              : 2009                     ',
            '/AMPUSER/2009/language                            :                          ',
            '/AMPUSER/2009/noanswer                            :                          ',
            '/AMPUSER/2009/outboundcid                         :                          ',
            '/AMPUSER/2009/password                            :                          ',
            '/AMPUSER/2009/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2009/recording                           :                          ',
            '/AMPUSER/2009/recording/in/external               : always                   ',
            '/AMPUSER/2009/recording/in/internal               : always                   ',
            '/AMPUSER/2009/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2009/recording/out/external              : always                   ',
            '/AMPUSER/2009/recording/out/internal              : always                   ',
            '/AMPUSER/2009/recording/priority                  : 10                       ',
            '/AMPUSER/2009/ringtimer                           : 0                        ',
            '/AMPUSER/2009/voicemail                           : novm                     ',
            '/AMPUSER/2010/answermode                          : disabled                 ',
            '/AMPUSER/2010/cfringtimer                         : 0                        ',
            '/AMPUSER/2010/cidname                             : Ettore - Desenvolvimento ',
            '/AMPUSER/2010/cidnum                              : 2010                     ',
            '/AMPUSER/2010/concurrency_limit                   : 0                        ',
            '/AMPUSER/2010/device                              : 3000&2010                ',
            '/AMPUSER/2010/language                            :                          ',
            '/AMPUSER/2010/noanswer                            :                          ',
            '/AMPUSER/2010/outboundcid                         :                          ',
            '/AMPUSER/2010/password                            : 1234                     ',
            '/AMPUSER/2010/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2010/recording                           :                          ',
            '/AMPUSER/2010/recording/in/external               : always                   ',
            '/AMPUSER/2010/recording/in/internal               : always                   ',
            '/AMPUSER/2010/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2010/recording/out/external              : always                   ',
            '/AMPUSER/2010/recording/out/internal              : always                   ',
            '/AMPUSER/2010/recording/priority                  : 10                       ',
            '/AMPUSER/2010/ringtimer                           : 0                        ',
            '/AMPUSER/2010/voicemail                           : novm                     ',
            '/AMPUSER/2011/answermode                          : disabled                 ',
            '/AMPUSER/2011/cfringtimer                         : 0                        ',
            '/AMPUSER/2011/cidname                             : ATA Treinamento p1       ',
            '/AMPUSER/2011/cidnum                              : 2011                     ',
            '/AMPUSER/2011/concurrency_limit                   : 0                        ',
            '/AMPUSER/2011/device                              : 2011                     ',
            '/AMPUSER/2011/language                            :                          ',
            '/AMPUSER/2011/noanswer                            :                          ',
            '/AMPUSER/2011/outboundcid                         :                          ',
            '/AMPUSER/2011/password                            :                          ',
            '/AMPUSER/2011/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2011/recording                           :                          ',
            '/AMPUSER/2011/recording/in/external               : always                   ',
            '/AMPUSER/2011/recording/in/internal               : always                   ',
            '/AMPUSER/2011/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2011/recording/out/external              : always                   ',
            '/AMPUSER/2011/recording/out/internal              : always                   ',
            '/AMPUSER/2011/recording/priority                  : 0                        ',
            '/AMPUSER/2011/ringtimer                           : 0                        ',
            '/AMPUSER/2011/voicemail                           : novm                     ',
            '/AMPUSER/2012/answermode                          : disabled                 ',
            '/AMPUSER/2012/cfringtimer                         : 0                        ',
            '/AMPUSER/2012/cidname                             : ATA Treinamento p2       ',
            '/AMPUSER/2012/cidnum                              : 2012                     ',
            '/AMPUSER/2012/concurrency_limit                   : 0                        ',
            '/AMPUSER/2012/device                              : 2012                     ',
            '/AMPUSER/2012/language                            :                          ',
            '/AMPUSER/2012/noanswer                            :                          ',
            '/AMPUSER/2012/outboundcid                         :                          ',
            '/AMPUSER/2012/password                            :                          ',
            '/AMPUSER/2012/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2012/recording                           :                          ',
            '/AMPUSER/2012/recording/in/external               : always                   ',
            '/AMPUSER/2012/recording/in/internal               : always                   ',
            '/AMPUSER/2012/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2012/recording/out/external              : always                   ',
            '/AMPUSER/2012/recording/out/internal              : always                   ',
            '/AMPUSER/2012/recording/priority                  : 0                        ',
            '/AMPUSER/2012/ringtimer                           : 0                        ',
            '/AMPUSER/2012/voicemail                           : novm                     ',
            '/AMPUSER/2013/answermode                          : disabled                 ',
            '/AMPUSER/2013/cfringtimer                         : 0                        ',
            '/AMPUSER/2013/cidname                             : Suporte SmartPhone       ',
            '/AMPUSER/2013/cidnum                              : 2013                     ',
            '/AMPUSER/2013/concurrency_limit                   : 4                        ',
            '/AMPUSER/2013/device                              : 2013                     ',
            '/AMPUSER/2013/language                            :                          ',
            '/AMPUSER/2013/noanswer                            :                          ',
            '/AMPUSER/2013/outboundcid                         :                          ',
            '/AMPUSER/2013/password                            :                          ',
            '/AMPUSER/2013/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2013/recording                           :                          ',
            '/AMPUSER/2013/recording/in/external               : always                   ',
            '/AMPUSER/2013/recording/in/internal               : always                   ',
            '/AMPUSER/2013/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2013/recording/out/external              : always                   ',
            '/AMPUSER/2013/recording/out/internal              : always                   ',
            '/AMPUSER/2013/recording/priority                  : 0                        ',
            '/AMPUSER/2013/ringtimer                           : 0                        ',
            '/AMPUSER/2013/voicemail                           : novm                     ',
            '/AMPUSER/2014/answermode                          : disabled                 ',
            '/AMPUSER/2014/cfringtimer                         : 0                        ',
            '/AMPUSER/2014/cidname                             : Teste Dispositivo        ',
            '/AMPUSER/2014/cidnum                              : 2014                     ',
            '/AMPUSER/2014/concurrency_limit                   : 0                        ',
            '/AMPUSER/2014/device                              : 2014                     ',
            '/AMPUSER/2014/language                            :                          ',
            '/AMPUSER/2014/noanswer                            :                          ',
            '/AMPUSER/2014/outboundcid                         :                          ',
            '/AMPUSER/2014/password                            :                          ',
            '/AMPUSER/2014/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2014/recording                           :                          ',
            '/AMPUSER/2014/recording/in/external               : always                   ',
            '/AMPUSER/2014/recording/in/internal               : always                   ',
            '/AMPUSER/2014/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2014/recording/out/external              : always                   ',
            '/AMPUSER/2014/recording/out/internal              : always                   ',
            '/AMPUSER/2014/recording/priority                  : 0                        ',
            '/AMPUSER/2014/ringtimer                           : 0                        ',
            '/AMPUSER/2014/voicemail                           : novm                     ',
            '/AMPUSER/2015/answermode                          : disabled                 ',
            '/AMPUSER/2015/cfringtimer                         : 0                        ',
            '/AMPUSER/2015/cidname                             : Julio - Suporte          ',
            '/AMPUSER/2015/cidnum                              : 2015                     ',
            '/AMPUSER/2015/concurrency_limit                   : 0                        ',
            '/AMPUSER/2015/device                              : 2015                     ',
            '/AMPUSER/2015/language                            :                          ',
            '/AMPUSER/2015/noanswer                            :                          ',
            '/AMPUSER/2015/outboundcid                         :                          ',
            '/AMPUSER/2015/password                            :                          ',
            '/AMPUSER/2015/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2015/recording                           :                          ',
            '/AMPUSER/2015/recording/in/external               : always                   ',
            '/AMPUSER/2015/recording/in/internal               : always                   ',
            '/AMPUSER/2015/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2015/recording/out/external              : always                   ',
            '/AMPUSER/2015/recording/out/internal              : always                   ',
            '/AMPUSER/2015/recording/priority                  : 10                       ',
            '/AMPUSER/2015/ringtimer                           : 0                        ',
            '/AMPUSER/2015/voicemail                           : novm                     ',
            '/AMPUSER/2016/answermode                          : disabled                 ',
            '/AMPUSER/2016/cfringtimer                         : 0                        ',
            '/AMPUSER/2016/cidname                             : Suporte Movel            ',
            '/AMPUSER/2016/cidnum                              : 2016                     ',
            '/AMPUSER/2016/concurrency_limit                   : 0                        ',
            '/AMPUSER/2016/device                              : 2016                     ',
            '/AMPUSER/2016/followme/changecid                  : default                  ',
            '/AMPUSER/2016/followme/ddial                      : DIRECT                   ',
            '/AMPUSER/2016/followme/fixedcid                   :                          ',
            '/AMPUSER/2016/followme/grpconf                    : DISABLED                 ',
            '/AMPUSER/2016/followme/grplist                    : 982127932#               ',
            '/AMPUSER/2016/followme/grptime                    : 60                       ',
            '/AMPUSER/2016/followme/prering                    : 0                        ',
            '/AMPUSER/2016/language                            :                          ',
            '/AMPUSER/2016/noanswer                            :                          ',
            '/AMPUSER/2016/outboundcid                         :                          ',
            '/AMPUSER/2016/password                            :                          ',
            '/AMPUSER/2016/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2016/recording                           :                          ',
            '/AMPUSER/2016/recording/in/external               : always                   ',
            '/AMPUSER/2016/recording/in/internal               : always                   ',
            '/AMPUSER/2016/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2016/recording/out/external              : always                   ',
            '/AMPUSER/2016/recording/out/internal              : always                   ',
            '/AMPUSER/2016/recording/priority                  : 10                       ',
            '/AMPUSER/2016/ringtimer                           : 0                        ',
            '/AMPUSER/2016/voicemail                           : novm                     ',
            '/AMPUSER/2020/answermode                          : disabled                 ',
            '/AMPUSER/2020/cfringtimer                         : 0                        ',
            '/AMPUSER/2020/cidname                             : UraPrincipal IMF         ',
            '/AMPUSER/2020/cidnum                              : 2020                     ',
            '/AMPUSER/2020/concurrency_limit                   : 0                        ',
            '/AMPUSER/2020/device                              : 2020                     ',
            '/AMPUSER/2020/language                            :                          ',
            '/AMPUSER/2020/noanswer                            :                          ',
            '/AMPUSER/2020/outboundcid                         :                          ',
            '/AMPUSER/2020/password                            :                          ',
            '/AMPUSER/2020/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2020/recording                           :                          ',
            '/AMPUSER/2020/recording/in/external               : always                   ',
            '/AMPUSER/2020/recording/in/internal               : always                   ',
            '/AMPUSER/2020/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2020/recording/out/external              : always                   ',
            '/AMPUSER/2020/recording/out/internal              : always                   ',
            '/AMPUSER/2020/recording/priority                  : 10                       ',
            '/AMPUSER/2020/ringtimer                           : 0                        ',
            '/AMPUSER/2020/voicemail                           : novm                     ',
            '/AMPUSER/2021/answermode                          : disabled                 ',
            '/AMPUSER/2021/cfringtimer                         : 0                        ',
            '/AMPUSER/2021/cidname                             : UraNetwork - IMF         ',
            '/AMPUSER/2021/cidnum                              : 2021                     ',
            '/AMPUSER/2021/concurrency_limit                   : 0                        ',
            '/AMPUSER/2021/device                              : 2021                     ',
            '/AMPUSER/2021/language                            :                          ',
            '/AMPUSER/2021/noanswer                            :                          ',
            '/AMPUSER/2021/outboundcid                         :                          ',
            '/AMPUSER/2021/password                            :                          ',
            '/AMPUSER/2021/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2021/recording                           :                          ',
            '/AMPUSER/2021/recording/in/external               : always                   ',
            '/AMPUSER/2021/recording/in/internal               : always                   ',
            '/AMPUSER/2021/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2021/recording/out/external              : always                   ',
            '/AMPUSER/2021/recording/out/internal              : always                   ',
            '/AMPUSER/2021/recording/priority                  : 10                       ',
            '/AMPUSER/2021/ringtimer                           : 0                        ',
            '/AMPUSER/2021/voicemail                           : novm                     ',
            '/AMPUSER/2022/answermode                          : disabled                 ',
            '/AMPUSER/2022/cfringtimer                         : 0                        ',
            '/AMPUSER/2022/cidname                             : Rafael - IMF             ',
            '/AMPUSER/2022/cidnum                              : 2022                     ',
            '/AMPUSER/2022/concurrency_limit                   : 0                        ',
            '/AMPUSER/2022/device                              : 2022                     ',
            '/AMPUSER/2022/language                            :                          ',
            '/AMPUSER/2022/noanswer                            :                          ',
            '/AMPUSER/2022/outboundcid                         :                          ',
            '/AMPUSER/2022/password                            :                          ',
            '/AMPUSER/2022/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2022/recording                           :                          ',
            '/AMPUSER/2022/recording/in/external               : always                   ',
            '/AMPUSER/2022/recording/in/internal               : always                   ',
            '/AMPUSER/2022/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2022/recording/out/external              : always                   ',
            '/AMPUSER/2022/recording/out/internal              : always                   ',
            '/AMPUSER/2022/recording/priority                  : 0                        ',
            '/AMPUSER/2022/ringtimer                           : 0                        ',
            '/AMPUSER/2022/voicemail                           : novm                     ',
            '/AMPUSER/2023/answermode                          : disabled                 ',
            '/AMPUSER/2023/cfringtimer                         : 0                        ',
            '/AMPUSER/2023/cidname                             : Shima - IMF              ',
            '/AMPUSER/2023/cidnum                              : 2023                     ',
            '/AMPUSER/2023/concurrency_limit                   : 0                        ',
            '/AMPUSER/2023/device                              : 2023                     ',
            '/AMPUSER/2023/language                            :                          ',
            '/AMPUSER/2023/noanswer                            :                          ',
            '/AMPUSER/2023/outboundcid                         :                          ',
            '/AMPUSER/2023/password                            :                          ',
            '/AMPUSER/2023/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2023/recording                           :                          ',
            '/AMPUSER/2023/recording/in/external               : always                   ',
            '/AMPUSER/2023/recording/in/internal               : always                   ',
            '/AMPUSER/2023/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2023/recording/out/external              : always                   ',
            '/AMPUSER/2023/recording/out/internal              : always                   ',
            '/AMPUSER/2023/recording/priority                  : 10                       ',
            '/AMPUSER/2023/ringtimer                           : 0                        ',
            '/AMPUSER/2023/voicemail                           : novm                     ',
            '/AMPUSER/2098/answermode                          : disabled                 ',
            '/AMPUSER/2098/callmenum                           : 2098                     ',
            '/AMPUSER/2098/cfringtimer                         : 0                        ',
            '/AMPUSER/2098/cidname                             : VoicemailSuporte         ',
            '/AMPUSER/2098/cidnum                              : 2098                     ',
            '/AMPUSER/2098/concurrency_limit                   : 0                        ',
            '/AMPUSER/2098/device                              : 2098                     ',
            '/AMPUSER/2098/language                            :                          ',
            '/AMPUSER/2098/noanswer                            :                          ',
            '/AMPUSER/2098/outboundcid                         :                          ',
            '/AMPUSER/2098/password                            :                          ',
            '/AMPUSER/2098/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2098/recording                           :                          ',
            '/AMPUSER/2098/recording/in/external               : always                   ',
            '/AMPUSER/2098/recording/in/internal               : always                   ',
            '/AMPUSER/2098/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2098/recording/out/external              : always                   ',
            '/AMPUSER/2098/recording/out/internal              : always                   ',
            '/AMPUSER/2098/recording/priority                  : 0                        ',
            '/AMPUSER/2098/ringtimer                           : 0                        ',
            '/AMPUSER/2098/voicemail                           : default                  ',
            '/AMPUSER/2099/answermode                          : disabled                 ',
            '/AMPUSER/2099/cfringtimer                         : 0                        ',
            '/AMPUSER/2099/cidname                             : Fax Digital              ',
            '/AMPUSER/2099/cidnum                              : 2099                     ',
            '/AMPUSER/2099/concurrency_limit                   : 0                        ',
            '/AMPUSER/2099/device                              : 2099                     ',
            '/AMPUSER/2099/language                            :                          ',
            '/AMPUSER/2099/noanswer                            :                          ',
            '/AMPUSER/2099/outboundcid                         :                          ',
            '/AMPUSER/2099/password                            :                          ',
            '/AMPUSER/2099/queues/qnostate                     : usestate                 ',
            '/AMPUSER/2099/recording                           :                          ',
            '/AMPUSER/2099/recording/in/external               : dontcare                 ',
            '/AMPUSER/2099/recording/in/internal               : dontcare                 ',
            '/AMPUSER/2099/recording/ondemand                  : disabled                 ',
            '/AMPUSER/2099/recording/out/external              : dontcare                 ',
            '/AMPUSER/2099/recording/out/internal              : dontcare                 ',
            '/AMPUSER/2099/recording/priority                  : 0                        ',
            '/AMPUSER/2099/ringtimer                           : 0                        ',
            '/AMPUSER/2099/voicemail                           : novm                     ',
            '/AMPUSER/3000/answermode                          : disabled                 ',
            '/AMPUSER/3000/cfringtimer                         : 0                        ',
            '/AMPUSER/3000/cidname                             : Tecnosuper               ',
            '/AMPUSER/3000/cidnum                              : 3000                     ',
            '/AMPUSER/3000/concurrency_limit                   : 0                        ',
            '/AMPUSER/3000/language                            :                          ',
            '/AMPUSER/3000/noanswer                            :                          ',
            '/AMPUSER/3000/outboundcid                         :                          ',
            '/AMPUSER/3000/password                            :                          ',
            '/AMPUSER/3000/queues/qnostate                     : usestate                 ',
            '/AMPUSER/3000/recording                           :                          ',
            '/AMPUSER/3000/recording/in/external               : always                   ',
            '/AMPUSER/3000/recording/in/internal               : always                   ',
            '/AMPUSER/3000/recording/ondemand                  : disabled                 ',
            '/AMPUSER/3000/recording/out/external              : always                   ',
            '/AMPUSER/3000/recording/out/internal              : always                   ',
            '/AMPUSER/3000/recording/priority                  : 0                        ',
            '/AMPUSER/3000/ringtimer                           : 0                        ',
            '/AMPUSER/3000/voicemail                           : novm                     ',
            '/AMPUSER/400/answermode                           : disabled                 ',
            '/AMPUSER/400/cfringtimer                          : 0                        ',
            '/AMPUSER/400/cidname                              : Videoconf                ',
            '/AMPUSER/400/cidnum                               : 400                      ',
            '/AMPUSER/400/concurrency_limit                    : 0                        ',
            '/AMPUSER/400/device                               : 400                      ',
            '/AMPUSER/400/language                             :                          ',
            '/AMPUSER/400/noanswer                             :                          ',
            '/AMPUSER/400/outboundcid                          :                          ',
            '/AMPUSER/400/password                             :                          ',
            '/AMPUSER/400/queues/qnostate                      : usestate                 ',
            '/AMPUSER/400/recording                            :                          ',
            '/AMPUSER/400/recording/in/external                : dontcare                 ',
            '/AMPUSER/400/recording/in/internal                : dontcare                 ',
            '/AMPUSER/400/recording/ondemand                   : disabled                 ',
            '/AMPUSER/400/recording/out/external               : dontcare                 ',
            '/AMPUSER/400/recording/out/internal               : dontcare                 ',
            '/AMPUSER/400/recording/priority                   : 10                       ',
            '/AMPUSER/400/ringtimer                            : 0                        ',
            '/AMPUSER/400/voicemail                            : novm                     ',
            '468 results found.',
            '--END COMMAND--'
        ]) + '\r\n'
        self.assertTrue(Response.match(follows_response))
        response = Response.read(follows_response)
        self.assertFalse(response.is_error())
        self.assertIsNotNone(response.follows)
        self.assertListEqual(LINE_REGEX.split(follows_response)[2:-1], response.follows)
        self.assertEqual(follows_response, str(response))

    def test_with_follows_queue_show_command(self):
        """
        Action: Command
        Command: queue show
        """
        follows_response = '\n'.join([
            'Response: Follows\r',
            'Privilege: Command\r',
            '911 has 0 calls (max unlimited) in \'ringall\' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s',
            '   Members: ',
            '      Carol - Financeiro (Local/2007@from-queue/n from hint:2007@ext-local) (ringinuse enabled) (Unavailable) has taken no calls yet',
            '   No Callers',
            '',
            '910 has 0 calls (max unlimited) in \'ringall\' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s',
            '   Members: ',
            '      Julio - Suporte (Local/2015@from-queue/n from hint:2015@ext-local) (ringinuse enabled) (Unavailable) has taken no calls yet',
            '      Ettore - Desenvolvimento (Local/2010@from-queue/n from hint:2010@ext-local) (ringinuse enabled) (dynamic) (Unavailable) has taken no calls yet',
            '      Gustavo - Suporte (Local/2004@from-queue/n from hint:2004@ext-local) (ringinuse enabled) (Unavailable) has taken no calls yet',
            '   No Callers',
            '',
            '912 has 0 calls (max unlimited) in \'ringall\' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s',
            '   Members: ',
            '      Carol - Financeiro (Local/2007@from-queue/n from hint:2007@ext-local) (ringinuse enabled) (Unavailable) has taken no calls yet',
            '   No Callers',
            '',
            '901 has 0 calls (max unlimited) in \'ringall\' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s',
            '   Members: ',
            '      NoteTeo (Local/2005@from-queue/n from hint:2005@ext-local) (ringinuse enabled) (Unavailable) has taken no calls yet',
            '      Carol - Financeiro (Local/2007@from-queue/n from hint:2007@ext-local) (ringinuse enabled) (Unavailable) has taken no calls yet',
            '   No Callers',
            '',
            'default has 0 calls (max unlimited) in \'ringall\' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 0s',
            '   No Members',
            '   No Callers',
            '',
            '902 has 0 calls (max unlimited) in \'ringall\' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s',
            '   Members: ',
            '      Julio - Suporte (Local/2015@from-queue/n from hint:2015@ext-local) (ringinuse enabled) (dynamic) (Unavailable) has taken no calls yet',
            '      Wainer (Local/2000@from-queue/n from hint:2000@ext-local) (ringinuse enabled) (dynamic) (Unavailable) has taken no calls yet',
            '      Gustavo - Suporte (Local/2004@from-queue/n from hint:2004@ext-local) (ringinuse enabled) (dynamic) (Unavailable) has taken no calls yet',
            '   No Callers',
            '',
            '903 has 0 calls (max unlimited) in \'ringall\' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s',
            '   Members: ',
            '      Ettore - Desenvolvimento (Local/2010@from-queue/n from hint:2010@ext-local) (ringinuse enabled) (dynamic) (paused) (Unavailable) has taken no calls yet',
            '   No Callers',
            '',
            '951 has 0 calls (max unlimited) in \'ringall\' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s',
            '   Members: ',
            '      Wainer (Local/2000@from-queue/n from hint:2000@ext-local) (ringinuse enabled) (dynamic) (Unavailable) has taken no calls yet',
            '   No Callers',
            '',
            '950 has 0 calls (max 1) in \'ringall\' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s',
            '   Members: ',
            '      Ettore - Desenvolvimento (Local/2010@from-queue/n from hint:2010@ext-local) (ringinuse enabled) (dynamic) (paused) (Unavailable) has taken no calls yet',
            '   No Callers',
            '',
            '--END COMMAND--',
        ]) + '\r\n'
        self.assertTrue(Response.match(follows_response))
        response = Response.read(follows_response)
        self.assertFalse(response.is_error())
        self.assertIsNotNone(response.follows)
        self.assertListEqual(LINE_REGEX.split(follows_response)[2:-1], response.follows)
        self.assertEqual(follows_response, str(response))

    def test_event(self):
        event = '\r\n'.join([
            'Event: FullyBooted',
            'Privilege: system, all',
            'Status: Fully Booted',
        ])
        self.assertFalse(Response.match(event))
        with self.assertRaises(Exception):
            Response.read(event)


TIMEOUT = 0.01


class TestFutureResponse(unittest.TestCase):
    response = None

    def test_callback(self):
        expected = 'some response'

        def callback(response):
            self.response = response

        future = FutureResponse(callback=callback)
        future.response = expected
        self.assertEqual(future.response, expected)
        self.assertEqual(self.response, expected)

    def test_lock_timeout(self):
        future = FutureResponse(timeout=TIMEOUT)
        before = time.time()
        response = future.response
        after = time.time()
        diff = after - before
        self.assertIsNone(response)
        self.assertAlmostEqual(diff, TIMEOUT, delta=0.1)

    def test_lock(self):
        expected = 'some respone'
        future = FutureResponse()

        def runnable():
            time.sleep(TIMEOUT)
            future.response = expected

        start_new_thread(runnable, (), {})
        response = future.response
        self.assertEqual(response, expected)
