From 36408936f88be2d8fb739cce5af079c83be1bd1d Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Sun, 5 Jan 2014 08:02:06 +0200 Subject: [PATCH] tests: Optimize wait_event() Replace the fixed 100 ms waits with a select()-based wait and timeout for full wait based on monotonic time to optimize wait_event(). Signed-hostap: Jouni Malinen --- tests/hwsim/hostapd.py | 14 +++++++++----- tests/hwsim/wpasupplicant.py | 24 ++++++++++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/tests/hwsim/hostapd.py b/tests/hwsim/hostapd.py index 94e95d04d..fa1d35612 100644 --- a/tests/hwsim/hostapd.py +++ b/tests/hwsim/hostapd.py @@ -1,7 +1,7 @@ #!/usr/bin/python # # Python class for controlling hostapd -# Copyright (c) 2013, Jouni Malinen +# Copyright (c) 2013-2014, Jouni Malinen # # This software may be distributed under the terms of the BSD license. # See README for more details. @@ -121,16 +121,20 @@ class Hostapd: logger.debug(self.ifname + ": " + ev) def wait_event(self, events, timeout): - count = 0 - while count < timeout * 10: - count = count + 1 - time.sleep(0.1) + start = os.times()[4] + while True: while self.mon.pending(): ev = self.mon.recv() logger.debug(self.ifname + ": " + ev) for event in events: if event in ev: return ev + now = os.times()[4] + remaining = start + timeout - now + if remaining <= 0: + break + if not self.mon.pending(timeout=remaining): + break return None def get_status(self): diff --git a/tests/hwsim/wpasupplicant.py b/tests/hwsim/wpasupplicant.py index 3823ad21f..0fc5c79fd 100644 --- a/tests/hwsim/wpasupplicant.py +++ b/tests/hwsim/wpasupplicant.py @@ -447,32 +447,40 @@ class WpaSupplicant: raise Exception("P2P_CONNECT failed") def wait_event(self, events, timeout=10): - count = 0 - while count < timeout * 10: - count = count + 1 - time.sleep(0.1) + start = os.times()[4] + while True: while self.mon.pending(): ev = self.mon.recv() logger.debug(self.ifname + ": " + ev) for event in events: if event in ev: return ev + now = os.times()[4] + remaining = start + timeout - now + if remaining <= 0: + break + if not self.mon.pending(timeout=remaining): + break return None def wait_global_event(self, events, timeout): if self.global_iface is None: self.wait_event(events, timeout) else: - count = 0 - while count < timeout * 10: - count = count + 1 - time.sleep(0.1) + start = os.times()[4] + while True: while self.global_mon.pending(): ev = self.global_mon.recv() logger.debug(self.ifname + "(global): " + ev) for event in events: if event in ev: return ev + now = os.times()[4] + remaining = start + timeout - now + if remaining <= 0: + break + if not self.global_mon.pending(timeout=remaining): + break return None def wait_go_ending_session(self):