Skip to content

Commit

Permalink
feat!: add more codes for standalone testing (#62)
Browse files Browse the repository at this point in the history
feat!: add more codes for standalone testing

Since the addon for standalone testing is likely not placed within the app's directory tree, the base_dir of the addon is now explicitly specified by the addon itself. Currently, the bindings for C++/Go/Python are designed so that users don't need to explicitly specify the base_dir of the addon. Instead, the addon's register function will attempt to automatically detect the base_dir and pass it to ten_runtime. However, in certain special cases, such as with Cython, the Python code for automatically detecting the addon's base_dir might be optimized away. Therefore, an optional parameter is provided to allow users to explicitly specify the base_dir of the addon.

BREAKING CHANGE: Generally, no changes are required to the addon code, but C++ addons will need to be recompiled.
  • Loading branch information
halajohn authored Oct 4, 2024
1 parent be43e88 commit 5cf657a
Show file tree
Hide file tree
Showing 111 changed files with 1,016 additions and 757 deletions.
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
"args": [
"--gtest_filter=StandaloneTest.Basic"
"--gtest_filter=ExtensionTest.PropertySetInt32"
],
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"env": {
Expand Down Expand Up @@ -419,9 +419,9 @@
"name": "app (C/C++) (lldb, launch)",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/asr_xf_nrt/asr_xf_nrt_app/bin/asr_xf_nrt_app_source",
"program": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/hello_world/hello_world_app/bin/hello_world_app_source",
"args": [],
"cwd": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/asr_xf_nrt/asr_xf_nrt_app/",
"cwd": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/hello_world/hello_world_app",
"env": {
"ASAN_OPTIONS": "use_sigaltstack=0",
},
Expand Down
17 changes: 11 additions & 6 deletions core/include/ten_runtime/addon/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@

#include "ten_runtime/ten_env/internal/on_xxx_done.h"
#include "ten_runtime/ten_env/ten_env.h"
#include "ten_utils/lib/path.h" // IWYU pragma: export
#include "ten_utils/macro/ctor.h"

#define TEN_ADDON_REGISTER(TYPE, NAME, ADDON) \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_register_##TYPE(#NAME, (ADDON)); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_##TYPE(#NAME); \
#define TEN_ADDON_REGISTER(TYPE, NAME, ADDON) \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_string_t *base_dir = ten_path_get_module_path( \
(void *)____ctor_ten_declare_##NAME##_##TYPE##_addon____); \
ten_addon_register_##TYPE(#NAME, ten_string_get_raw_str(base_dir), \
(ADDON)); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_##TYPE(#NAME); \
}

typedef struct ten_addon_t ten_addon_t;
Expand Down
2 changes: 1 addition & 1 deletion core/include/ten_runtime/addon/extension/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef struct ten_extension_t ten_extension_t;
typedef struct ten_addon_host_t ten_addon_host_t;

TEN_RUNTIME_API ten_addon_host_t *ten_addon_register_extension(
const char *name, ten_addon_t *addon);
const char *name, const char *base_dir, ten_addon_t *addon);

TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_extension(const char *name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef struct ten_addon_host_t ten_addon_host_t;
TEN_ADDON_REGISTER(extension_group, NAME, ADDON)

TEN_RUNTIME_API ten_addon_host_t *ten_addon_register_extension_group(
const char *name, ten_addon_t *addon);
const char *name, const char *base_dir, ten_addon_t *addon);

TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_extension_group(
const char *name);
3 changes: 3 additions & 0 deletions core/include/ten_runtime/app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ TEN_RUNTIME_API bool ten_app_check_integrity(ten_app_t *self,
bool check_thread);

TEN_RUNTIME_API ten_env_t *ten_app_get_ten_env(ten_app_t *self);

TEN_RUNTIME_API void ten_app_add_ten_package_base_dir(ten_app_t *self,
const char *base_dir);
15 changes: 12 additions & 3 deletions core/include/ten_runtime/binding/cpp/internal/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ten_runtime/binding/cpp/internal/common.h"
#include "ten_runtime/binding/cpp/internal/ten_env.h"
#include "ten_runtime/ten_env/ten_env.h"
#include "ten_utils/lib/path.h" // IWYU pragma: export

