-
Notifications
You must be signed in to change notification settings - Fork 0
Add register module in lazy manner #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
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,172 @@ | ||
| """ | ||
| MIT License | ||
| Copyright (c) 2025 Disguise Technologies ltd | ||
| """ | ||
|
|
||
| import asyncio | ||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| from designer_plugin.d3sdk.function import d3function | ||
| from designer_plugin.d3sdk.session import D3AsyncSession, D3Session | ||
| from designer_plugin.models import PluginPayload, PluginResponse, PluginStatus | ||
|
|
||
|
|
||
| # Register a module so D3Function._available_d3functions knows about it. | ||
| @d3function("lazy_test_module") | ||
| def _lazy_test_fn() -> str: | ||
| return "hello world" | ||
|
|
||
|
|
||
| def _make_response() -> PluginResponse: | ||
| return PluginResponse( | ||
| status=PluginStatus(code=0, message="OK", details=[]), | ||
| returnValue=None, | ||
| ) | ||
|
|
||
|
|
||
| def _module_payload() -> PluginPayload: | ||
| """Payload that references a registered @d3function module.""" | ||
| return PluginPayload(moduleName="lazy_test_module", script="return _lazy_test_fn()") | ||
|
|
||
|
|
||
| def _script_payload() -> PluginPayload: | ||
| """Payload with no module (equivalent to @d3pythonscript).""" | ||
| return PluginPayload(moduleName=None, script="return 42") | ||
|
|
||
|
|
||
| class TestD3SessionLazyRegistration: | ||
| """Lazy registration behaviour for the synchronous D3Session.""" | ||
|
|
||
| def test_registered_modules_starts_empty(self): | ||
| session = D3Session("localhost", 80) | ||
| assert session.registered_modules == set() | ||
|
|
||
| def test_module_registered_on_first_execute(self): | ||
| session = D3Session("localhost", 80) | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_register_module") as mock_reg, | ||
| patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), | ||
| ): | ||
| session.execute(_module_payload()) | ||
| mock_reg.assert_called_once() | ||
| assert "lazy_test_module" in session.registered_modules | ||
|
|
||
| def test_module_not_re_registered_on_second_execute(self): | ||
| session = D3Session("localhost", 80) | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_register_module") as mock_reg, | ||
| patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), | ||
| ): | ||
| session.execute(_module_payload()) | ||
| session.execute(_module_payload()) | ||
| mock_reg.assert_called_once() | ||
|
|
||
| def test_no_registration_for_script_payload(self): | ||
| """Payloads without a moduleName must never trigger registration.""" | ||
| session = D3Session("localhost", 80) | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_register_module") as mock_reg, | ||
| patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), | ||
| ): | ||
| session.execute(_script_payload()) | ||
| mock_reg.assert_not_called() | ||
|
|
||
| def test_context_module_not_re_registered_lazily(self): | ||
| """A module pre-registered via context_modules must not be registered again in execute().""" | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_register_module") as mock_reg, | ||
| patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), | ||
| ): | ||
| with D3Session("localhost", 80, {"lazy_test_module"}) as session: | ||
| assert "lazy_test_module" in session.registered_modules | ||
| session.execute(_module_payload()) | ||
| mock_reg.assert_called_once() # only from __enter__, not from execute() | ||
|
|
||
| def test_registered_modules_updated_after_execute(self): | ||
| session = D3Session("localhost", 80) | ||
| assert "lazy_test_module" not in session.registered_modules | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_register_module"), | ||
| patch("designer_plugin.d3sdk.session.d3_api_execute", return_value=_make_response()), | ||
| ): | ||
| session.execute(_module_payload()) | ||
| assert "lazy_test_module" in session.registered_modules | ||
|
|
||
|
|
||
| class TestD3AsyncSessionLazyRegistration: | ||
| """Lazy registration behaviour for the asynchronous D3AsyncSession.""" | ||
|
|
||
| def test_registered_modules_starts_empty(self): | ||
| session = D3AsyncSession("localhost", 80) | ||
| assert session.registered_modules == set() | ||
|
|
||
| def test_module_registered_on_first_execute(self): | ||
| async def run(): | ||
| session = D3AsyncSession("localhost", 80) | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock) as mock_reg, | ||
| patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, | ||
| ): | ||
| mock_exec.return_value = _make_response() | ||
| await session.execute(_module_payload()) | ||
| mock_reg.assert_called_once() | ||
| assert "lazy_test_module" in session.registered_modules | ||
|
|
||
| asyncio.run(run()) | ||
|
|
||
| def test_module_not_re_registered_on_second_execute(self): | ||
| async def run(): | ||
| session = D3AsyncSession("localhost", 80) | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock) as mock_reg, | ||
| patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, | ||
| ): | ||
| mock_exec.return_value = _make_response() | ||
| await session.execute(_module_payload()) | ||
| await session.execute(_module_payload()) | ||
| mock_reg.assert_called_once() | ||
|
|
||
| asyncio.run(run()) | ||
|
|
||
| def test_no_registration_for_script_payload(self): | ||
| """Payloads without a moduleName must never trigger registration.""" | ||
| async def run(): | ||
| session = D3AsyncSession("localhost", 80) | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock) as mock_reg, | ||
| patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, | ||
| ): | ||
| mock_exec.return_value = _make_response() | ||
| await session.execute(_script_payload()) | ||
| mock_reg.assert_not_called() | ||
|
|
||
| asyncio.run(run()) | ||
|
|
||
| def test_context_module_not_re_registered_lazily(self): | ||
| """A module pre-registered via context_modules must not be registered again in execute().""" | ||
| async def run(): | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock) as mock_reg, | ||
| patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, | ||
| ): | ||
| mock_exec.return_value = _make_response() | ||
| async with D3AsyncSession("localhost", 80, {"lazy_test_module"}) as session: | ||
| assert "lazy_test_module" in session.registered_modules | ||
| await session.execute(_module_payload()) | ||
| mock_reg.assert_called_once() | ||
|
|
||
| asyncio.run(run()) | ||
|
|
||
| def test_registered_modules_updated_after_execute(self): | ||
| async def run(): | ||
| session = D3AsyncSession("localhost", 80) | ||
| assert "lazy_test_module" not in session.registered_modules | ||
| with ( | ||
| patch("designer_plugin.d3sdk.session.d3_api_aregister_module", new_callable=AsyncMock), | ||
| patch("designer_plugin.d3sdk.session.d3_api_aexecute", new_callable=AsyncMock) as mock_exec, | ||
| ): | ||
| mock_exec.return_value = _make_response() | ||
| await session.execute(_module_payload()) | ||
| assert "lazy_test_module" in session.registered_modules | ||
|
|
||
| asyncio.run(run()) |
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.
we cache
registered_modulesinD3SessionBaseso we can register module on demand in lazy manner.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.
What's the difference between context_modules and registered_modules?
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.
context_modulesis modules that will get registered when entering context.register_moduleis modules that is actually registered by the session.