-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestmainloop.py
More file actions
70 lines (53 loc) · 1.76 KB
/
testmainloop.py
File metadata and controls
70 lines (53 loc) · 1.76 KB
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
import unittest
import stackless
import stacklesslib.main
import stacklesslib.app
class TestMainLoop(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
self.checkLeftThingsClean()
def checkLeftThingsClean(self):
#self.assertEqual(len(stacklesslib.main.event_queue), 0)
return True
def testPreemptiveRun(self):
"""
Create a tasklet and run it pre-emptively, ensuring that we
get the tasklet returned from 'run_tasklets' when it is
interrupted.
"""
t = stackless.tasklet(ArbitraryFunc)()
t.run()
while t.alive:
stacklesslib.main.mainloop.pump(0)
ret = stacklesslib.main.mainloop.run_tasklets(100)
if ret is None and t.alive:
continue
self.assertEqual(ret, t)
break
else:
self.fail("Tasklet was not interrupted")
def testCooperativeRun(self):
"""
Create a tasklet and run it cooperatively, ensuring we never
get it interrupted and returned from run_tasklets.
"""
t = stackless.tasklet(ArbitraryFunc)()
t.run()
while t.alive:
stacklesslib.main.mainloop.pump(0)
ret = stacklesslib.main.mainloop.run_tasklets()
self.assertFalse(ret)
def ArbitraryFunc():
sum = 0
for i in range(1000):
for j in range(1000):
sum += 10
stacklesslib.app.sleep(0)
def load_tests(loader, tests, pattern): # test loader protocol
suite = unittest.TestSuite()
suite.addTests(tests)
stacklesslib.app.install_stackless()
return suite
if __name__ == '__main__':
unittest.main()