blob: 9e59c5d74d5dfe40cfa96ae5dc210677c02a9e37 [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
9def gen_xid():
10 return random.randrange(1,0xffffffff)
Rich Laneb64ce3d2012-07-26 15:37:57 -070011
12"""
13Wait on a condition variable until the given function returns non-None or a timeout expires.
14The condition variable must already be acquired.
15There is deliberately no support for an infinite timeout.
16TODO: get the default timeout from configuration
17"""
18def 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)