-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix: clean up duplicate platform adapters on reload #6102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stablegenius49
wants to merge
1
commit into
AstrBotDevs:master
Choose a base branch
from
stablegenius49:fix/6100-platform-reload-cleanup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| from astrbot.core.platform.manager import PlatformManager | ||
| from astrbot.core.platform.platform import Platform | ||
| from astrbot.core.platform.platform_metadata import PlatformMetadata | ||
| from astrbot.core.platform.register import platform_cls_map | ||
|
|
||
|
|
||
| class DummyAstrBotConfig(dict): | ||
| def save_config(self, replace_config: dict | None = None) -> None: | ||
| if replace_config is not None: | ||
| self.clear() | ||
| self.update(replace_config) | ||
|
|
||
|
|
||
| class DummyPlatform(Platform): | ||
| instances: list["DummyPlatform"] = [] | ||
|
|
||
| def __init__(self, platform_config: dict, platform_settings: dict, event_queue): | ||
| super().__init__(platform_config, event_queue) | ||
| self.platform_settings = platform_settings | ||
| self.terminated = False | ||
| self._stop_event = asyncio.Event() | ||
| self.__class__.instances.append(self) | ||
|
|
||
| async def _run(self) -> None: | ||
| await self._stop_event.wait() | ||
|
|
||
| def run(self): | ||
| return self._run() | ||
|
|
||
| async def terminate(self) -> None: | ||
| self.terminated = True | ||
| self._stop_event.set() | ||
|
|
||
| def meta(self) -> PlatformMetadata: | ||
| return PlatformMetadata( | ||
| name="dummy", | ||
| description="dummy platform", | ||
| id=self.config["id"], | ||
| support_proactive_message=False, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def manager(monkeypatch: pytest.MonkeyPatch) -> PlatformManager: | ||
| DummyPlatform.instances.clear() | ||
| monkeypatch.setitem(platform_cls_map, "dummy", DummyPlatform) | ||
| config = DummyAstrBotConfig({"platform": [], "platform_settings": {}}) | ||
| return PlatformManager(config, asyncio.Queue()) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_load_platform_replaces_existing_same_id(manager: PlatformManager): | ||
| config = {"id": "default", "type": "dummy", "enable": True} | ||
|
|
||
| await manager.load_platform(config.copy()) | ||
| first_inst = DummyPlatform.instances[-1] | ||
|
|
||
| await manager.load_platform(config.copy()) | ||
| second_inst = DummyPlatform.instances[-1] | ||
|
|
||
| assert first_inst is not second_inst | ||
| assert first_inst.terminated is True | ||
| assert second_inst.terminated is False | ||
| assert manager._inst_map["default"]["inst"] is second_inst | ||
| assert manager.platform_insts == [second_inst] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_terminate_platform_cleans_orphaned_instances(manager: PlatformManager): | ||
| config = {"id": "default", "type": "dummy", "enable": True} | ||
|
|
||
| await manager.load_platform(config.copy()) | ||
| tracked_inst = DummyPlatform.instances[-1] | ||
|
|
||
| orphan_inst = DummyPlatform(config.copy(), {}, asyncio.Queue()) | ||
| manager.platform_insts.append(orphan_inst) | ||
| manager._start_platform_task("orphan_default", orphan_inst) | ||
|
|
||
| await manager.terminate_platform("default") | ||
|
|
||
| assert tracked_inst.terminated is True | ||
| assert orphan_inst.terminated is True | ||
| assert manager.platform_insts == [] | ||
| assert "default" not in manager._inst_map |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
terminate_platformmethod can be refactored for better performance and readability.self.platform_insts, rather than usingremove()in a loop.asyncio.gatherwill improve performance, as these are independent I/O-bound operations.Applying these changes will make the function more robust and faster.