Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet committed Feb 4, 2025
1 parent 011a2b6 commit 9dd5635
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/helpers/test_debounce.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import timedelta
import logging
from unittest.mock import AsyncMock, Mock
import weakref

import pytest

Expand Down Expand Up @@ -529,3 +530,37 @@ async def _func() -> None:
async_fire_time_changed(hass, utcnow() + timedelta(seconds=1))
await hass.async_block_till_done(wait_background_tasks=False)
assert len(calls) == 2


async def test_shutdown_releases_parent_class(hass: HomeAssistant) -> None:
"""Test shutdown releases parent class.
See https://github.com/home-assistant/core/issues/137237
"""
calls = []

class SomeClass:
def run_func(self) -> None:
calls.append(None)

my_class = SomeClass()
my_class_weak_ref = weakref.ref(my_class)

debouncer = debounce.Debouncer(
hass,
_LOGGER,
cooldown=0.01,
immediate=True,
function=my_class.run_func,
)

# Debouncer keeps a reference to the function, prevening GC
del my_class
await debouncer.async_call()
await hass.async_block_till_done()
assert len(calls) == 1
assert my_class_weak_ref() is not None

# Debouncer shutdown releases the class
debouncer.async_shutdown()
assert my_class_weak_ref() is None

0 comments on commit 9dd5635

Please sign in to comment.