Skip to content

Commit

Permalink
feat: add log api in Python tester (#553)
Browse files Browse the repository at this point in the history
Co-authored-by: Hu Yueh-Wei <[email protected]>
  • Loading branch information
sunxilin and halajohn committed Jan 14, 2025
1 parent 5013eaa commit cf75ae3
Show file tree
Hide file tree
Showing 23 changed files with 754 additions and 66 deletions.
8 changes: 6 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@
"environment": [
{
"name": "PYTHONPATH",
"value": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime_python/lib:${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime_python/interface"
"value": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime_python/lib:${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime_python/interface:${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app"
},
{
"name": "LD_PRELOAD",
Expand Down Expand Up @@ -840,7 +840,11 @@
"name": "Python Debugger: Python File",
"type": "debugpy",
"request": "launch",
"program": "${file}"
"program": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/tests/test_async_outer_thread.py",
"env": {
"PYTHONPATH": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime_python/lib:${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime_python/interface:${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app",
"LD_PRELOAD": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime/lib/libasan.so"
}
}
],
"compounds": [
Expand Down
40 changes: 20 additions & 20 deletions core/include/ten_runtime/binding/cpp/detail/ten_env_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ using notify_std_func_t = std::function<void(ten_env_t &)>;
using notify_std_with_user_data_func_t =
std::function<void(ten_env_t &, void *user_data)>;

struct proxy_notify_info_t {
explicit proxy_notify_info_t(notify_std_func_t &&func)
struct proxy_notify_ctx_t {
explicit proxy_notify_ctx_t(notify_std_func_t &&func)
: notify_std_func(std::move(func)) {}
explicit proxy_notify_info_t(notify_std_with_user_data_func_t &&func,
void *user_data)
explicit proxy_notify_ctx_t(notify_std_with_user_data_func_t &&func,
void *user_data)
: notify_std_with_user_data_func(std::move(func)), user_data(user_data) {}

~proxy_notify_info_t() = default;
~proxy_notify_ctx_t() = default;

// @{
proxy_notify_info_t(const proxy_notify_info_t &) = delete;
proxy_notify_info_t &operator=(const proxy_notify_info_t &) = delete;
proxy_notify_info_t(proxy_notify_info_t &&) = delete;
proxy_notify_info_t &operator=(proxy_notify_info_t &&) = delete;
proxy_notify_ctx_t(const proxy_notify_ctx_t &) = delete;
proxy_notify_ctx_t &operator=(const proxy_notify_ctx_t &) = delete;
proxy_notify_ctx_t(proxy_notify_ctx_t &&) = delete;
proxy_notify_ctx_t &operator=(proxy_notify_ctx_t &&) = delete;
// @}

notify_std_func_t notify_std_func;
Expand All @@ -50,20 +50,20 @@ struct proxy_notify_info_t {
inline void proxy_notify(::ten_env_t *ten_env, void *data = nullptr) {
TEN_ASSERT(data, "Invalid argument.");

auto *info = static_cast<proxy_notify_info_t *>(data);
auto *ctx = static_cast<proxy_notify_ctx_t *>(data);
auto *cpp_ten_env =
static_cast<ten_env_t *>(ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(ten_env)));

if (info->notify_std_func != nullptr) {
auto func = info->notify_std_func;
if (ctx->notify_std_func != nullptr) {
auto func = ctx->notify_std_func;
func(*cpp_ten_env);
} else if (info->notify_std_with_user_data_func != nullptr) {
auto func = info->notify_std_with_user_data_func;
func(*cpp_ten_env, info->user_data);
} else if (ctx->notify_std_with_user_data_func != nullptr) {
auto func = ctx->notify_std_with_user_data_func;
func(*cpp_ten_env, ctx->user_data);
}

delete info;
delete ctx;
}

} // namespace
Expand Down Expand Up @@ -124,21 +124,21 @@ class ten_env_proxy_t {

bool notify(notify_std_func_t &&notify_func, bool sync = false,
error_t *err = nullptr) {
auto *info = new proxy_notify_info_t(std::move(notify_func));
auto *ctx = new proxy_notify_ctx_t(std::move(notify_func));

auto rc =
ten_env_proxy_notify(c_ten_env_proxy, proxy_notify, info, sync,
ten_env_proxy_notify(c_ten_env_proxy, proxy_notify, ctx, sync,
err != nullptr ? err->get_c_error() : nullptr);
if (!rc) {
delete info;
delete ctx;
}

return rc;
}

bool notify(notify_std_with_user_data_func_t &&notify_func, void *user_data,
bool sync = false, error_t *err = nullptr) {
auto *info = new proxy_notify_info_t(std::move(notify_func), user_data);
auto *info = new proxy_notify_ctx_t(std::move(notify_func), user_data);

auto rc =
ten_env_proxy_notify(c_ten_env_proxy, proxy_notify, info, sync,
Expand Down
28 changes: 14 additions & 14 deletions core/include/ten_runtime/binding/cpp/detail/test/env_tester_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ namespace {

using tester_notify_std_func_t = std::function<void(ten_env_tester_t &)>;

struct tester_proxy_notify_info_t {
explicit tester_proxy_notify_info_t(tester_notify_std_func_t &&func)
struct tester_proxy_notify_ctx_t {
explicit tester_proxy_notify_ctx_t(tester_notify_std_func_t &&func)
: notify_std_func(std::move(func)) {}

~tester_proxy_notify_info_t() = default;
~tester_proxy_notify_ctx_t() = default;

// @{
tester_proxy_notify_info_t(const tester_proxy_notify_info_t &) = delete;
tester_proxy_notify_info_t &operator=(const tester_proxy_notify_info_t &) =
tester_proxy_notify_ctx_t(const tester_proxy_notify_ctx_t &) = delete;
tester_proxy_notify_ctx_t &operator=(const tester_proxy_notify_ctx_t &) =
delete;
tester_proxy_notify_info_t(tester_proxy_notify_info_t &&) = delete;
tester_proxy_notify_info_t &operator=(tester_proxy_notify_info_t &&) = delete;
tester_proxy_notify_ctx_t(tester_proxy_notify_ctx_t &&) = delete;
tester_proxy_notify_ctx_t &operator=(tester_proxy_notify_ctx_t &&) = delete;
// @}

tester_notify_std_func_t notify_std_func;
Expand All @@ -41,17 +41,17 @@ struct tester_proxy_notify_info_t {
inline void proxy_notify(::ten_env_tester_t *ten_env, void *data = nullptr) {
TEN_ASSERT(data, "Invalid argument.");

auto *info = static_cast<tester_proxy_notify_info_t *>(data);
auto *ctx = static_cast<tester_proxy_notify_ctx_t *>(data);
auto *cpp_ten_env =
static_cast<ten_env_tester_t *>(ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(ten_env)));

if (info->notify_std_func != nullptr) {
auto func = info->notify_std_func;
if (ctx->notify_std_func != nullptr) {
auto func = ctx->notify_std_func;
func(*cpp_ten_env);
}

delete info;
delete ctx;
}

} // namespace
Expand Down Expand Up @@ -101,13 +101,13 @@ class ten_env_tester_proxy_t {
return false;
}

auto *info = new tester_proxy_notify_info_t(std::move(notify_func));
auto *ctx = new tester_proxy_notify_ctx_t(std::move(notify_func));

auto rc = ten_env_tester_proxy_notify(
c_ten_env_tester_proxy, proxy_notify, info,
c_ten_env_tester_proxy, proxy_notify, ctx,
err != nullptr ? err->get_c_error() : nullptr);
if (!rc) {
delete info;
delete ctx;
}

return rc;
Expand Down
7 changes: 7 additions & 0 deletions core/include/ten_runtime/test/env_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "ten_utils/lib/error.h"
#include "ten_utils/lib/smart_ptr.h"
#include "ten_utils/log/log.h"

typedef struct ten_env_tester_t ten_env_tester_t;

Expand Down Expand Up @@ -55,3 +56,9 @@ TEN_RUNTIME_API bool ten_env_tester_return_result(

TEN_RUNTIME_API bool ten_env_tester_stop_test(ten_env_tester_t *self,
ten_error_t *error);

TEN_RUNTIME_API bool ten_env_tester_log(ten_env_tester_t *self,
TEN_LOG_LEVEL level,
const char *func_name,
const char *file_name, size_t line_no,
const char *msg, ten_error_t *error);
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@ TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_tester_send_video_frame(
TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_tester_return_result(
PyObject *self, PyObject *args);

TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_tester_log(PyObject *self,
PyObject *args);

TEN_RUNTIME_PRIVATE_API bool ten_py_ten_env_tester_check_integrity(
ten_py_ten_env_tester_t *self);
3 changes: 3 additions & 0 deletions core/include_internal/ten_runtime/test/extension_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@ struct ten_extension_tester_t {
TEN_RUNTIME_API bool ten_extension_tester_check_integrity(
ten_extension_tester_t *self, bool check_thread);

TEN_RUNTIME_PRIVATE_API bool ten_extension_tester_thread_call_by_me(
ten_extension_tester_t *self);

TEN_RUNTIME_PRIVATE_API void test_app_ten_env_send_cmd(ten_env_t *ten_env,
void *user_data);
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
from .audio_frame import AudioFrame, AudioFrameDataFmt
from .data import Data
from .log_level import LogLevel
from .test import ExtensionTester, TenEnvTester
from .error import TenError
from .test import ExtensionTester, TenEnvTester
from .async_test import AsyncExtensionTester, AsyncTenEnvTester

# Specify what should be imported when a user imports * from the
# ten_runtime_python package.
Expand All @@ -48,4 +49,6 @@
"ExtensionTester",
"TenEnvTester",
"TenError",
"AsyncExtensionTester",
"AsyncTenEnvTester",
]
Loading

0 comments on commit cf75ae3

Please sign in to comment.