|
1
|
|
-'''
|
|
2
|
|
-Module with dummy classes for unit testing of XMPP and related code.
|
|
3
|
|
-'''
|
|
4
|
|
-
|
|
5
|
|
-import threading, time
|
|
6
|
|
-
|
|
7
|
|
-from mock import Mock
|
|
8
|
|
-
|
|
9
|
|
-from nbxmpp import idlequeue
|
|
10
|
|
-
|
|
11
|
|
-IDLEQUEUE_INTERVAL = 0.2 # polling interval. 200ms is used in Gajim as default
|
|
12
|
|
-IDLEMOCK_TIMEOUT = 30 # how long we wait for an event
|
|
13
|
|
-
|
|
14
|
|
-class IdleQueueThread(threading.Thread):
|
|
15
|
|
- '''
|
|
16
|
|
- Thread for regular processing of idlequeue.
|
|
17
|
|
- '''
|
|
18
|
|
- def __init__(self):
|
|
19
|
|
- self.iq = idlequeue.IdleQueue()
|
|
20
|
|
- self.stop = threading.Event() # Event to stop the thread main loop.
|
|
21
|
|
- self.stop.clear()
|
|
22
|
|
- threading.Thread.__init__(self)
|
|
23
|
|
-
|
|
24
|
|
- def run(self):
|
|
25
|
|
- while not self.stop.isSet():
|
|
26
|
|
- self.iq.process()
|
|
27
|
|
- time.sleep(IDLEQUEUE_INTERVAL)
|
|
28
|
|
-
|
|
29
|
|
- def stop_thread(self):
|
|
30
|
|
- self.stop.set()
|
|
31
|
|
-
|
|
32
|
|
-
|
|
33
|
|
-class IdleMock:
|
|
34
|
|
- '''
|
|
35
|
|
- Serves as template for testing objects that are normally controlled by GUI.
|
|
36
|
|
- Allows to wait for asynchronous callbacks with wait() method.
|
|
37
|
|
- '''
|
|
38
|
|
- def __init__(self):
|
|
39
|
|
- self._event = threading.Event()
|
|
40
|
|
- self._event.clear()
|
|
41
|
|
-
|
|
42
|
|
- def wait(self):
|
|
43
|
|
- '''
|
|
44
|
|
- Block until some callback sets the event and clearing the event
|
|
45
|
|
- subsequently.
|
|
46
|
|
- Returns True if event was set, False on timeout
|
|
47
|
|
- '''
|
|
48
|
|
- self._event.wait(IDLEMOCK_TIMEOUT)
|
|
49
|
|
- if self._event.isSet():
|
|
50
|
|
- self._event.clear()
|
|
51
|
|
- return True
|
|
52
|
|
- else:
|
|
53
|
|
- return False
|
|
54
|
|
-
|
|
55
|
|
- def set_event(self):
|
|
56
|
|
- self._event.set()
|
|
57
|
|
-
|
|
58
|
|
-class MockConnection(IdleMock, Mock):
|
|
59
|
|
- '''
|
|
60
|
|
- Class simulating Connection class from gajim/common/connection.py
|
|
61
|
|
-
|
|
62
|
|
- It is derived from Mock in order to avoid defining all methods
|
|
63
|
|
- from real Connection that are called from NBClient or Dispatcher
|
|
64
|
|
- ( _event_dispatcher for example)
|
|
65
|
|
- '''
|
|
66
|
|
-
|
|
67
|
|
- def __init__(self, *args):
|
|
68
|
|
- self.connect_succeeded = True
|
|
69
|
|
- IdleMock.__init__(self)
|
|
70
|
|
- Mock.__init__(self, *args)
|
|
71
|
|
-
|
|
72
|
|
- def on_connect(self, success, *args):
|
|
73
|
|
- '''
|
|
74
|
|
- Method called after connecting - after receiving <stream:features>
|
|
75
|
|
- from server (NOT after TLS stream restart) or connect failure
|
|
76
|
|
- '''
|
|
77
|
|
- self.connect_succeeded = success
|
|
78
|
|
- self.set_event()
|
|
79
|
|
-
|
|
80
|
|
-
|
|
81
|
|
- def on_auth(self, con, auth):
|
|
82
|
|
- '''
|
|
83
|
|
- Method called after authentication, regardless of the result.
|
|
84
|
|
-
|
|
85
|
|
- :Parameters:
|
|
86
|
|
- con : NonBlockingClient
|
|
87
|
|
- reference to authenticated object
|
|
88
|
|
- auth : string
|
|
89
|
|
- type of authetication in case of success ('old_auth', 'sasl') or
|
|
90
|
|
- None in case of auth failure
|
|
91
|
|
- '''
|
|
92
|
|
- self.auth_connection = con
|
|
93
|
|
- self.auth = auth
|
|
94
|
|
- self.set_event()
|