Skip to content
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

fix: bugfix for is_cmd_connected and is_property_exist in Python binding #211

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,12 @@ PyObject *ten_py_ten_env_is_property_exist(PyObject *self, PyObject *args) {
"Failed to parse argument when ten_env.is_property_exist.");
}

ten_error_t err;
ten_error_init(&err);

bool is_exist = ten_env_is_property_exist(py_ten_env->c_ten_env, path, &err);
if (!ten_error_is_success(&err)) {
ten_error_deinit(&err);

return ten_py_raise_py_value_error_exception(
"Failed to check if property exists.");
ten_value_t *value =
ten_py_ten_property_get_and_check_if_exists(py_ten_env, path);
if (!value) {
return PyBool_FromLong(false);
}

ten_error_deinit(&err);
return PyBool_FromLong(is_exist);
ten_value_destroy(value);
return PyBool_FromLong(true);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,57 @@
//
#include "include_internal/ten_runtime/binding/python/common/error.h"
#include "include_internal/ten_runtime/binding/python/ten_env/ten_env.h"
#include "ten_runtime/ten_env_proxy/ten_env_proxy.h"
#include "ten_utils/macro/memory.h"

typedef struct ten_env_notify_is_cmd_connected_info_t {
bool result;
ten_string_t name;
ten_event_t *completed;
} ten_env_notify_is_cmd_connected_info_t;

static ten_env_notify_is_cmd_connected_info_t *
ten_env_notify_is_cmd_connected_info_create(const char *name) {
ten_env_notify_is_cmd_connected_info_t *info =
TEN_MALLOC(sizeof(ten_env_notify_is_cmd_connected_info_t));
TEN_ASSERT(info, "Failed to allocate memory.");

info->result = true;
ten_string_init_formatted(&info->name, "%s", name);
info->completed = ten_event_create(0, 1);

return info;
}

static void ten_env_notify_is_cmd_connected_info_destroy(
ten_env_notify_is_cmd_connected_info_t *info) {
TEN_ASSERT(info, "Invalid argument.");

ten_string_deinit(&info->name);
ten_event_destroy(info->completed);

TEN_FREE(info);
}

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

ten_env_notify_is_cmd_connected_info_t *info = user_data;
TEN_ASSERT(info, "Should not happen.");

ten_error_t err;
ten_error_init(&err);

info->result = ten_env_is_cmd_connected(
ten_env, ten_string_get_raw_str(&info->name), &err);

ten_event_set(info->completed);

ten_error_deinit(&err);
}

PyObject *ten_py_ten_env_is_cmd_connected(PyObject *self, PyObject *args) {
ten_py_ten_env_t *py_ten_env = (ten_py_ten_env_t *)self;
Expand All @@ -26,8 +77,27 @@ PyObject *ten_py_ten_env_is_cmd_connected(PyObject *self, PyObject *args) {
ten_error_t err;
ten_error_init(&err);

bool is_connected =
ten_env_is_cmd_connected(py_ten_env->c_ten_env, cmd_name, &err);
ten_env_notify_is_cmd_connected_info_t *info =
ten_env_notify_is_cmd_connected_info_create(cmd_name);

if (!ten_env_proxy_notify(py_ten_env->c_ten_env_proxy,
ten_env_proxy_notify_is_cmd_connected, info, false,
&err)) {
ten_error_deinit(&err);
ten_env_notify_is_cmd_connected_info_destroy(info);

return ten_py_raise_py_value_error_exception(
"Failed to notify is command connected.");
}

PyThreadState *saved_py_thread_state = PyEval_SaveThread();
ten_event_wait(info->completed, -1);
PyEval_RestoreThread(saved_py_thread_state);

bool is_connected = info->result;

ten_env_notify_is_cmd_connected_info_destroy(info);

if (!ten_error_is_success(&err)) {
ten_error_deinit(&err);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ async def on_init(self, ten_env: AsyncTenEnv) -> None:
async def on_start(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.log_debug("on_start")

assert ten_env.is_property_exist("unknown_field") is False

ten_env.set_property_string("string_field", "hello")
assert ten_env.is_property_exist("string_field") is True

ten_env.on_start_done()

async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
Expand All @@ -41,6 +47,9 @@ async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:
# Mock async operation, e.g. network, file I/O.
await asyncio.sleep(0.5)

assert ten_env.is_cmd_connected("hello") is True
assert ten_env.is_cmd_connected("unknown_cmd") is False

# Send a new command to other extensions and wait for the result. The
# result will be returned to the original sender.
new_cmd = Cmd.create("hello")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ class test_extension_1 : public ten::extension_t {
explicit test_extension_1(const std::string &name) : ten::extension_t(name) {}

void on_configure(ten::ten_env_t &ten_env) override {
// We have increased the path timeout to 20 minutes because, under limited
// computing resources, it is easy to exceed the path timeout without
// completing the data transmission. This can lead to the path being
// discarded, causing the test case to hang indefinitely. Therefore, we have
// extended the path timeout to avoid this situation.

// clang-format off
bool rc = ten_env.init_property_from_json( R"({
"_ten": {
"path_timeout": 600000000
"path_timeout": 1200000000
}
})");
// clang-format on
Expand Down Expand Up @@ -831,9 +837,15 @@ class test_extension_2 : public ten::extension_t {
OUTER_THREAD_FOR_LOOP_CNT + 1) {}

void on_configure(ten::ten_env_t &ten_env) override {
// We have increased the path timeout to 20 minutes because, under limited
// computing resources, it is easy to exceed the path timeout without
// completing the data transmission. This can lead to the path being
// discarded, causing the test case to hang indefinitely. Therefore, we have
// extended the path timeout to avoid this situation.

ten_env.init_property_from_json(R"({
"_ten": {
"path_timeout": 600000000
"path_timeout": 1200000000
}
})");
ten_env.on_configure_done();
Expand Down
Loading