diff mbox series

tests: wpasupplicant: refactor code duplication in wait_global_event()

Message ID 20190201203159.21457-1-johannes@sipsolutions.net
State Accepted
Headers show
Series tests: wpasupplicant: refactor code duplication in wait_global_event() | expand

Commit Message

Johannes Berg Feb. 1, 2019, 8:31 p.m. UTC
From: Johannes Berg <johannes.berg@intel.com>

This code is identical to the wait_event() code, except for the
mon/global_mon instance. Create a _wait_event() function that
encapsulates this, and use it for both.

While at it, fix the bug in wait_global_event() where in the case
of not having a global_mon it always returns None.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
 tests/hwsim/wpasupplicant.py | 33 +++++++++++----------------------
 1 file changed, 11 insertions(+), 22 deletions(-)

Comments

Jouni Malinen Feb. 1, 2019, 11:01 p.m. UTC | #1
On Fri, Feb 01, 2019 at 09:31:59PM +0100, Johannes Berg wrote:
> This code is identical to the wait_event() code, except for the
> mon/global_mon instance. Create a _wait_event() function that
> encapsulates this, and use it for both.
> 
> While at it, fix the bug in wait_global_event() where in the case
> of not having a global_mon it always returns None.

Thanks, applied.
diff mbox series

Patch

diff --git a/tests/hwsim/wpasupplicant.py b/tests/hwsim/wpasupplicant.py
index fed9a5eff398..0cc6ea8c946a 100644
--- a/tests/hwsim/wpasupplicant.py
+++ b/tests/hwsim/wpasupplicant.py
@@ -764,12 +764,12 @@  class WpaSupplicant:
             return self.group_form_result(ev, expect_failure, go_neg_res)
         raise Exception("P2P_CONNECT failed")
 
-    def wait_event(self, events, timeout=10):
+    def _wait_event(self, mon, pfx, events, timeout):
         start = os.times()[4]
         while True:
-            while self.mon.pending():
-                ev = self.mon.recv()
-                logger.debug(self.dbg + ": " + ev)
+            while mon.pending():
+                ev = mon.recv()
+                logger.debug(self.dbg + pfx + ev)
                 for event in events:
                     if event in ev:
                         return ev
@@ -777,29 +777,18 @@  class WpaSupplicant:
             remaining = start + timeout - now
             if remaining <= 0:
                 break
-            if not self.mon.pending(timeout=remaining):
+            if not mon.pending(timeout=remaining):
                 break
         return None
 
+    def wait_event(self, events, timeout=10):
+        return self._wait_event(self.mon, ": ", events, timeout)
+
     def wait_global_event(self, events, timeout):
         if self.global_iface is None:
-            self.wait_event(events, timeout)
-        else:
-            start = os.times()[4]
-            while True:
-                while self.global_mon.pending():
-                    ev = self.global_mon.recv()
-                    logger.debug(self.global_dbg + 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
+            return self.wait_event(events, timeout)
+        return self._wait_event(self.global_mon, "(global): ",
+                                events, timeout)
 
     def wait_group_event(self, events, timeout=10):
         if self.group_ifname and self.group_ifname != self.ifname: