Revision 27abfa9f
b/kamaki/clients/test.py | ||
---|---|---|
96 | 96 |
|
97 | 97 |
class SilentEvent(TestCase): |
98 | 98 |
|
99 |
def thread_content(self, methodid): |
|
99 |
def thread_content(self, methodid, raiseException=0):
|
|
100 | 100 |
wait = 0.1 |
101 | 101 |
self.can_finish = -1 |
102 | 102 |
while self.can_finish < methodid and wait < 4: |
103 | 103 |
sleep(wait) |
104 | 104 |
wait = 2 * wait |
105 |
if raiseException and raiseException == methodid: |
|
106 |
raise Exception('Some exception') |
|
105 | 107 |
self._value = methodid |
106 | 108 |
self.assertTrue(wait < 4) |
107 | 109 |
|
... | ... | |
109 | 111 |
from kamaki.clients import SilentEvent |
110 | 112 |
self.SE = SilentEvent |
111 | 113 |
|
112 |
def test_threads(self): |
|
113 |
threads = [] |
|
114 |
for i in range(4): |
|
115 |
threads.append(self.SE(self.thread_content, i)) |
|
116 |
|
|
114 |
def test_run(self): |
|
115 |
threads = [self.SE(self.thread_content, i) for i in range(4)] |
|
117 | 116 |
for t in threads: |
118 | 117 |
t.start() |
119 | 118 |
|
... | ... | |
123 | 122 |
threads[i].join() |
124 | 123 |
self.assertFalse(threads[i].is_alive()) |
125 | 124 |
|
125 |
def test_value(self): |
|
126 |
threads = [self.SE(self.thread_content, i) for i in range(4)] |
|
127 |
for t in threads: |
|
128 |
t.start() |
|
129 |
|
|
130 |
for mid, t in enumerate(threads): |
|
131 |
if t.is_alive(): |
|
132 |
self.can_finish = mid |
|
133 |
continue |
|
134 |
self.assertTrue(mid, t.value) |
|
135 |
|
|
136 |
def test_exception(self): |
|
137 |
threads = [self.SE(self.thread_content, i, (i % 2)) for i in range(4)] |
|
138 |
for t in threads: |
|
139 |
t.start() |
|
140 |
|
|
141 |
for i, t in enumerate(threads): |
|
142 |
if t.is_alive(): |
|
143 |
self.can_finish = i |
|
144 |
continue |
|
145 |
if i % 2: |
|
146 |
self.assertTrue(isinstance(t.exception, Exception)) |
|
147 |
else: |
|
148 |
self.assertFalse(t.exception) |
|
149 |
|
|
126 | 150 |
|
127 | 151 |
# TestCase auxiliary methods |
128 | 152 |
|
Also available in: Unified diff