Skip to content

Commit

Permalink
fix: read float type property, but int value from json (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Dec 1, 2024
1 parent 616df9f commit c0e7ee4
Show file tree
Hide file tree
Showing 21 changed files with 151 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
"args": [
"--gtest_filter=AudioFrameTest.Basic"
"--gtest_filter=PropertyTest.GetFloat64TypeButIntValue"
],
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"env": {
"LD_LIBRARY_PATH": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"ASAN_OPTIONS": "abort_on_error=1",
"TEN_ENABLE_MEMORY_TRACKING": "true"
// "TEN_ENABLE_MEMORY_TRACKING": "true"
},
},
{
Expand Down
8 changes: 8 additions & 0 deletions core/include_internal/ten_utils/value/type_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
"This file contains C99 array designated initializer, and Visual Studio C++ compiler can only support up to C89 by default, so we enable this checking to prevent any wrong inclusion of this file."
#endif

#define TEN_IS_INTEGER_TYPE(t) \
((t) == TEN_TYPE_INT8 || (t) == TEN_TYPE_UINT8 || (t) == TEN_TYPE_INT16 || \
(t) == TEN_TYPE_UINT16 || (t) == TEN_TYPE_INT32 || \
(t) == TEN_TYPE_UINT32 || (t) == TEN_TYPE_INT64 || (t) == TEN_TYPE_UINT64)

#define TEN_IS_FLOAT_TYPE(t) \
((t) == TEN_TYPE_FLOAT32 || (t) == TEN_TYPE_FLOAT64)

typedef struct ten_type_info_t {
const char *name;
} ten_type_info_t;
Expand Down
16 changes: 4 additions & 12 deletions core/src/ten_utils/value/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,12 @@ bool ten_type_is_compatible(TEN_TYPE actual, TEN_TYPE expected) {
return true;
}

if ((expected == TEN_TYPE_UINT8 || expected == TEN_TYPE_INT8 ||
expected == TEN_TYPE_UINT16 || expected == TEN_TYPE_INT16 ||
expected == TEN_TYPE_UINT32 || expected == TEN_TYPE_INT32 ||
expected == TEN_TYPE_UINT64 || expected == TEN_TYPE_INT64) &&
(actual == TEN_TYPE_UINT8 || actual == TEN_TYPE_INT8 ||
actual == TEN_TYPE_UINT16 || actual == TEN_TYPE_INT16 ||
actual == TEN_TYPE_UINT32 || actual == TEN_TYPE_INT32 ||
actual == TEN_TYPE_UINT64 || actual == TEN_TYPE_INT64)) {
return true;
if (TEN_IS_INTEGER_TYPE(expected)) {
return TEN_IS_INTEGER_TYPE(actual);
}

if ((expected == TEN_TYPE_FLOAT32 || expected == TEN_TYPE_FLOAT64) &&
(actual == TEN_TYPE_FLOAT32 || actual == TEN_TYPE_FLOAT64)) {
return true;
if (TEN_IS_FLOAT_TYPE(expected)) {
return TEN_IS_INTEGER_TYPE(actual) || TEN_IS_FLOAT_TYPE(actual);
}

switch (expected) {
Expand Down
1 change: 1 addition & 0 deletions tests/ten_runtime/smoke/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ten_executable("ten_runtime_smoke_test") {
"log_test",
"msg_test",
"notify_test",
"property",
"result_conversion",
"standalone_test",
"ten_env_call_timing",
Expand Down
1 change: 0 additions & 1 deletion tests/ten_runtime/smoke/extension_test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ glob("extension_test") {
"path",
"predefined_graph",
"prepare_to_stop",
"property",
"resp_handler",
"return",
"same_thread_ext_on_xxx",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(test_property_access_app_store_2,

} // namespace

TEST(ExtensionTest, PropertyAccessAppStore) { // NOLINT
TEST(PropertyTest, AccessAppStore) { // NOLINT
auto *app_thread = ten_thread_create("app thread", app_thread_main, nullptr);

// Create a client and connect to the app.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(test_property_access_app_store_async_2,

} // namespace

TEST(ExtensionTest, PropertyAccessAppStoreAsync) { // NOLINT
TEST(PropertyTest, AccessAppStoreAsync) { // NOLINT
auto *app_thread = ten_thread_create("app thread", app_thread_main, nullptr);

// Create a client and connect to the app.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_all__extension, test_extension);

} // namespace

TEST(ExtensionTest, PropertyAll) { // NOLINT
TEST(PropertyTest, All) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_start_graph_cmd__extension,

} // namespace

TEST(ExtensionTest, PropertyConnectCmd) { // NOLINT
TEST(PropertyTest, ConnectCmd) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(

} // namespace

TEST(ExtensionTest, PropertyConnectCmdOverrideExtensionSuccess) { // NOLINT
TEST(PropertyTest, ConnectCmdOverrideExtensionSuccess) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_extension__extension,

} // namespace

TEST(ExtensionTest, PropertyExtension) { // NOLINT
TEST(PropertyTest, Extension) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_get_float64__extension,

} // namespace

TEST(ExtensionTest, PropertyGetFloat64) { // NOLINT
TEST(PropertyTest, GetFloat64) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//
// 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.
//
#include <nlohmann/json.hpp>
#include <string>

#include "gtest/gtest.h"
#include "include_internal/ten_runtime/binding/cpp/ten.h"
#include "ten_utils/lang/cpp/lib/value.h"
#include "ten_utils/lib/thread.h"
#include "ten_utils/macro/macros.h"
#include "tests/common/client/cpp/msgpack_tcp.h"
#include "tests/ten_runtime/smoke/extension_test/util/binding/cpp/check.h"

#define PROP_NAME "test_prop"
#define PROP_VAL 123

namespace {

class test_extension : public ten::extension_t {
public:
explicit test_extension(const std::string &name) : ten::extension_t(name) {}

void on_cmd(ten::ten_env_t &ten_env,
std::unique_ptr<ten::cmd_t> cmd) override {
if (std::string(cmd->get_name()) == "hello_world") {
auto prop_value = ten_env.get_property_float64("app:" PROP_NAME);
if (fabs(prop_value - PROP_VAL) < 0.01) {
auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK);
cmd_result->set_property("detail", "hello world, too");
ten_env.return_result(std::move(cmd_result), std::move(cmd));
}
}
}
};

class test_app : public ten::app_t {
public:
void on_configure(ten::ten_env_t &ten_env) override {
ten::ten_env_internal_accessor_t ten_env_internal_accessor(&ten_env);
bool rc = ten_env_internal_accessor.init_manifest_from_json(
// clang-format off
"{\
\"type\": \"app\",\
\"name\": \"test_app\",\
\"version\": \"1.0.0\",\
\"api\": {\
\"property\": {\
\"" PROP_NAME "\": {\
\"type\": \"float64\"\
}\
}\
}\
}"
// clang-format on
);
ASSERT_EQ(rc, true);

rc = ten_env.init_property_from_json(
"{\
\"_ten\": {\
\"uri\": \"msgpack://127.0.0.1:8001/\"},\
\"" PROP_NAME "\":" TEN_XSTR(PROP_VAL) "}");
ASSERT_EQ(rc, true);

ten_env.on_configure_done();
}
};

void *test_app_thread_main(TEN_UNUSED void *args) {
auto *app = new test_app();
app->run();
delete app;

return nullptr;
}

TEN_CPP_REGISTER_ADDON_AS_EXTENSION(
property_get_float64_type_but_int_value__extension, test_extension);

} // namespace

TEST(PropertyTest, GetFloat64TypeButIntValue) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);

// Create a client and connect to the app.
auto *client = new ten::msgpack_tcp_client_t("msgpack://127.0.0.1:8001/");

// Send graph.
auto start_graph_cmd = ten::cmd_start_graph_t::create();
start_graph_cmd->set_graph_from_json(R"({
"nodes": [{
"type": "extension",
"name": "test_extension",
"addon": "property_get_float64_type_but_int_value__extension",
"app": "msgpack://127.0.0.1:8001/",
"extension_group": "property_get_float64_type_but_int_value__extension_group"
}]
})");
auto cmd_result =
client->send_cmd_and_recv_result(std::move(start_graph_cmd));
ten_test::check_status_code(cmd_result, TEN_STATUS_CODE_OK);

// Send a user-defined 'hello world' command.
auto hello_world_cmd = ten::cmd_t::create("hello_world");
hello_world_cmd->set_dest(
"msgpack://127.0.0.1:8001/", nullptr,
"property_get_float64_type_but_int_value__extension_group",
"test_extension");
cmd_result = client->send_cmd_and_recv_result(std::move(hello_world_cmd));
ten_test::check_status_code(cmd_result, TEN_STATUS_CODE_OK);
ten_test::check_detail_with_string(cmd_result, "hello world, too");

delete client;

ten_thread_join(app_thread, -1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_get_int32__extension,

} // namespace

TEST(ExtensionTest, PropertyGetInt32) { // NOLINT
TEST(PropertyTest, GetInt32) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_in_graph_use_env_1__extension,

} // namespace

TEST(ExtensionTest, PropertyInGraphUseEnv1) { // NOLINT
TEST(PropertyTest, InGraphUseEnv1) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_in_graph_use_env_2__extension,

} // namespace

TEST(ExtensionTest, PropertyInGraphUseEnv2) { // NOLINT
TEST(PropertyTest, InGraphUseEnv2) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_not_exist__extension,

} // namespace

TEST(ExtensionTest, PropertyNotExist) { // NOLINT
TEST(PropertyTest, NotExist) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_predefined_graph__extension,

} // namespace

TEST(ExtensionTest, PropertyPredefinedGraph) { // NOLINT
TEST(PropertyTest, PredefinedGraph) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_set_float32__extension,

} // namespace

TEST(ExtensionTest, PropertySetFloat32) { // NOLINT
TEST(PropertyTest, SetFloat32) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ TEN_CPP_REGISTER_ADDON_AS_EXTENSION(property_set_int32__extension,

} // namespace

TEST(ExtensionTest, PropertySetInt32) { // NOLINT
TEST(PropertyTest, SetInt32) { // NOLINT
// Start app.
auto *app_thread =
ten_thread_create("app thread", test_app_thread_main, nullptr);
Expand Down

0 comments on commit c0e7ee4

Please sign in to comment.