From 973ee269d33be50adbd8dbf633bfda260d927a8a Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Fri, 13 Aug 2021 15:45:51 -0700 Subject: [PATCH] auto-t: return existing instance from HostapdCLI This addresses the TODO where HostapdCLI was creating separate objects each time HostapdCLI was called. This was worked around by manually setting the important members but instead the class can be re-worked to act as somewhat of a singleton, per-config at least. If there is no HostapdCLI instance for a given config one is created and initialized. Subsequent HostapdCLI calls (for the same config) will be returned the same object rather than a new one. --- autotests/util/hostapd.py | 48 +++++++++++++++++++++++---------------- tools/test-runner | 4 ++++ 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/autotests/util/hostapd.py b/autotests/util/hostapd.py index a61b2d39..aabc997b 100644 --- a/autotests/util/hostapd.py +++ b/autotests/util/hostapd.py @@ -28,10 +28,28 @@ chan_freq_map = [ ctrl_count = 0 mainloop = GLib.MainLoop() -class HostapdCLI: - def _init_hostapd(self, config=None): +class HostapdCLI(object): + _instances = {} + + def __new__(cls, config=None, *args, **kwargs): + hapd = ctx.hostapd[config] + + if not config: + config = hapd.config + + if not config in cls._instances.keys(): + cls._instances[config] = object.__new__(cls, *args, **kwargs) + cls._instances[config]._initialized = False + + return cls._instances[config] + + def _init_hostapd(self, config): global ctrl_count - interface = None + + if self._initialized: + return + + self._initialized = True self.ctrl_sock = None if not ctx.hostapd: @@ -45,21 +63,6 @@ class HostapdCLI: if not hasattr(self, '_hostapd_restarted'): self._hostapd_restarted = False - # - # TODO: In theory some type of instance singleton could be created for - # HostapdCLI where test-runner initializes but any subsequent - # call to HostapdCLI (with the same config) returns the same - # object that test-runner has. This would avoid setting these - # variables. - # - if hapd.cli: - self.ifname = hapd.cli.ifname - self.ctrl_sock = hapd.cli.ctrl_sock - self.cmdline = hapd.cli.cmdline - self.interface = hapd.intf - self.config = hapd.config - return - self.interface = hapd.intf self.config = hapd.config @@ -83,7 +86,7 @@ class HostapdCLI: ctrl_count = ctrl_count + 1 - def __init__(self, config=None): + def __init__(self, config=None, *args, **kwargs): self._init_hostapd(config) def _poll_event(self, event): @@ -137,6 +140,13 @@ class HostapdCLI: def __del__(self): self._del_hostapd() + HostapdCLI._instances[self.config] = None + + # Check if this is the final instance + destroy = len([hapd for hapd in HostapdCLI._instances.values() if hapd is not None]) == 0 + if destroy: + HostapdCLI._instances = {} + def set_value(self, key, value): cmd = self.cmdline + ['set', key, value] ctx.start_process(cmd, wait=True) diff --git a/tools/test-runner b/tools/test-runner index 22eb191d..a7eaf5b6 100755 --- a/tools/test-runner +++ b/tools/test-runner @@ -545,6 +545,10 @@ class Hostapd: except: print("Failed to remove %s" % self.global_ctrl_iface) + for hapd in self.instances: + hapd.cli._instances[hapd.config] = None + hapd.cli = None + self.instances = None # Hostapd may have already been stopped