[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) <noreply@anthropic.com>

* 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) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Rodrigo Toshiaki Horie
2026-03-18 11:21:39 -03:00
committed by GitHub
parent c591eb4a7a
commit 679e48cbe8
3 changed files with 8 additions and 8 deletions

View File

@@ -77,13 +77,13 @@ class CallbackBrokerWorker:
MAX_RETRIES = 2 MAX_RETRIES = 2
INDIVIDUAL_EVENT_RETRIES = 3 INDIVIDUAL_EVENT_RETRIES = 3
last_stats = time.time()
last_flush = time.time()
total = 0 total = 0
last_event = '' last_event = ''
prof = None prof = None
def __init__(self): def __init__(self):
self.last_stats = time.time()
self.last_flush = time.time()
self.buff = {} self.buff = {}
self.redis = get_redis_client() self.redis = get_redis_client()
self.subsystem_metrics = s_metrics.CallbackReceiverMetrics(auto_pipe_execute=False) self.subsystem_metrics = s_metrics.CallbackReceiverMetrics(auto_pipe_execute=False)

View File

@@ -48,7 +48,7 @@ class TestCallbackBrokerWorker(TransactionTestCase):
worker = CallbackBrokerWorker() worker = CallbackBrokerWorker()
events = [InventoryUpdateEvent(uuid=str(uuid4()), **self.event_create_kwargs())] events = [InventoryUpdateEvent(uuid=str(uuid4()), **self.event_create_kwargs())]
worker.buff = {InventoryUpdateEvent: events} worker.buff = {InventoryUpdateEvent: events}
worker.flush() worker.flush(force=True)
assert worker.buff.get(InventoryUpdateEvent, []) == [] assert worker.buff.get(InventoryUpdateEvent, []) == []
assert InventoryUpdateEvent.objects.filter(uuid=events[0].uuid).count() == 1 assert InventoryUpdateEvent.objects.filter(uuid=events[0].uuid).count() == 1
@@ -61,7 +61,7 @@ class TestCallbackBrokerWorker(TransactionTestCase):
InventoryUpdateEvent(uuid=str(uuid4()), stdout='good2', **kwargs), InventoryUpdateEvent(uuid=str(uuid4()), stdout='good2', **kwargs),
] ]
worker.buff = {InventoryUpdateEvent: events.copy()} 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[0].uuid).count() == 1
assert InventoryUpdateEvent.objects.filter(uuid=events[1].uuid).count() == 0 assert InventoryUpdateEvent.objects.filter(uuid=events[1].uuid).count() == 0
assert InventoryUpdateEvent.objects.filter(uuid=events[2].uuid).count() == 1 assert InventoryUpdateEvent.objects.filter(uuid=events[2].uuid).count() == 1
@@ -71,7 +71,7 @@ class TestCallbackBrokerWorker(TransactionTestCase):
worker = CallbackBrokerWorker() worker = CallbackBrokerWorker()
events = [InventoryUpdateEvent(uuid=str(uuid4()), **self.event_create_kwargs())] events = [InventoryUpdateEvent(uuid=str(uuid4()), **self.event_create_kwargs())]
worker.buff = {InventoryUpdateEvent: events.copy()} worker.buff = {InventoryUpdateEvent: events.copy()}
worker.flush() worker.flush(force=True)
# put current saved event in buffer (error case) # put current saved event in buffer (error case)
worker.buff = {InventoryUpdateEvent: [InventoryUpdateEvent.objects.get(uuid=events[0].uuid)]} 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(InventoryUpdateEvent.objects, 'bulk_create', side_effect=ValueError):
with mock.patch.object(events[0], 'save', 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 assert "\x00" not in events[0].stdout

View File

@@ -61,7 +61,7 @@ def separate_async_optionals(creation_order):
continue continue
by_count = defaultdict(set) by_count = defaultdict(set)
has_creates = [cand for cand in group if hasattr(cand, 'dependencies')] 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 has_create in has_creates:
for dependency in has_create.dependencies: for dependency in has_create.dependencies:
for compared in [cand for cand in has_creates if cand != has_create]: 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') dependency_store = kw.get('ds')
if dependency_store is None: if dependency_store is None:
deps = self.dependencies + self.optional_dependencies 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) self.ds = DSAdapter(self.__class__.__name__, self._dependency_store)
else: else:
self._dependency_store = dependency_store.dependency_store self._dependency_store = dependency_store.dependency_store