namespace ten {

Expand Down Expand Up @@ -60,7 +61,7 @@ class addon_t {

private:
ten_addon_t *c_addon;
ten_env_t *cpp_ten_env;
ten_env_t *cpp_ten_env{};

virtual void on_create_instance_impl(ten_env_t &ten_env, const char *name,
void *context) = 0;
Expand Down Expand Up @@ -237,8 +238,12 @@ class extension_addon_t : public addon_t {
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_group_addon____) { \
g_##NAME##_default_extension_group_addon = \
new NAME##_default_extension_group_addon_t(); \
ten_string_t *base_dir = ten_path_get_module_path( \
(void *)____ctor_ten_declare_##NAME##_extension_group_addon____); \
ten_addon_register_extension_group( \
#NAME, g_##NAME##_default_extension_group_addon->get_c_addon()); \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_group_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_extension_group(#NAME); \
Expand All @@ -263,8 +268,12 @@ class extension_addon_t : public addon_t {
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_addon____) { \
g_##NAME##_default_extension_addon = \
new NAME##_default_extension_addon_t(); \
ten_string_t *base_dir = ten_path_get_module_path( \
(void *)____ctor_ten_declare_##NAME##_extension_addon____); \
ten_addon_register_extension( \
#NAME, g_##NAME##_default_extension_addon->get_c_addon()); \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_##TYPE##_addon____) { \
ten_addon_unregister_extension(#NAME); \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ class extension_tester_t {
extension_tester_t &operator=(const extension_tester_t &&) = delete;
// @}

void add_addon(const char *addon_name) {
void add_addon_name(const char *addon_name) {
TEN_ASSERT(addon_name, "Invalid argument.");
ten_extension_tester_add_addon(c_extension_tester, addon_name);
ten_extension_tester_add_addon_name(c_extension_tester, addon_name);
}

void run(error_t *err = nullptr) {
if (c_extension_tester == nullptr) {
return;
}
void add_addon_base_dir(const char *addon_path) {
TEN_ASSERT(addon_path, "Invalid argument.");
ten_extension_tester_add_addon_base_dir(c_extension_tester, addon_path);
}

ten_extension_tester_run(c_extension_tester);
bool run(error_t *err = nullptr) {
TEN_ASSERT(c_extension_tester, "Should not happen.");
return ten_extension_tester_run(c_extension_tester);
}

protected:
Expand Down
7 changes: 5 additions & 2 deletions core/include/ten_runtime/test/extension_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ TEN_RUNTIME_API ten_extension_tester_t *ten_extension_tester_create(

TEN_RUNTIME_API void ten_extension_tester_destroy(ten_extension_tester_t *self);

TEN_RUNTIME_API void ten_extension_tester_add_addon(
TEN_RUNTIME_API void ten_extension_tester_add_addon_name(
ten_extension_tester_t *self, const char *addon_name);

TEN_RUNTIME_API void ten_extension_tester_run(ten_extension_tester_t *self);
TEN_RUNTIME_API void ten_extension_tester_add_addon_base_dir(
ten_extension_tester_t *self, const char *addon_base_dir);

TEN_RUNTIME_API bool ten_extension_tester_run(ten_extension_tester_t *self);

TEN_RUNTIME_API ten_env_tester_t *ten_extension_tester_get_ten_env_tester(
ten_extension_tester_t *self);
6 changes: 3 additions & 3 deletions core/include/ten_utils/lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <stdbool.h>
#include <string.h>

#include "ten_utils/macro/check.h"
#include "ten_utils/lib/buf.h"
#include "ten_utils/lib/signature.h"
#include "ten_utils/macro/check.h"

#define TEN_STRING_SIGNATURE 0x178445C0402E320DU
#define TEN_STRING_PRE_BUF_SIZE 256
Expand Down Expand Up @@ -92,8 +92,8 @@ TEN_UTILS_API void ten_string_copy(ten_string_t *self, ten_string_t *other);
* @param other The other string object.
* @param size the max size, copy all if size <= 0
*/
TEN_UTILS_API void ten_string_copy_c_str(ten_string_t *self, const char *other,
size_t size);
TEN_UTILS_API void ten_string_init_from_c_str(ten_string_t *self,
const char *other, size_t size);

/**
* @brief Destroy a string object and release the memory.
Expand Down
13 changes: 9 additions & 4 deletions core/include_internal/ten_runtime/addon/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ typedef struct ten_addon_host_t {
ten_signature_t signature;

ten_string_t name; // The name of the addon.
ten_string_t base_dir;

ten_value_t manifest;
ten_value_t property;
Expand Down Expand Up @@ -114,6 +115,7 @@ TEN_RUNTIME_API void ten_addon_host_destroy(ten_addon_host_t *self);
TEN_RUNTIME_PRIVATE_API void ten_addon_register(ten_addon_store_t *addon_store,
ten_addon_host_t *addon_host,
const char *name,
const char *base_dir,
ten_addon_t *addon);

TEN_RUNTIME_PRIVATE_API ten_addon_t *ten_addon_unregister(
Expand Down Expand Up @@ -160,16 +162,19 @@ TEN_RUNTIME_PRIVATE_API bool ten_addon_host_destroy_instance_async(
TEN_RUNTIME_PRIVATE_API bool ten_addon_host_destroy_instance(
ten_addon_host_t *self, ten_env_t *ten_env, void *instance);

TEN_RUNTIME_PRIVATE_API void ten_addon_host_set_base_dir(
ten_addon_host_t *self, ten_app_t *app, ten_string_t *base_dir);

/**
* @brief The base directory of the loaded addon. This function can be called
* before any TEN app starts. Note that the returned string must be destroyed by
* users.
*/
TEN_RUNTIME_PRIVATE_API ten_string_t *ten_addon_host_get_base_dir(
TEN_RUNTIME_PRIVATE_API const char *ten_addon_host_get_base_dir(
ten_addon_host_t *self);

TEN_RUNTIME_PRIVATE_API void ten_addon_context_destroy(
ten_addon_context_t *self);

TEN_RUNTIME_PRIVATE_API ten_string_t *ten_addon_find_base_dir_from_app(
const char *addon_type, const char *addon_name);

TEN_RUNTIME_PRIVATE_API void ten_addon_find_and_set_base_dir(
ten_addon_host_t *self, const char *path);
8 changes: 7 additions & 1 deletion core/include_internal/ten_runtime/addon/addon_autoload.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

#include "ten_utils/lib/error.h"

typedef struct ten_app_t ten_app_t;

TEN_RUNTIME_PRIVATE_API void ten_addon_load_from_path(const char *path);

TEN_RUNTIME_PRIVATE_API bool ten_addon_load_all(ten_error_t *err);
TEN_RUNTIME_PRIVATE_API bool ten_addon_load_all_from_app_base_dir(
ten_app_t *app, ten_error_t *err);

TEN_RUNTIME_PRIVATE_API bool ten_addon_load_all_from_ten_package_base_dirs(
ten_app_t *app, ten_error_t *err);
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_protocol_find(
const char *protocol);

TEN_RUNTIME_API void ten_addon_register_protocol(const char *name,
const char *base_dir,
ten_addon_t *addon);

TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_protocol(const char *name);
6 changes: 3 additions & 3 deletions core/include_internal/ten_runtime/app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ typedef struct ten_app_t {
ten_schema_store_t schema_store;
ten_string_t base_dir;

ten_list_t ten_package_base_dirs;

void *user_data;
} ten_app_t;

Expand All @@ -119,14 +121,12 @@ TEN_RUNTIME_PRIVATE_API void ten_app_start(ten_app_t *self);
TEN_RUNTIME_API ten_sanitizer_thread_check_t *ten_app_get_thread_check(
ten_app_t *self);

TEN_RUNTIME_PRIVATE_API ten_string_t *ten_app_get_base_dir(ten_app_t *self);

TEN_RUNTIME_PRIVATE_API bool ten_app_thread_call_by_me(ten_app_t *self);

TEN_RUNTIME_PRIVATE_API ten_runloop_t *ten_app_get_attached_runloop(
ten_app_t *self);

TEN_RUNTIME_PRIVATE_API ten_string_t *ten_app_get_uri(ten_app_t *self);
TEN_RUNTIME_PRIVATE_API const char *ten_app_get_uri(ten_app_t *self);

TEN_RUNTIME_PRIVATE_API ten_protocol_context_store_t *
ten_app_get_protocol_context_store(ten_app_t *self);
Expand Down
9 changes: 7 additions & 2 deletions core/include_internal/ten_runtime/app/base_dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@

#include "ten_utils/lib/string.h"

TEN_RUNTIME_API void ten_app_find_base_dir(ten_string_t *start_path,
ten_string_t **app_path);
typedef struct ten_app_t ten_app_t;

TEN_RUNTIME_PRIVATE_API const char *ten_app_get_base_dir(ten_app_t *self);

TEN_RUNTIME_PRIVATE_API ten_string_t *ten_find_app_base_dir(void);

TEN_RUNTIME_PRIVATE_API void ten_app_find_and_set_base_dir(ten_app_t *self);
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
typedef struct ten_py_decorator_register_addon_t {
PyObject_HEAD
ten_string_t addon_name;
ten_string_t base_dir;
} ten_py_decorator_register_addon_t;

TEN_RUNTIME_PRIVATE_API bool
Expand Down
15 changes: 15 additions & 0 deletions core/include_internal/ten_runtime/common/base_dir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Copyright © 2024 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.
//
#pragma once

#include "ten_runtime/ten_config.h"

#include "ten_utils/lib/string.h"

TEN_RUNTIME_API ten_string_t *ten_find_base_dir(const char *start_path,
const char *type,
const char *name);
2 changes: 1 addition & 1 deletion core/include_internal/ten_runtime/common/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

#include "ten_runtime/ten_config.h"

#define DEFAULT_LOG_OUTPUT_LEVEL TEN_LOG_LEVEL_WARN
#define DEFAULT_LOG_OUTPUT_LEVEL TEN_LOG_LEVEL_DEBUG
3 changes: 2 additions & 1 deletion core/include_internal/ten_runtime/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ TEN_RUNTIME_PRIVATE_API ten_runloop_t *ten_engine_get_attached_runloop(
TEN_RUNTIME_PRIVATE_API bool ten_engine_is_ready_to_handle_msg(
ten_engine_t *self);

TEN_RUNTIME_PRIVATE_API const char *ten_engine_get_name(ten_engine_t *self);
TEN_RUNTIME_PRIVATE_API const char *ten_engine_get_name(ten_engine_t *self,
bool check_thread);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "ten_runtime/ten_config.h"

#include "ten_utils/lib/string.h"
typedef struct ten_extension_t ten_extension_t;

TEN_RUNTIME_PRIVATE_API ten_string_t *ten_get_app_base_dir(void);
TEN_RUNTIME_PRIVATE_API const char *ten_extension_get_base_dir(
ten_extension_t *self);
7 changes: 1 addition & 6 deletions core/include_internal/ten_runtime/extension/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ struct ten_extension_t {
ten_addon_host_t *addon_host;
ten_string_t name;

ten_string_t base_dir;

ten_env_t *ten_env;

ten_extension_thread_t *extension_thread;
Expand Down Expand Up @@ -255,10 +253,7 @@ TEN_RUNTIME_PRIVATE_API ten_runloop_t *ten_extension_get_attached_runloop(
ten_extension_t *self);

TEN_RUNTIME_PRIVATE_API const char *ten_extension_get_name(
ten_extension_t *self);

TEN_RUNTIME_PRIVATE_API ten_string_t *ten_extension_get_base_dir(
ten_extension_t *self);
ten_extension_t *self, bool check_thread);

TEN_RUNTIME_API ten_addon_host_t *ten_extension_get_addon(
ten_extension_t *self);
Expand Down
14 changes: 14 additions & 0 deletions core/include_internal/ten_runtime/extension_group/base_dir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Copyright © 2024 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.
//
#pragma once

#include "ten_runtime/ten_config.h"

typedef struct ten_extension_group_t ten_extension_group_t;

TEN_RUNTIME_PRIVATE_API const char *ten_extension_group_get_base_dir(
ten_extension_group_t *self);
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ typedef struct ten_extension_group_t {
ten_metadata_info_t *property_info;

size_t extensions_cnt_of_being_destroyed;

ten_string_t base_dir;
} ten_extension_group_t;

TEN_RUNTIME_PRIVATE_API void ten_extension_group_create_extensions(
Expand All @@ -105,9 +103,6 @@ TEN_RUNTIME_PRIVATE_API void ten_extension_group_on_deinit_done(
TEN_RUNTIME_PRIVATE_API void ten_extension_group_on_destroy_extensions_done(
ten_extension_group_t *self);

TEN_RUNTIME_PRIVATE_API ten_string_t *ten_extension_group_get_base_dir(
ten_extension_group_t *self);

TEN_RUNTIME_PRIVATE_API void ten_extension_group_on_create_extensions_done(
ten_extension_group_t *self, ten_list_t *extensions);

Expand Down
2 changes: 1 addition & 1 deletion core/include_internal/ten_runtime/extension_group/on_xxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ ten_extension_group_on_addon_destroy_extension_done(
ten_env_t *self, ten_addon_context_t *addon_context);

TEN_RUNTIME_PRIVATE_API const char *ten_extension_group_get_name(
ten_extension_group_t *self);
ten_extension_group_t *self, bool check_thread);
3 changes: 2 additions & 1 deletion core/include_internal/ten_runtime/test/extension_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ struct ten_extension_tester_t {
ten_env_proxy_t *tester_extension_ten_env_proxy;
ten_event_t *tester_extension_ten_env_proxy_create_completed;

ten_string_t target_extension_addon_name;
ten_list_t addon_names;
ten_list_t addon_base_dirs;

ten_extension_tester_on_start_func_t on_start;
ten_extension_tester_on_cmd_func_t on_cmd;
Expand Down
Loading

0 comments on commit 5cf657a

Please sign in to comment.