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 |
Rich Lane | 4dfd5e1 | 2012-12-22 19:48:01 -0800 | [diff] [blame] | 8 | import os |
Rich Lane | 7e1d5c5 | 2012-12-24 14:08:32 -0800 | [diff] [blame] | 9 | import fcntl |
Rich Lane | 2e37ec2 | 2012-12-26 09:47:39 -0800 | [diff] [blame] | 10 | import logging |
Dan Talayco | ea8ad80 | 2010-02-22 20:30:18 -0800 | [diff] [blame] | 11 | |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 12 | default_timeout = None # set by oft |
| 13 | |
Dan Talayco | ea8ad80 | 2010-02-22 20:30:18 -0800 | [diff] [blame] | 14 | def gen_xid(): |
| 15 | return random.randrange(1,0xffffffff) |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 16 | |
| 17 | """ |
| 18 | Wait on a condition variable until the given function returns non-None or a timeout expires. |
| 19 | The condition variable must already be acquired. |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 20 | The timeout value -1 means use the default timeout. |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 21 | There is deliberately no support for an infinite timeout. |
| 22 | TODO: get the default timeout from configuration |
| 23 | """ |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 24 | def timed_wait(cv, fn, timeout=-1): |
| 25 | if timeout == -1: |
| 26 | # TODO make this configurable |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 27 | timeout = default_timeout |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 28 | |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 29 | end_time = time.time() + timeout |
| 30 | while True: |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 31 | val = fn() |
| 32 | if val != None: |
| 33 | return val |
| 34 | |
| 35 | remaining_time = end_time - time.time() |
| 36 | cv.wait(remaining_time) |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 37 | |
| 38 | if time.time() > end_time: |
| 39 | return None |
Rich Lane | 4dfd5e1 | 2012-12-22 19:48:01 -0800 | [diff] [blame] | 40 | |
| 41 | class EventDescriptor(): |
| 42 | """ |
| 43 | Similar to a condition variable, but can be passed to select(). |
| 44 | Only supports one waiter. |
| 45 | """ |
| 46 | |
| 47 | def __init__(self): |
| 48 | self.pipe_rd, self.pipe_wr = os.pipe() |
Rich Lane | 7e1d5c5 | 2012-12-24 14:08:32 -0800 | [diff] [blame] | 49 | fcntl.fcntl(self.pipe_wr, fcntl.F_SETFL, os.O_NONBLOCK) |
Rich Lane | 4dfd5e1 | 2012-12-22 19:48:01 -0800 | [diff] [blame] | 50 | |
| 51 | def __del__(self): |
| 52 | os.close(self.pipe_rd) |
| 53 | os.close(self.pipe_wr) |
| 54 | |
| 55 | def notify(self): |
Rich Lane | 7e1d5c5 | 2012-12-24 14:08:32 -0800 | [diff] [blame] | 56 | try: |
| 57 | os.write(self.pipe_wr, "x") |
| 58 | except OSError as e: |
Rich Lane | 2e37ec2 | 2012-12-26 09:47:39 -0800 | [diff] [blame] | 59 | logging.warn("Failed to notify EventDescriptor: %s", e) |
Rich Lane | 4dfd5e1 | 2012-12-22 19:48:01 -0800 | [diff] [blame] | 60 | |
| 61 | def wait(self): |
| 62 | os.read(self.pipe_rd, 1) |
| 63 | |
| 64 | def fileno(self): |
| 65 | return self.pipe_rd |