Dan Talayco | ea8ad80 | 2010-02-22 20:30:18 -0800 | [diff] [blame] | 1 | |
| 2 | """ |
| 3 | Utilities for the OpenFlow test framework |
| 4 | """ |
| 5 | |
| 6 | import random |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 7 | import time |
Dan Talayco | ea8ad80 | 2010-02-22 20:30:18 -0800 | [diff] [blame] | 8 | |
| 9 | def gen_xid(): |
| 10 | return random.randrange(1,0xffffffff) |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 11 | |
| 12 | """ |
| 13 | Wait on a condition variable until the given function returns non-None or a timeout expires. |
| 14 | The condition variable must already be acquired. |
| 15 | There is deliberately no support for an infinite timeout. |
| 16 | TODO: get the default timeout from configuration |
| 17 | """ |
| 18 | def timed_wait(cv, fn, timeout=10): |
| 19 | end_time = time.time() + timeout |
| 20 | while True: |
| 21 | if time.time() > end_time: |
| 22 | return None |
| 23 | |
| 24 | val = fn() |
| 25 | if val != None: |
| 26 | return val |
| 27 | |
| 28 | remaining_time = end_time - time.time() |
| 29 | cv.wait(remaining_time) |