forked from RedhawkSDR/rest-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_concurrent.py
More file actions
executable file
·111 lines (89 loc) · 3.49 KB
/
test_concurrent.py
File metadata and controls
executable file
·111 lines (89 loc) · 3.49 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
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
#!/usr/bin/env python
#
# This file is protected by Copyright. Please refer to the COPYRIGHT file
# distributed with this source distribution.
#
# This file is part of REDHAWK rtl-demo-app.
#
# REDHAWK rtl-demo-app is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# REDHAWK rtl-demo-app is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
import unittest
import sys
import logging
import time
from tornado import gen
from tornado.testing import AsyncTestCase, LogTrapTestCase, main, gen_test
from futures import ThreadPoolExecutor
import tasking
EXECUTOR = ThreadPoolExecutor(4)
# all method returning suite is required by tornado.testing.main()
def all():
return unittest.TestLoader().loadTestsFromModule(__import__(__name__))
class FuturesTest(AsyncTestCase):
@tasking.background_task
def _sleepfunc(self, input, duration=1, exception=False):
logging.debug("_sleepfun start")
time.sleep(duration)
if exception:
raise input
logging.debug("_sleepfun end")
return input
@gen_test
def test_background_future(self):
'''
Runs a thread that sleeps in the background and generates a Future to indicate it's
done or raised an exception.
'''
f = yield self._sleepfunc("the input is 1", 1)
logging.debug("The future is %s", f)
self.assertEquals("the input is 1", f)
@gen_test
def test_background_future_except1(self):
'''
Runs a thread that sleeps in the background and generates a Future to indicate it's
done or raised an exception.
'''
try:
f = yield self._sleepfunc(ValueError('ignore me'), 1, exception=True)
self.fail("Expecting ValueError")
except ValueError:
logging.info("Manual inspection of stack trace required:", exc_info=1)
def test_background_future_exception(self):
'''
Runs a thread that sleeps in the background and generates a Future to indicate it's
done or raised an exception.
'''
f = self._sleepfunc(ValueError('ignore me'), 1, exception=True)
logging.debug("The future is %s", f)
self.io_loop.add_future(f, self.stop)
f2 = self.wait()
# print "RESULT IS '%s'" % self.wait()
self.assertEquals(ValueError, type(f2.exception()))
@gen_test
def test_callback_future(self):
@tasking.safe_return_future
def cbfunc(input, duration=1, callback=None):
logging.debug("input=%s, callback=%s", input, callback)
def background(i):
logging.debug("invoking cbfunc")
time.sleep(duration)
callback(i)
EXECUTOR.submit(background, input)
f = yield cbfunc("the input is 1", 1)
logging.debug("The future is %s", f)
self.assertEquals("the input is 1", f)
#TODO: Add tests for callback options
if __name__ == '__main__':
# to enable logging, use --logging=debug
main()