Skip to content

Commit

Permalink
feat: add more standalone testing logic (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Oct 5, 2024
1 parent b1b66c3 commit 4b49244
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 387 deletions.
120 changes: 0 additions & 120 deletions build/ten_runtime/feature/go_test_build.py

This file was deleted.

10 changes: 5 additions & 5 deletions core/include_internal/ten_runtime/test/extension_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ struct ten_extension_tester_t {
ten_signature_t signature;
ten_sanitizer_thread_check_t thread_check;

ten_thread_t *tester_app_thread;
ten_env_proxy_t *tester_app_ten_env_proxy;
ten_event_t *tester_app_ten_env_proxy_create_completed;
ten_thread_t *test_app_thread;
ten_env_proxy_t *test_app_ten_env_proxy;
ten_event_t *test_app_ten_env_proxy_create_completed;

ten_env_proxy_t *tester_extension_ten_env_proxy;
ten_event_t *tester_extension_ten_env_proxy_create_completed;
ten_env_proxy_t *test_extension_ten_env_proxy;
ten_event_t *test_extension_ten_env_proxy_create_completed;

ten_list_t addon_names;
ten_list_t addon_base_dirs;
Expand Down
2 changes: 1 addition & 1 deletion core/include_internal/ten_runtime/test/test_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

#include "ten_runtime/ten_config.h"

TEN_RUNTIME_PRIVATE_API void *ten_builtin_tester_app_thread_main(void *args);
TEN_RUNTIME_PRIVATE_API void *ten_builtin_test_app_thread_main(void *args);
5 changes: 2 additions & 3 deletions core/include_internal/ten_runtime/test/test_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "ten_runtime/ten_config.h"

TEN_RUNTIME_PRIVATE_API void ten_builtin_tester_extension_addon_register(void);
TEN_RUNTIME_PRIVATE_API void ten_builtin_test_extension_addon_register(void);

TEN_RUNTIME_PRIVATE_API void ten_builtin_tester_extension_addon_unregister(
void);
TEN_RUNTIME_PRIVATE_API void ten_builtin_test_extension_addon_unregister(void);
4 changes: 2 additions & 2 deletions core/src/ten_runtime/global/on_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ TEN_CONSTRUCTOR(ten_runtime_on_load) {
// multiple apps within a single process, they are registered in the global
// addon store.
ten_builtin_extension_group_addon_register();
ten_builtin_tester_extension_addon_register();
ten_builtin_test_extension_addon_register();
}

TEN_DESTRUCTOR(ten_runtime_on_unload) {
ten_builtin_tester_extension_addon_unregister();
ten_builtin_test_extension_addon_unregister();
ten_builtin_extension_group_addon_unregister();

ten_global_deinit();
Expand Down
17 changes: 11 additions & 6 deletions core/src/ten_runtime/test/env_tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ bool ten_env_tester_check_integrity(ten_env_tester_t *self) {
return false;
}

// TODO(Wei): Currently, all calls to ten_env_tester must be made within the
// tester thread. If we need to call the ten_env_tester API from a non-tester
// thread, a mechanism similar to ten_env_tester_proxy must be created.
if (!ten_sanitizer_thread_check_do_check(&self->tester->thread_check)) {
return false;
}
Expand Down Expand Up @@ -136,6 +139,8 @@ static void send_cmd_callback(ten_extension_t *extension, ten_env_t *ten_env,
if (send_cmd_info->handler) {
send_cmd_info->cmd_result = ten_shared_ptr_clone(cmd_result);

// Inject cmd result into the extension_tester thread to ensure thread
// safety.
ten_runloop_post_task_tail(
send_cmd_info->tester->tester_runloop,
ten_extension_tester_execute_cmd_result_handler_task,
Expand All @@ -145,8 +150,8 @@ static void send_cmd_callback(ten_extension_t *extension, ten_env_t *ten_env,
}
}

static void tester_extension_ten_env_send_cmd(ten_env_t *ten_env,
void *user_data) {
static void test_extension_ten_env_send_cmd(ten_env_t *ten_env,
void *user_data) {
TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true),
"Should not happen.");

Expand All @@ -171,9 +176,9 @@ bool ten_env_tester_send_cmd(ten_env_tester_t *self, ten_shared_ptr_t *cmd,
ten_extension_tester_send_cmd_info_create(
self->tester, ten_shared_ptr_clone(cmd), handler, user_data);

TEN_ASSERT(self->tester->tester_extension_ten_env_proxy, "Invalid argument.");
return ten_env_proxy_notify(self->tester->tester_extension_ten_env_proxy,
tester_extension_ten_env_send_cmd, send_cmd_info,
TEN_ASSERT(self->tester->test_extension_ten_env_proxy, "Invalid argument.");
return ten_env_proxy_notify(self->tester->test_extension_ten_env_proxy,
test_extension_ten_env_send_cmd, send_cmd_info,
false, NULL);
}

Expand All @@ -188,7 +193,7 @@ void ten_env_tester_stop_test(ten_env_tester_t *self) {
NULL, NULL, NULL);
TEN_ASSERT(rc, "Should not happen.");

ten_env_proxy_notify(self->tester->tester_app_ten_env_proxy,
ten_env_proxy_notify(self->tester->test_app_ten_env_proxy,
test_app_ten_env_send_cmd, close_app_cmd, false, NULL);
}

Expand Down
60 changes: 32 additions & 28 deletions core/src/ten_runtime/test/extension_tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ ten_extension_tester_t *ten_extension_tester_create(
self->ten_env_tester = ten_env_tester_create(self);
self->tester_runloop = ten_runloop_create(NULL);

self->tester_extension_ten_env_proxy = NULL;
self->tester_extension_ten_env_proxy_create_completed =
ten_event_create(0, 1);
self->test_extension_ten_env_proxy = NULL;
self->test_extension_ten_env_proxy_create_completed = ten_event_create(0, 1);

self->tester_app_ten_env_proxy = NULL;
self->tester_app_ten_env_proxy_create_completed = ten_event_create(0, 1);
self->test_app_ten_env_proxy = NULL;
self->test_app_ten_env_proxy_create_completed = ten_event_create(0, 1);

self->tester_app_thread = NULL;
self->test_app_thread = NULL;
self->user_data = NULL;

return self;
Expand Down Expand Up @@ -141,29 +140,31 @@ void ten_extension_tester_destroy(ten_extension_tester_t *self) {
TEN_ASSERT(self && ten_extension_tester_check_integrity(self, true),
"Invalid argument.");

TEN_ASSERT(self->tester_app_ten_env_proxy == NULL,
"The `ten_env_proxy` of `tester_app` should be released in the "
"tester task triggered by the `deinit` of `tester_app`.");
if (self->tester_app_ten_env_proxy_create_completed) {
ten_event_destroy(self->tester_app_ten_env_proxy_create_completed);
TEN_ASSERT(self->test_app_ten_env_proxy == NULL,
"The `ten_env_proxy` of `test_app` should be released in the "
"tester task triggered by the `deinit` of `test_app`.");
if (self->test_app_ten_env_proxy_create_completed) {
ten_event_destroy(self->test_app_ten_env_proxy_create_completed);
}

TEN_ASSERT(
self->tester_extension_ten_env_proxy == NULL,
"The `ten_env_proxy` of `tester_extension` should be released in the "
"tester task triggered by the `deinit` of `tester_extension`.");
if (self->tester_extension_ten_env_proxy_create_completed) {
ten_event_destroy(self->tester_extension_ten_env_proxy_create_completed);
self->test_extension_ten_env_proxy == NULL,
"The `ten_env_proxy` of `test_extension` should be released in the "
"tester task triggered by the `deinit` of `test_extension`.");
if (self->test_extension_ten_env_proxy_create_completed) {
ten_event_destroy(self->test_extension_ten_env_proxy_create_completed);
}

ten_thread_join(self->tester_app_thread, -1);
ten_thread_join(self->test_app_thread, -1);

ten_list_clear(&self->addon_names);
ten_list_clear(&self->addon_base_dirs);

ten_env_tester_destroy(self->ten_env_tester);
ten_sanitizer_thread_check_deinit(&self->thread_check);

ten_runloop_destroy(self->tester_runloop);
self->tester_runloop = NULL;

TEN_FREE(self);
}
Expand Down Expand Up @@ -301,16 +302,16 @@ static void ten_extension_tester_create_and_start_graph(

ten_json_destroy(start_graph_cmd_json);

rc = ten_env_proxy_notify(self->tester_app_ten_env_proxy,
rc = ten_env_proxy_notify(self->test_app_ten_env_proxy,
test_app_ten_env_send_cmd, start_graph_cmd, false,
NULL);
TEN_ASSERT(rc, "Should not happen.");

// Wait for the tester extension to create the `ten_env_proxy`.
ten_event_wait(self->tester_extension_ten_env_proxy_create_completed, -1);
ten_event_wait(self->test_extension_ten_env_proxy_create_completed, -1);

ten_event_destroy(self->tester_extension_ten_env_proxy_create_completed);
self->tester_extension_ten_env_proxy_create_completed = NULL;
ten_event_destroy(self->test_extension_ten_env_proxy_create_completed);
self->test_extension_ten_env_proxy_create_completed = NULL;
}

static void ten_extension_tester_create_and_run_app(
Expand All @@ -319,17 +320,17 @@ static void ten_extension_tester_create_and_run_app(
"Invalid argument.");

// Create the tester app.
self->tester_app_thread = ten_thread_create(
"test app thread", ten_builtin_tester_app_thread_main, self);
self->test_app_thread = ten_thread_create(
"test app thread", ten_builtin_test_app_thread_main, self);

// Wait until the tester app is started successfully.
ten_event_wait(self->tester_app_ten_env_proxy_create_completed, -1);
ten_event_wait(self->test_app_ten_env_proxy_create_completed, -1);

ten_event_destroy(self->tester_app_ten_env_proxy_create_completed);
self->tester_app_ten_env_proxy_create_completed = NULL;
ten_event_destroy(self->test_app_ten_env_proxy_create_completed);
self->test_app_ten_env_proxy_create_completed = NULL;

TEN_ASSERT(self->tester_app_ten_env_proxy,
"tester_app should have been created its ten_env_proxy.");
TEN_ASSERT(self->test_app_ten_env_proxy,
"test_app should have been created its ten_env_proxy.");
}

static void ten_extension_tester_on_start_task(void *self_,
Expand Down Expand Up @@ -366,6 +367,9 @@ bool ten_extension_tester_run(ten_extension_tester_t *self) {
return false;
}

// Inject the task that calls on_start into the runloop of extension_tester,
// ensuring that on_start is called within the extension_tester thread to
// guarantee thread safety.
ten_runloop_post_task_tail(self->tester_runloop,
ten_extension_tester_on_start_task, self, NULL);

Expand Down
Loading

0 comments on commit 4b49244

Please sign in to comment.