Changeset 9822

Show
Ignore:
Timestamp:
06/19/08 01:58:19 (6 months ago)
Author:
tomk
Message:

added stub for new transports module plus basic test for it, testing code reorganized

Location:
branches/bosh_support
Files:
3 added
4 modified

Legend:

Unmodified
Added
Removed
  • branches/bosh_support/src/common/xmpp/debug.py

    r9487 r9822  
    155155                  # show(), set this to 0 
    156156                  # 
    157                   validate_flags = 1, 
     157                  validate_flags = 0, 
    158158                  # 
    159159                  # If you dont want the welcome message, set to 0 
  • branches/bosh_support/src/common/xmpp/idlequeue.py

    r8704 r9822  
    5454         
    5555        def remove_timeout(self, fd): 
     56                print 'read timeout removed for fd %s' % fd 
    5657                if self.read_timeouts.has_key(fd): 
    5758                        del(self.read_timeouts[fd]) 
     
    6970                ''' set a new timeout, if it is not removed after 'seconds',  
    7071                then obj.read_timeout() will be called ''' 
     72                print 'read timeout set for fd %s on %s seconds' % (fd, seconds) 
    7173                timeout = self.current_time() + seconds 
    7274                self.read_timeouts[fd] = timeout 
  • branches/bosh_support/src/common/xmpp/transports_nb.py

    r9794 r9822  
    338338 
    339339        def _plug_idle(self): 
     340                # readable if socket is connected or disconnecting 
    340341                readable = self.state != 0 
     342                # writeable if sth to send 
    341343                if self.sendqueue or self.sendbuff: 
    342344                        writable = True 
     
    347349         
    348350        def pollout(self): 
     351                print 'pollout called - send possible' 
    349352                if self.state == 0: 
    350353                        self.connect_to_next_ip() 
     
    360363         
    361364        def pollin(self): 
     365                print 'pollin called - receive possible' 
    362366                self._do_receive()  
    363367         
     
    584588 
    585589                try: 
     590                        print "==============sock.connect called" 
    586591                        self._sock.connect(self._server) 
    587592                        self._sock.setblocking(False) 
     
    589594                        (errnum, errstr) = ee 
    590595                # in progress, or would block 
     596                print "errnum: %s" % errnum 
    591597                if errnum in (errno.EINPROGRESS, errno.EALREADY, errno.EWOULDBLOCK): 
    592598                        self.state = 1 
  • branches/bosh_support/test/test_client_nb.py

    r9794 r9822  
    1 import unittest, threading 
    2 from mock import Mock 
     1import unittest 
     2from xmpp_mocks import * 
    33 
    4 import sys, time, os.path 
     4import sys, os.path 
    55 
    66gajim_root = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..') 
     
    88sys.path.append(gajim_root + '/src/common/xmpp') 
    99 
    10 import client_nb, idlequeue 
     10import client_nb 
    1111 
    1212''' 
     
    1616''' 
    1717 
    18 idlequeue_interval = 0.2 
    19 ''' 
    20 IdleQueue polling interval. 200ms is used in Gajim as default 
    21 ''' 
    2218 
    2319xmpp_server_port = ('xmpp.example.org',5222) 
     
    2723''' 
    2824 
    29 credentials = ['primus', 'l0v3', 'testclient'] 
     25credentials = ['login', 'pass', 'testclient'] 
    3026''' 
    3127[username, password, passphrase] 
    3228Script will autheticate itself with this credentials on above mentioned server. 
    3329''' 
    34  
    35  
    36 class MockConnectionClass(Mock): 
    37         ''' 
    38         Class simulating Connection class from src/common/connection.py 
    39  
    40         It is derived from Mock in order to avoid defining all methods 
    41         from real Connection that are called from NBClient or Dispatcher 
    42         ( _event_dispatcher for example) 
    43         ''' 
    44  
    45         def __init__(self, *args): 
    46                 self.event = threading.Event() 
    47                 ''' 
    48                 is used for waiting on connect, auth and disconnect callbacks 
    49                 ''' 
    50  
    51                 self.event.clear() 
    52                 Mock.__init__(self, *args) 
    53  
    54         def on_connect(self, *args): 
    55                 ''' 
    56                 Method called on succesful connecting - after receiving <stream:features> 
    57                 from server (NOT after TLS stream restart). 
    58                 ''' 
    59  
    60                 #print 'on_connect - args:' 
    61                 #for i in args: 
    62                 #       print '    %s' % i 
    63                 self.connect_failed = False 
    64                 self.event.set() 
    65  
    66         def on_connect_failure(self, *args): 
    67                 ''' 
    68                 Method called on failure while connecting - on everything from TCP error 
    69                 to error during TLS handshake 
    70                 ''' 
    71  
    72                 #print 'on_connect failure - args:' 
    73                 #for i in args: 
    74                 #       print '    %s' % i 
    75                 self.connect_failed = True 
    76                 self.event.set() 
    77          
    78         def on_auth(self, con, auth): 
    79                 ''' 
    80                 Method called after authentication is done regardless on the result. 
    81  
    82                 :Parameters: 
    83                         con : NonBlockingClient 
    84                                 reference to authenticated object 
    85                         auth : string 
    86                                 type of authetication in case of success ('old_auth', 'sasl') or 
    87                                 None in case of auth failure 
    88                 ''' 
    89  
    90                 #print 'on_auth - args:' 
    91                 #print '    con: %s' % con 
    92                 #print '    auth: %s' % auth 
    93                 self.auth_connection = con 
    94                 self.auth = auth 
    95                 self.event.set() 
    96  
    97         def wait(self): 
    98                 ''' 
    99                 Waiting until some callback sets the event and clearing the event subsequently.  
    100                 ''' 
    101  
    102                 self.event.wait() 
    103                 self.event.clear() 
    104  
    105          
    106  
    107  
    108 class IdleQueueThread(threading.Thread): 
    109         ''' 
    110         Thread for regular processing of idlequeue. 
    111         ''' 
    112         def __init__(self): 
    113                 self.iq = idlequeue.IdleQueue() 
    114                 self.stop = threading.Event() 
    115                 ''' 
    116                 Event used to stopping the thread main loop. 
    117                 ''' 
    118  
    119                 self.stop.clear() 
    120                 threading.Thread.__init__(self) 
    121          
    122         def run(self): 
    123                 while not self.stop.isSet(): 
    124                         self.iq.process() 
    125                         time.sleep(idlequeue_interval) 
    126                 self.iq.process() 
    127  
    128         def stop_thread(self): 
    129                 self.stop.set() 
    130  
    131  
    132          
    13330 
    13431class TestNonBlockingClient(unittest.TestCase): 
     
    14845                                server=xmpp_server_port[0], 
    14946                                port=xmpp_server_port[1], 
    150                                 on_connect=self.connection.on_connect, 
    151                                 on_connect_failure=self.connection.on_connect_failure, 
     47                                on_connect=lambda *args: self.connection.on_connect(True, *args), 
     48                                on_connect_failure=lambda *args: self.connection.on_connect(False, *args), 
    15249                                caller=self.connection  
    15350                                ) 
     
    18178                 
    18279                # if on_connect was called, client has to be connected and vice versa 
    183                 if self.connection.connect_failed: 
     80                if self.connection.connect_succeeded: 
     81                        self.assert_(self.client.isConnected()) 
     82                else: 
    18483                        self.assert_(not self.client.isConnected()) 
    185                 else: 
    186                         self.assert_(self.client.isConnected()) 
    18784 
    18885        def client_auth(self, username, password, resource, sasl): 
     
    204101                Does disconnecting of connected client. Returns when TCP connection is closed. 
    205102                ''' 
    206                 self.client.start_disconnect(None, on_disconnect=self.connection.event.set) 
     103                self.client.start_disconnect(None, on_disconnect=self.connection.set_event) 
    207104 
    208105                print 'waiting for disconnecting...' 
     
    261158                self.open_stream(xmpp_server_port) 
    262159                self.assert_(self.client.isConnected()) 
    263                 self.client_auth(credentials[0], "wrong pass", credentials[2], sasl=0) 
     160                self.client_auth(credentials[0], "wrong pass", credentials[2], sasl=1) 
    264161                self.assert_(self.connection.auth is None) 
    265162                self.do_disconnect() 
     
    272169if __name__ == '__main__': 
    273170 
    274         suite = unittest.TestLoader().loadTestsFromTestCase(TestNonBlockingClient) 
     171        #suite = unittest.TestLoader().loadTestsFromTestCase(TestNonBlockingClient) 
     172        suite = unittest.TestSuite() 
     173        suite.addTest(TestNonBlockingClient('test_proper_connect_sasl')) 
     174 
    275175        unittest.TextTestRunner(verbosity=2).run(suite) 
    276176