-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
252 lines (193 loc) · 6.57 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from asynclapi import *
import clapi as api
import unittest
import time
# Mock for SerialWrapper
class SerialWrapper_Mock:
data = None
last_push = None
def inWaiting(self):
return self.data != None
# отправка стандартного сообщения на Arduino
def push(self, code:int, args):
self.last_push = {"code":code,"args":args}
def request(self, code:int, args):
self.push(code, args)
return self.pull()
def flush(self):
pass
def clear_input(self):
pass
# принять сообщение от Arduino
def pull(self):
kek = self.data
self.data = None
return kek
# установка соединения с Arduino
def handshake(self):
return {'device_id':'test'}
# Mock for Core
class Core_Mock():
devices = list()
# Put devices mocks
def __init__(self, devices):
for d in devices:
if hasattr(d,'id'):
setattr(api, str(d.id), d)
self.devices.append(d)
# Mock for device
class Device_Mock(api.Device):
def __init__(self, id):
self.serial = SerialWrapper_Mock()
self.task_pool = TaskPool(self.serial)
self.id = id
test_async_request_var = 0
class ClapiFunctionalTest(unittest.TestCase):
""" Clapi functions test """
@classmethod
def setUpClass(cls):
print("Set up ClapiTest \n=============================")
@classmethod
def tearDownClass(cls):
print("\n=============================\nTear down ClapiTest")
def setUp(self):
print("\nSet up for [" + self.shortDescription() + "]")
# set up our fake devices
devices = [
Device_Mock("dev1"),
Device_Mock("dev2")
]
# instead of api.start():
api.core = Core_Mock(devices)
def tearDown(self):
print("Tear down for [" + self.shortDescription() + "]")
api.core = None
def test_push(self):
"""test push"""
print(self.id)
code = 0x42
args = [1, 2, 3]
try:
api.dev1.push(code, *args)
api.dev2.push(code, *args)
except:
self.fail("Something went wrong with sync PUSH")
def test_pull(self):
"""test pull"""
print(self.id)
s1 = api.dev1.serial
s2 = api.dev2.serial
s1.data = '{"status":200}'
s2.data = '{"status":300}'
self.assertEqual(api.dev1.pull(), {"status": 200})
self.assertEqual(api.dev2.pull(), {"status": 300})
def test_request(self):
"""test request"""
print(self.id)
code = 0x42
args = [1, 2, 3]
s1 = api.dev1.serial
s2 = api.dev2.serial
s1.data = '{"status":200,"code":%d}'%(code)
s2.data = '{"status":300,"code":%d}'%(code)
try:
r1 = api.dev1.request(code, *args)
r2 = api.dev2.request(code, *args)
except:
self.fail("Something went wrong with sync PUSH")
self.assertEqual(r1, {"status": 200, "code": code})
self.assertEqual(r2, {"status": 300, "code": code})
def test_method_chaining(self):
"""test method chaining"""
print(self.id)
def simple_callback(kek):
pass
try:
l = LongPoll(4)\
.args(26) \
.callback(simple_callback)
r = Request(4) \
.args(26) \
.callback(simple_callback)
p = Push(4) \
.args(26)
except:
self.fail("Something wrong with method chaining")
def test_async_push(self):
"""test ASYNC push"""
print(self.id)
args = [1, 2, 3]
s1 = api.dev1.serial
s2 = api.dev2.serial
tp1 = api.dev1.task_pool
tp2 = api.dev2.task_pool
try:
p1=api.dev1.push_async(98)\
.args(*args)\
#.execute()
p2=api.dev2.push_async(99)\
.args(*args)
#.execute()
except:
self.fail("Something went wrong with sync PUSH")
p1.execute()
p2.execute()
time.sleep(0.2) # wait main_loop thread of each device
api.dev1.task_pool.running = False
api.dev2.task_pool.running = False
time.sleep(0.2) # wait main_loop thread stops
self.assertEqual({"code":98,"args":args}, s1.last_push)
self.assertEqual({"code":99,"args":args}, s2.last_push)
if tp1.main_thread:
self.assertFalse(tp1.main_thread.isAlive())
if tp2.main_thread:
self.assertFalse(tp2.main_thread.isAlive())
self.assertTrue(len(tp1.tasks) == 0)
self.assertTrue(len(tp2.tasks) == 0)
self.assertTrue(len(tp1.subscribers.items()) == 0)
self.assertTrue(len(tp2.subscribers.items()) == 0)
def test_async_request(self):
"""test ASYNC request"""
print(self.id)
args = [25, 2]
s1 = api.dev1.serial
s2 = api.dev2.serial
tp1 = api.dev1.task_pool
tp2 = api.dev2.task_pool
def simple_callback(data):
global test_async_request_var
if data["code"] == 4:
test_async_request_var += 1
if data["code"] == 5:
test_async_request_var += 2
try:
p1=api.dev1.request_async(4)\
.args(*args)\
.callback(simple_callback)
#.execute()
p2=api.dev2.request_async(5)\
.args(*args)\
.callback(simple_callback)
#.execute()
except:
self.fail("Something went wrong with sync REQUEST")
p1.execute()
p2.execute()
time.sleep(0.2) # wait main_loop thread stops
s1.data = '{"code":4}'
s2.data = '{"code":5}'
time.sleep(0.2) # wait main_loop thread stops
api.dev1.task_pool.running = False
api.dev2.task_pool.running = False
time.sleep(0.2) # wait main_loop thread stops
self.assertEqual(test_async_request_var, 3)
if tp1.main_thread:
self.assertFalse(tp1.main_thread.isAlive())
if tp2.main_thread:
self.assertFalse(tp2.main_thread.isAlive())
self.assertTrue(len(tp1.tasks) == 0)
self.assertTrue(len(tp2.tasks) == 0)
self.assertTrue(len(tp1.subscribers.items()) == 0)
self.assertTrue(len(tp2.subscribers.items()) == 0)
if __name__=="__main__":
unittest.main()