blob: 219ce4f8b69fa102662156e0591a123e11dc66c1 [file] [log] [blame]
Dan Talaycoea8ad802010-02-22 20:30:18 -08001
2"""
3Utilities for the OpenFlow test framework
4"""
5
6import random
Rich Laneb64ce3d2012-07-26 15:37:57 -07007import time
Dan Talaycoea8ad802010-02-22 20:30:18 -08008
Rich Lanee55abf72012-07-26 20:11:42 -07009default_timeout = None # set by oft
10
Dan Talaycoea8ad802010-02-22 20:30:18 -080011def gen_xid():
12 return random.randrange(1,0xffffffff)
Rich Laneb64ce3d2012-07-26 15:37:57 -070013
14"""
15Wait on a condition variable until the given function returns non-None or a timeout expires.
16The condition variable must already be acquired.
Rich Lane8806bc42012-07-26 19:18:37 -070017The timeout value -1 means use the default timeout.
Rich Laneb64ce3d2012-07-26 15:37:57 -070018There is deliberately no support for an infinite timeout.
19TODO: get the default timeout from configuration
20"""
Rich Lane8806bc42012-07-26 19:18:37 -070021def timed_wait(cv, fn, timeout=-1):
22 if timeout == -1:
23 # TODO make this configurable
Rich Lanee55abf72012-07-26 20:11:42 -070024 timeout = default_timeout
Rich Lane8806bc42012-07-26 19:18:37 -070025
Rich Laneb64ce3d2012-07-26 15:37:57 -070026 end_time = time.time() + timeout
27 while True:
Rich Laneb64ce3d2012-07-26 15:37:57 -070028 val = fn()
29 if val != None:
30 return val
31
32 remaining_time = end_time - time.time()
33 cv.wait(remaining_time)
Rich Lane8806bc42012-07-26 19:18:37 -070034
35 if time.time() > end_time:
36 return None