Dan Talayco | ea8ad80 | 2010-02-22 20:30:18 -0800 | [diff] [blame] | 1 | |
Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | |
Dan Talayco | ea8ad80 | 2010-02-22 20:30:18 -0800 | [diff] [blame] | 18 | """ |
| 19 | Utilities for the OpenFlow test framework |
| 20 | """ |
| 21 | |
| 22 | import random |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 23 | import time |
Rich Lane | 4dfd5e1 | 2012-12-22 19:48:01 -0800 | [diff] [blame] | 24 | import os |
Rich Lane | 7e1d5c5 | 2012-12-24 14:08:32 -0800 | [diff] [blame] | 25 | import fcntl |
Rich Lane | 2e37ec2 | 2012-12-26 09:47:39 -0800 | [diff] [blame] | 26 | import logging |
Dan Talayco | ea8ad80 | 2010-02-22 20:30:18 -0800 | [diff] [blame] | 27 | |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 28 | default_timeout = None # set by oft |
Rich Lane | 48f6aed | 2014-03-23 15:51:02 -0700 | [diff] [blame] | 29 | default_negative_timeout = None # set by oft |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 30 | |
Dan Talayco | ea8ad80 | 2010-02-22 20:30:18 -0800 | [diff] [blame] | 31 | def gen_xid(): |
| 32 | return random.randrange(1,0xffffffff) |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 33 | |
| 34 | """ |
| 35 | Wait on a condition variable until the given function returns non-None or a timeout expires. |
| 36 | The condition variable must already be acquired. |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 37 | The timeout value -1 means use the default timeout. |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 38 | There is deliberately no support for an infinite timeout. |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 39 | """ |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 40 | def timed_wait(cv, fn, timeout=-1): |
| 41 | if timeout == -1: |
Rich Lane | e55abf7 | 2012-07-26 20:11:42 -0700 | [diff] [blame] | 42 | timeout = default_timeout |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 43 | |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 44 | end_time = time.time() + timeout |
| 45 | while True: |
Rich Lane | b64ce3d | 2012-07-26 15:37:57 -0700 | [diff] [blame] | 46 | val = fn() |
| 47 | if val != None: |
| 48 | return val |
| 49 | |
| 50 | remaining_time = end_time - time.time() |
| 51 | cv.wait(remaining_time) |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 52 | |
| 53 | if time.time() > end_time: |
| 54 | return None |
Rich Lane | 4dfd5e1 | 2012-12-22 19:48:01 -0800 | [diff] [blame] | 55 | |
| 56 | class EventDescriptor(): |
| 57 | """ |
| 58 | Similar to a condition variable, but can be passed to select(). |
| 59 | Only supports one waiter. |
| 60 | """ |
| 61 | |
| 62 | def __init__(self): |
| 63 | self.pipe_rd, self.pipe_wr = os.pipe() |
Rich Lane | 7e1d5c5 | 2012-12-24 14:08:32 -0800 | [diff] [blame] | 64 | fcntl.fcntl(self.pipe_wr, fcntl.F_SETFL, os.O_NONBLOCK) |
Rich Lane | 4dfd5e1 | 2012-12-22 19:48:01 -0800 | [diff] [blame] | 65 | |
| 66 | def __del__(self): |
| 67 | os.close(self.pipe_rd) |
| 68 | os.close(self.pipe_wr) |
| 69 | |
| 70 | def notify(self): |
Rich Lane | 7e1d5c5 | 2012-12-24 14:08:32 -0800 | [diff] [blame] | 71 | try: |
| 72 | os.write(self.pipe_wr, "x") |
| 73 | except OSError as e: |
Rich Lane | 2e37ec2 | 2012-12-26 09:47:39 -0800 | [diff] [blame] | 74 | logging.warn("Failed to notify EventDescriptor: %s", e) |
Rich Lane | 4dfd5e1 | 2012-12-22 19:48:01 -0800 | [diff] [blame] | 75 | |
| 76 | def wait(self): |
| 77 | os.read(self.pipe_rd, 1) |
| 78 | |
| 79 | def fileno(self): |
| 80 | return self.pipe_rd |