mirror of
https://github.com/Second-Hand-Friends/kleinanzeigen-bot.git
synced 2026-03-13 10:51:51 +01:00
fix: logging file handler not closed on bot shutdown. Fixes #405
This commit is contained in:
@@ -60,6 +60,7 @@ class KleinanzeigenBot(WebScrapingMixin):
|
|||||||
def __del__(self) -> None:
|
def __del__(self) -> None:
|
||||||
if self.file_log:
|
if self.file_log:
|
||||||
LOG_ROOT.removeHandler(self.file_log)
|
LOG_ROOT.removeHandler(self.file_log)
|
||||||
|
self.file_log.close()
|
||||||
self.close_browser_session()
|
self.close_browser_session()
|
||||||
|
|
||||||
def get_version(self) -> str:
|
def get_version(self) -> str:
|
||||||
@@ -707,7 +708,7 @@ class KleinanzeigenBot(WebScrapingMixin):
|
|||||||
await self.web_sleep(1) # Wait for city dropdown to populate
|
await self.web_sleep(1) # Wait for city dropdown to populate
|
||||||
options = await self.web_find_all(By.CSS_SELECTOR, "#pstad-citychsr option")
|
options = await self.web_find_all(By.CSS_SELECTOR, "#pstad-citychsr option")
|
||||||
for option in options:
|
for option in options:
|
||||||
option_text = await self.web_text(By.CSS_SELECTOR, "option", parent=option)
|
option_text = await self.web_text(By.CSS_SELECTOR, "option", parent = option)
|
||||||
if option_text == ad_cfg["contact"]["location"]:
|
if option_text == ad_cfg["contact"]["location"]:
|
||||||
await self.web_select(By.ID, "pstad-citychsr", option_text)
|
await self.web_select(By.ID, "pstad-citychsr", option_text)
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -3,11 +3,12 @@ SPDX-FileCopyrightText: © Sebastian Thomschke and contributors
|
|||||||
SPDX-License-Identifier: AGPL-3.0-or-later
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
SPDX-ArtifactOfProjectHomePage: https://github.com/Second-Hand-Friends/kleinanzeigen-bot/
|
SPDX-ArtifactOfProjectHomePage: https://github.com/Second-Hand-Friends/kleinanzeigen-bot/
|
||||||
"""
|
"""
|
||||||
import pytest
|
import gc, pytest
|
||||||
from kleinanzeigen_bot import KleinanzeigenBot
|
from kleinanzeigen_bot import KleinanzeigenBot
|
||||||
|
|
||||||
|
|
||||||
class TestKleinanzeigenBot:
|
class TestKleinanzeigenBot:
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def bot(self) -> KleinanzeigenBot:
|
def bot(self) -> KleinanzeigenBot:
|
||||||
return KleinanzeigenBot()
|
return KleinanzeigenBot()
|
||||||
@@ -31,3 +32,19 @@ class TestKleinanzeigenBot:
|
|||||||
version = bot.get_version()
|
version = bot.get_version()
|
||||||
assert isinstance(version, str)
|
assert isinstance(version, str)
|
||||||
assert len(version) > 0
|
assert len(version) > 0
|
||||||
|
|
||||||
|
def test_file_log_closed_after_bot_shutdown(self) -> None:
|
||||||
|
"""Ensure the file log handler is properly closed after the bot is deleted"""
|
||||||
|
|
||||||
|
# Directly instantiate the bot to control its lifecycle within the test
|
||||||
|
bot = KleinanzeigenBot()
|
||||||
|
|
||||||
|
bot.configure_file_logging()
|
||||||
|
file_log = bot.file_log
|
||||||
|
assert not file_log.stream.closed # type: ignore[union-attr]
|
||||||
|
|
||||||
|
# Delete and garbage collect the bot instance to ensure the destructor (__del__) is called
|
||||||
|
del bot
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
|
assert file_log.stream is None # type: ignore[union-attr]
|
||||||
|
|||||||
Reference in New Issue
Block a user