Skip to content

Commit

Permalink
refactor: refine addon mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Jan 2, 2025
1 parent 92d4408 commit 398ab54
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/include_internal/ten_runtime/addon/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ten_addon_type_from_string(const char *addon_type_str);
TEN_RUNTIME_PRIVATE_API ten_addon_t *ten_addon_unregister(
ten_addon_store_t *store, const char *addon_name);

TEN_RUNTIME_PRIVATE_API void ten_addon_unregister_all_and_cleanup(void);
TEN_RUNTIME_API void ten_unregister_all_addons_and_cleanup(void);

TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_addon_get_store(void);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ typedef struct ten_py_addon_manager_register_addon_decorator_t {
TEN_RUNTIME_PRIVATE_API PyObject *
ten_py_addon_manager_register_addon_as_extension(PyObject *self,
PyObject *args);

TEN_RUNTIME_PRIVATE_API PyObject *ten_py_unregister_all_addons_and_cleanup(
PyObject *self, PyObject *args);
2 changes: 1 addition & 1 deletion core/src/ten_runtime/addon/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ static void ten_addon_unregister_all(void) {
ten_addon_unregister_all_protocol();
}

void ten_addon_unregister_all_and_cleanup(void) {
void ten_unregister_all_addons_and_cleanup(void) {
// Destroy all addon loaders' singleton to avoid memory leak.
ten_addon_loader_addons_destroy_singleton_instance();

Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_runtime/app/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static void ten_app_unregister_addons_after_app_close(ten_app_t *self) {
return;
}

ten_addon_unregister_all_and_cleanup();
ten_unregister_all_addons_and_cleanup();
}

void ten_app_on_deinit(ten_app_t *self) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from .async_extension import AsyncExtension
from .async_ten_env import AsyncTenEnv
from .addon import Addon
from .addon_manager import register_addon_as_extension, _AddonManager
from .addon_manager import (
register_addon_as_extension,
unregister_all_addons_and_cleanup,
_AddonManager,
)
from .ten_env import TenEnv
from .cmd import Cmd
from .cmd_result import CmdResult, StatusCode
Expand All @@ -26,6 +30,7 @@
"Addon",
"_AddonManager",
"register_addon_as_extension",
"unregister_all_addons_and_cleanup",
"App",
"Extension",
"AsyncExtension",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
from typing import Callable, Dict, Type, Optional

from .addon import Addon
from libten_runtime_python import _register_addon_as_extension
from libten_runtime_python import (
_register_addon_as_extension,
_unregister_all_addons_and_cleanup,
)


class _AddonManager:
Expand Down Expand Up @@ -183,3 +186,7 @@ def register_handler(register_ctx):
return cls

return decorator


def unregister_all_addons_and_cleanup() -> None:
_unregister_all_addons_and_cleanup()
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,4 @@ class _ExtensionTester:
def _register_addon_as_extension(
name: str, base_dir: Optional[str], instance: Addon, register_ctx: object
): ...
def _unregister_all_addons_and_cleanup() -> None: ...
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ PyObject *ten_py_addon_manager_register_addon_as_extension(PyObject *self,

Py_RETURN_NONE;
}

PyObject *ten_py_unregister_all_addons_and_cleanup(PyObject *self,
PyObject *args) {
ten_unregister_all_addons_and_cleanup();

Py_RETURN_NONE;
}
3 changes: 3 additions & 0 deletions core/src/ten_runtime/binding/python/native/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ static PyModuleDef *ten_py_runtime_module(void) {
{"_register_addon_as_extension",
ten_py_addon_manager_register_addon_as_extension, METH_VARARGS,
"Register an addon as an extension"},
{"_unregister_all_addons_and_cleanup",
ten_py_unregister_all_addons_and_cleanup, METH_VARARGS,
"Unregister all addons and cleanup"},
{NULL, NULL, 0, NULL}};

static struct PyModuleDef module_def = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import pytest
import sys
import os
from ten import (
unregister_all_addons_and_cleanup,
)


@pytest.fixture(scope="session", autouse=True)
Expand All @@ -30,4 +33,4 @@ def global_setup_and_teardown():
yield

# Teardown part.
# =-=-=
unregister_all_addons_and_cleanup()
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Copyright © 2025 Agora
# This file is part of TEN Framework, an open source project.
# Licensed under the Apache License, Version 2.0, with certain conditions.
# Refer to the "LICENSE" file in the root directory for more information.
#
import pytest
import sys
import os
from ten import (
unregister_all_addons_and_cleanup,
)


@pytest.fixture(scope="session", autouse=True)
def global_setup_and_teardown():
# Set the environment variable.
os.environ["TEN_DISABLE_ADDON_UNREGISTER_AFTER_APP_CLOSE"] = "true"

# Verify the environment variable is correctly set.
if (
"TEN_DISABLE_ADDON_UNREGISTER_AFTER_APP_CLOSE" not in os.environ
or os.environ["TEN_DISABLE_ADDON_UNREGISTER_AFTER_APP_CLOSE"] != "true"
):
print(
"Failed to set TEN_DISABLE_ADDON_UNREGISTER_AFTER_APP_CLOSE",
file=sys.stderr,
)
sys.exit(1)

# Yield control to the test; after the test execution is complete, continue
# with the teardown process.
yield

# Teardown part.
unregister_all_addons_and_cleanup()
2 changes: 1 addition & 1 deletion tests/ten_runtime/smoke/gtest_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class GlobalTestEnvironment : public ::testing::Environment {
}

// This method is run after all test cases.
void TearDown() override { ten_addon_unregister_all_and_cleanup(); }
void TearDown() override { ten_unregister_all_addons_and_cleanup(); }
};

GTEST_API_ int main(int argc, char **argv) {
Expand Down

0 comments on commit 398ab54

Please sign in to comment.