From 679e48cbe84da71f0b1f2bce89a960f3e0ebe78d Mon Sep 17 00:00:00 2001 From: Rodrigo Toshiaki Horie Date: Wed, 18 Mar 2026 11:21:39 -0300 Subject: [PATCH] [AAP-68258] Fix SonarCloud Reliability issues (#16354) * Fix SonarCloud Reliability issues: time-dependent class attrs and dict comprehensions - Move last_stats/last_flush from class body to __init__ in CallbackBrokerWorker (S8434: time-dependent expressions evaluated at class definition) - Replace dict comprehensions with dict.fromkeys() in has_create.py (S7519: constant-value dict should use fromkeys) Co-Authored-By: Claude Opus 4.6 (1M context) * Fix callback receiver tests to use flush(force=True) Tests were implicitly relying on last_flush being a stale class-level timestamp. Now that last_flush is set in __init__, the time-based flush condition isn't met when flush() is called immediately after construction. Use force=True to explicitly trigger an immediate flush in tests. Co-Authored-By: Claude Opus 4.6 (1M context) --------- Co-authored-by: Claude Opus 4.6 (1M context) --- awx/main/dispatch/worker/callback.py | 4 ++-- .../tests/functional/commands/test_callback_receiver.py | 8 ++++---- awxkit/awxkit/api/mixins/has_create.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/awx/main/dispatch/worker/callback.py b/awx/main/dispatch/worker/callback.py index b47b648086..5673bbb9b9 100644 --- a/awx/main/dispatch/worker/callback.py +++ b/awx/main/dispatch/worker/callback.py @@ -77,13 +77,13 @@ class CallbackBrokerWorker: MAX_RETRIES = 2 INDIVIDUAL_EVENT_RETRIES = 3 - last_stats = time.time() - last_flush = time.time() total = 0 last_event = '' prof = None def __init__(self): + self.last_stats = time.time() + self.last_flush = time.time() self.buff = {} self.redis = get_redis_client() self.subsystem_metrics = s_metrics.CallbackReceiverMetrics(auto_pipe_execute=False) diff --git a/awx/main/tests/functional/commands/test_callback_receiver.py b/awx/main/tests/functional/commands/test_callback_receiver.py index 145c48d605..f585864cb2 100644 --- a/awx/main/tests/functional/commands/test_callback_receiver.py +++ b/awx/main/tests/functional/commands/test_callback_receiver.py @@ -48,7 +48,7 @@ class TestCallbackBrokerWorker(TransactionTestCase): worker = CallbackBrokerWorker() events = [InventoryUpdateEvent(uuid=str(uuid4()), **self.event_create_kwargs())] worker.buff = {InventoryUpdateEvent: events} - worker.flush() + worker.flush(force=True) assert worker.buff.get(InventoryUpdateEvent, []) == [] assert InventoryUpdateEvent.objects.filter(uuid=events[0].uuid).count() == 1 @@ -61,7 +61,7 @@ class TestCallbackBrokerWorker(TransactionTestCase): InventoryUpdateEvent(uuid=str(uuid4()), stdout='good2', **kwargs), ] worker.buff = {InventoryUpdateEvent: events.copy()} - worker.flush() + worker.flush(force=True) assert InventoryUpdateEvent.objects.filter(uuid=events[0].uuid).count() == 1 assert InventoryUpdateEvent.objects.filter(uuid=events[1].uuid).count() == 0 assert InventoryUpdateEvent.objects.filter(uuid=events[2].uuid).count() == 1 @@ -71,7 +71,7 @@ class TestCallbackBrokerWorker(TransactionTestCase): worker = CallbackBrokerWorker() events = [InventoryUpdateEvent(uuid=str(uuid4()), **self.event_create_kwargs())] worker.buff = {InventoryUpdateEvent: events.copy()} - worker.flush() + worker.flush(force=True) # put current saved event in buffer (error case) worker.buff = {InventoryUpdateEvent: [InventoryUpdateEvent.objects.get(uuid=events[0].uuid)]} @@ -113,7 +113,7 @@ class TestCallbackBrokerWorker(TransactionTestCase): with mock.patch.object(InventoryUpdateEvent.objects, 'bulk_create', side_effect=ValueError): with mock.patch.object(events[0], 'save', side_effect=ValueError): - worker.flush() + worker.flush(force=True) assert "\x00" not in events[0].stdout diff --git a/awxkit/awxkit/api/mixins/has_create.py b/awxkit/awxkit/api/mixins/has_create.py index 21f37fe7a2..909a36b50e 100644 --- a/awxkit/awxkit/api/mixins/has_create.py +++ b/awxkit/awxkit/api/mixins/has_create.py @@ -61,7 +61,7 @@ def separate_async_optionals(creation_order): continue by_count = defaultdict(set) has_creates = [cand for cand in group if hasattr(cand, 'dependencies')] - counts = {has_create: 0 for has_create in has_creates} + counts = dict.fromkeys(has_creates, 0) for has_create in has_creates: for dependency in has_create.dependencies: for compared in [cand for cand in has_creates if cand != has_create]: @@ -212,7 +212,7 @@ class HasCreate(object): dependency_store = kw.get('ds') if dependency_store is None: deps = self.dependencies + self.optional_dependencies - self._dependency_store = {base_subclass: None for base_subclass in deps} + self._dependency_store = dict.fromkeys(deps) self.ds = DSAdapter(self.__class__.__name__, self._dependency_store) else: self._dependency_store = dependency_store.dependency_store