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

chore: 1. remote_t must be created with connection_t 2. add more test… #259

Merged
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
4 changes: 2 additions & 2 deletions core/src/ten_runtime/app/predefined_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ bool ten_app_get_predefined_graph_extensions_and_groups_info_by_name(
return false;
}

if (!ten_extensions_info_clone(
extensions_info, &predefined_graph_info->extensions_info, err)) {
if (!ten_extensions_info_clone(&predefined_graph_info->extensions_info,
extensions_info, err)) {
return false;
}

Expand Down
2 changes: 0 additions & 2 deletions core/src/ten_runtime/connection/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,6 @@ void ten_connection_attach_to_remote(ten_connection_t *self,
ten_atomic_store(&self->attach_to, TEN_CONNECTION_ATTACH_TO_REMOTE);
self->attached_target.remote = remote;

remote->connection = self;

ten_connection_set_on_closed(self, ten_remote_on_connection_closed, remote);

if (self->protocol) {
Expand Down
16 changes: 6 additions & 10 deletions core/src/ten_runtime/engine/internal/remote_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,12 @@ void ten_engine_link_connection_to_remote(ten_engine_t *self,
TEN_ASSERT(uri, "Invalid argument.");

ten_remote_t *remote = ten_engine_find_remote(self, uri);
if (remote) {
TEN_ASSERT(
remote->connection == NULL,
"The relationship of remote and connection should be 1-1 mapping.");

ten_connection_attach_to_remote(connection, remote);
} else {
remote = ten_remote_create_for_engine(uri, self, connection);
TEN_ASSERT(
!remote,
"The relationship of remote and connection should be 1-1 mapping.");

ten_engine_add_remote(self, remote);
}
remote = ten_remote_create_for_engine(uri, self, connection);
ten_engine_add_remote(self, remote);
}

static void ten_engine_on_remote_protocol_created(ten_env_t *ten_env,
Expand Down Expand Up @@ -262,6 +257,7 @@ static bool ten_engine_create_remote_async(
TEN_LOGE("Failed to create protocol for %s. err: %s", uri,
ten_error_errmsg(&err));
ten_error_deinit(&err);
ten_engine_on_protocol_created_info_destroy(info);
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/ten_runtime/remote/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ static ten_remote_t *ten_remote_create_empty(const char *uri,

ten_connection_attach_to_remote(connection, self);

self->connection = connection;

self->engine = NULL;

ten_loc_init_empty(&self->explicit_dest_loc);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
//
// 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 "gtest/gtest.h"
#include "include_internal/ten_runtime/binding/cpp/ten.h"
#include "ten_runtime/common/status_code.h"
#include "tests/common/client/cpp/msgpack_tcp.h"
#include "tests/ten_runtime/smoke/extension_test/util/binding/cpp/check.h"

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

void on_start(ten::ten_env_t &ten_env) override {
std::string start_graph_json = R"({
"_ten": {
"type": "start_graph",
"seq_id": "222",
"dest": [{
"app": "localhost"
}],
"predefined_graph": "graph_1"
}
})"_json.dump();

ten_env.send_json(
start_graph_json.c_str(),
[start_graph_json](ten::ten_env_t &ten_env,
std::unique_ptr<ten::cmd_result_t> cmd) {
auto status_code = cmd->get_status_code();
ASSERT_EQ(status_code, TEN_STATUS_CODE_ERROR);

auto detail = cmd->get_property_string("detail");
ASSERT_EQ(detail, "Failed to connect to msgpack://127.0.0.1:8888/");

// The app will not be closed because it is running in
// long_running_mode.

ten_env.on_start_done();
});
}

void on_cmd(ten::ten_env_t &ten_env,
std::unique_ptr<ten::cmd_t> cmd) override {
nlohmann::json json = nlohmann::json::parse(cmd->to_json());
if (json["_ten"]["name"] == "test") {
nlohmann::json detail = {{"id", 1}, {"name", "a"}};

auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK);
cmd_result->set_property_from_json("detail", detail.dump().c_str());
ten_env.return_result(std::move(cmd_result), std::move(cmd));
} else {
TEN_ASSERT(0, "Should not happen.");
}
}
};

class test_app_1 : 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
R"({
"type": "app",
"name": "test_app",
"version": "0.1.0"
})"
// clang-format on
);
ASSERT_EQ(rc, true);

rc = ten_env.init_property_from_json(
// clang-format off
R"({
"_ten": {
"uri": "msgpack://127.0.0.1:8001/",
"log_level": 2,
"long_running_mode": true,
"predefined_graphs": [{
"name": "default",
"auto_start": false,
"singleton": true,
"nodes": [{
"type": "extension",
"name": "predefined_graph",
"app": "msgpack://127.0.0.1:8001/",
"addon": "failed_to_connect_to_remote__predefined_graph_extension",
"extension_group": "failed_to_connect_to_remote__predefined_graph_group"
}]
},{
"name": "graph_1",
"auto_start": false,
"nodes": [{
"type": "extension",
"name": "normal_extension_1",
"app": "msgpack://127.0.0.1:8001/",
"addon": "failed_to_connect_to_remote__normal_extension_1",
"extension_group": "failed_to_connect_to_remote__normal_extension_group"
}, {
"type": "extension",
"name": "normal_extension_2",
"app": "msgpack://127.0.0.1:8888/",
"addon": "failed_to_connect_to_remote__normal_extension_2",
"extension_group": "failed_to_connect_to_remote__normal_extension_group"
}],
"connections": [{
"app": "msgpack://127.0.0.1:8001/",
"extension_group": "failed_to_connect_to_remote__normal_extension_group",
"extension": "normal_extension_1",
"cmd": [{
"name": "hello_world",
"dest": [{
"app": "msgpack://127.0.0.1:8888/",
"extension_group": "failed_to_connect_to_remote__normal_extension_group",
"extension": "normal_extension_2"
}]
}]
}]
}]
}
})"
// clang-format on
);
ASSERT_EQ(rc, true);

ten_env.on_configure_done();
}
};

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

return nullptr;
}

TEN_CPP_REGISTER_ADDON_AS_EXTENSION(
failed_to_connect_to_remote__predefined_graph_extension,
test_predefined_graph);

} // namespace

TEST(ExtensionTest, FailedToConnectToRemote) { // NOLINT
auto *app_1_thread =
ten_thread_create("app thread 1", app_thread_1_main, nullptr);

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

// Do not need to send 'start_graph' command first.
// The 'graph_id' MUST be "default" if we want to send the request to
// predefined graph.
nlohmann::json resp = client->send_json_and_recv_resp_in_json(
R"({
"_ten": {
"name": "test",
"seq_id": "111",
"dest": [{
"app": "msgpack://127.0.0.1:8001/",
"graph": "default",
"extension_group": "failed_to_connect_to_remote__predefined_graph_group",
"extension": "predefined_graph"
}]
}
})"_json);
ten_test::check_result_is(resp, "111", TEN_STATUS_CODE_OK,
R"({"id": 1, "name": "a"})");

delete client;

// Send a close_app command to close the app as the app is running in
// long_running_mode.
ten::msgpack_tcp_client_t::close_app("msgpack://127.0.0.1:8001/");

ten_thread_join(app_1_thread, -1);
}
Loading