From 3dd226981d4f24842602f15ee17a9c9e5b824207 Mon Sep 17 00:00:00 2001 From: Edgar Brissow Date: Wed, 29 Jan 2025 13:13:49 -0300 Subject: [PATCH] Tests building --- src/cpp/link_creation_agent/agent.cc | 45 ++++--- src/cpp/link_creation_agent/agent.h | 20 ++-- src/cpp/link_creation_agent/compose.yaml | 140 +++++++--------------- src/cpp/tests/link_creation_agent_test.cc | 33 +++-- src/scripts/bazel_test.sh | 2 +- 5 files changed, 103 insertions(+), 137 deletions(-) diff --git a/src/cpp/link_creation_agent/agent.cc b/src/cpp/link_creation_agent/agent.cc index 7b71d43..97b8486 100644 --- a/src/cpp/link_creation_agent/agent.cc +++ b/src/cpp/link_creation_agent/agent.cc @@ -13,10 +13,10 @@ using namespace query_element; LinkCreationAgent::LinkCreationAgent(string config_path) { this->config_path = config_path; load_config(); - link_creation_node_server = new LinkCreationNode(link_creation_server_id); - query_node_client = new DASNode(query_node_client_id, query_node_server_id); - service = new LinkCreationService(thread_count); - das_client = new DasAgentNode(das_client_id, query_node_server_id); + link_creation_node_server = new LinkCreationNode(link_creation_agent_server_id); + query_node_client = new DASNode(query_agent_client_id, query_agent_server_id); + service = new LinkCreationService(link_creation_agent_thread_count); + das_client = new DasAgentNode(das_agent_client_id, das_agent_server_id); this->agent_thread = new thread(&LinkCreationAgent::run, this); } @@ -30,6 +30,12 @@ LinkCreationAgent::~LinkCreationAgent() { } void LinkCreationAgent::stop() { + agent_mutex.lock(); + if(!is_stoping){ + is_stoping = true; + save_buffer(); + } + agent_mutex.unlock(); link_creation_node_server->graceful_shutdown(); query_node_client->graceful_shutdown(); das_client->graceful_shutdown(); @@ -42,12 +48,13 @@ void LinkCreationAgent::stop() { void LinkCreationAgent::run() { int current_buffer_position = 0; while (true) { + if(is_stoping) break; LinkCreationAgentRequest* lca_request = NULL; bool is_from_buffer = false; if (!link_creation_node_server->is_query_empty()) { vector request = link_creation_node_server->pop_request(); lca_request = create_request(request); - lca_request->current_interval = default_interval; + lca_request->current_interval = requests_interval_seconds; if (lca_request != NULL && (lca_request->infinite || lca_request->repeat > 0)) { request_buffer.push_back(*lca_request); } @@ -62,7 +69,7 @@ void LinkCreationAgent::run() { if (lca_request == NULL || lca_request->last_execution + lca_request->current_interval > time(0)) { - this_thread::sleep_for(chrono::seconds(loop_interval)); + this_thread::sleep_for(chrono::milliseconds(loop_interval)); continue; } @@ -109,20 +116,22 @@ void LinkCreationAgent::load_config() { if (getline(is_line, value)) { value.erase(remove(value.begin(), value.end(), ' '), value.end()); key.erase(remove(key.begin(), key.end(), ' '), key.end()); - if (key == "default_interval") - this->default_interval = stoi(value); - else if (key == "thread_count") - this->thread_count = stoi(value); - else if (key == "query_node_client_id") - this->query_node_client_id = value; - else if (key == "query_node_server_id") - this->query_node_server_id = value; - else if (key == "link_creation_server_id") - this->link_creation_server_id = value; - else if (key == "das_client_id") - this->das_client_id = value; + if (key == "requests_interval_seconds") + this->requests_interval_seconds = stoi(value); + else if (key == "link_creation_agent_thread_count") + this->link_creation_agent_thread_count = stoi(value); + else if (key == "query_agent_client_id") + this->query_agent_client_id = value; + else if (key == "query_agent_server_id") + this->query_agent_server_id = value; + else if (key == "link_creation_agent_server_id") + this->link_creation_agent_server_id = value; + else if (key == "das_agent_client_id") + this->das_agent_client_id = value; else if (key == "requests_buffer_file") this->requests_buffer_file = value; + else if (key == "das_agent_server_id") + this->das_agent_server_id = value; else if (key == "context") this->context = value; } diff --git a/src/cpp/link_creation_agent/agent.h b/src/cpp/link_creation_agent/agent.h index 0be3c13..1e95281 100644 --- a/src/cpp/link_creation_agent/agent.h +++ b/src/cpp/link_creation_agent/agent.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "DASNode.h" #include "RemoteIterator.h" @@ -81,8 +82,6 @@ class LinkCreationAgent { * @brief Load all requests that have the infinite value set as true from the disk or DB. */ void load_buffer(); - - /** * @brief Stop the agent */ @@ -90,12 +89,13 @@ class LinkCreationAgent { // Attributes loaded from config file string config_path; // Path to the configuration file - int default_interval; // Default interval to send requests - int thread_count; // Number of threads to process requests - string query_node_client_id; // ID of the query node client - string query_node_server_id; // ID of the query node server - string link_creation_server_id; // ID of the link creation server - string das_client_id; // ID of the DAS client + int requests_interval_seconds; // Default interval to send requests + int link_creation_agent_thread_count; // Number of threads to process requests + string query_agent_client_id; // ID of the query node client + string query_agent_server_id; // ID of the query node server + string link_creation_agent_server_id; // ID of the link creation server + string das_agent_client_id; // ID of the DAS client + string das_agent_server_id; string requests_buffer_file; // Path to the requests buffer file string context; // Context to send to attention broker @@ -106,6 +106,8 @@ class LinkCreationAgent { LinkCreationNode* link_creation_node_server; das::DasAgentNode* das_client; thread* agent_thread; - int loop_interval = 1; + mutex agent_mutex; + bool is_stoping = false; + int loop_interval = 100; // miliseconds }; } // namespace link_creation_agent diff --git a/src/cpp/link_creation_agent/compose.yaml b/src/cpp/link_creation_agent/compose.yaml index 7de2aa6..a6aacfb 100644 --- a/src/cpp/link_creation_agent/compose.yaml +++ b/src/cpp/link_creation_agent/compose.yaml @@ -7,14 +7,12 @@ services: ports: - 6379:6379 healthcheck: - test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ] + test: ["CMD", "redis-cli", "--raw", "incr", "ping"] interval: 10s timeout: 10s retries: 5 start_period: 40s network_mode: host - # networks: - # - app_network mongodb: image: mongo:latest @@ -41,10 +39,6 @@ services: retries: 5 start_period: 40s network_mode: host - - # networks: - # - app_network - metta-parser: image: trueagi/das:0.5.4-metta-parser @@ -71,21 +65,11 @@ services: restart: on-failure network_mode: host - # networks: - # - app_network - - das-attention-broker: - # How to build: - # Clone the das-node repository - # Run: - # ./scripts/docker_image_build.sh - # build: ./docker_das_node working_dir: /opt image: das-attention-broker-builder container_name: test-das-attention-broker command: ./bin/attention_broker_service 37007 - # command: sleep infinity ports: - 37007:37007 volumes: @@ -104,85 +88,47 @@ services: DAS_MONGODB_USERNAME: root DAS_MONGODB_PASSWORD: root network_mode: host - - # networks: - # - app_network - - - # das-query-agent: - # # How to build: - # # Clone the das-node repository - # # Run: - # # ./scripts/docker_image_build.sh - # # build: ./docker_das_node - # working_dir: /opt - # image: das-attention-broker-builder - # container_name: test-das-query-agent-node - # command: ./bin/query_broker 35700 - # # command: sleep infinity - # ports: - # - 35700:35700 - # # - 6379:6379 - # volumes: - # - ../../../src:/opt - # restart: on-failure - # depends_on: - # - mongodb - # - redis - # - metta-parser - # - das-attention-broker - # environment: - # DAS_MONGODB_NAME: das - # DAS_MONGODB_HOSTNAME: localhost - # DAS_MONGODB_PORT: 27017 - # DAS_REDIS_HOSTNAME: 0.0.0.0 - # DAS_REDIS_PORT: 6379 - # DAS_MONGODB_USERNAME: root - # DAS_MONGODB_PASSWORD: root - # network_mode: host - - # networks: - # - app_network - - - # das-link-create-agent: - # # How to build: - # # Clone the das-node repository - # # Run: - # # ./scripts/docker_image_build.sh - # # build: ./docker_das_node - # image: das-attention-broker-builder - # container_name: test-das-link-create-agent-node - # working_dir: /opt - # command: ./bin/link_creation_server --type server --config_file /tmp/config - # # command: sleep infinity - # volumes: - # - ./data:/tmp - # - ../../../src:/opt - # ports: - # # - 35700:35700 - # - 9090:9090 - # restart: on-failure - # depends_on: - # - mongodb - # - redis - # - metta-parser - # - das-attention-broker - # - das-query-agent - # # environment: - # # DAS_MONGODB_NAME: das - # # DAS_MONGODB_HOSTNAME: localhost - # # DAS_MONGODB_PORT: 27017 - # # DAS_REDIS_HOSTNAME: 0.0.0.0 - # # DAS_REDIS_PORT: 6378 - # # DAS_MONGODB_USERNAME: root - # # DAS_MONGODB_PASSWORD: root - # # networks: - # # - app_network - # network_mode: host - + das-query-agent: + working_dir: /opt + image: das-attention-broker-builder + container_name: test-das-query-agent-node + command: ./bin/query_broker 35700 + ports: + - 35700:35700 + volumes: + - ../../../src:/opt + restart: on-failure + depends_on: + - mongodb + - redis + - metta-parser + - das-attention-broker + environment: + DAS_MONGODB_NAME: das + DAS_MONGODB_HOSTNAME: localhost + DAS_MONGODB_PORT: 27017 + DAS_REDIS_HOSTNAME: 0.0.0.0 + DAS_REDIS_PORT: 6379 + DAS_MONGODB_USERNAME: root + DAS_MONGODB_PASSWORD: root + network_mode: host -# networks: -# app_network: -# driver: bridge \ No newline at end of file + das-link-create-agent: + image: das-attention-broker-builder + container_name: test-das-link-create-agent-node + working_dir: /opt + command: ./bin/link_creation_server --type server --config_file /tmp/config + volumes: + - ./data:/tmp + - ../../../src:/opt + ports: + - 9090:9090 + restart: on-failure + depends_on: + - mongodb + - redis + - metta-parser + - das-attention-broker + - das-query-agent + network_mode: host diff --git a/src/cpp/tests/link_creation_agent_test.cc b/src/cpp/tests/link_creation_agent_test.cc index cfe6b05..30e85b0 100644 --- a/src/cpp/tests/link_creation_agent_test.cc +++ b/src/cpp/tests/link_creation_agent_test.cc @@ -1,6 +1,7 @@ #include "agent.h" #include #include +#include #include using namespace std; @@ -11,23 +12,20 @@ class LinkCreationAgentTest : public ::testing::Test { LinkCreationAgent* agent; void SetUp() override { - // Create a temporary config file for testing + // Create a temporary config file for testing ofstream config_file("test_config.cfg"); - config_file << "default_interval=1\n"; - config_file << "thread_count=1\n"; - config_file << "query_node_client_id=test_client_id\n"; - config_file << "query_node_server_id=test_server_id\n"; - config_file << "link_creation_server_id=test_link_creation_server_id\n"; - config_file << "das_client_id=test_das_client_id\n"; + config_file << "requests_interval_seconds=1\n"; + config_file << "link_creation_agent_thread_count=1\n"; + config_file << "query_agent_client_id=localhost:8080\n"; + config_file << "query_agent_server_id=localhost:8081\n"; + config_file << "link_creation_agent_server_id=localhost:8082\n"; + config_file << "das_agent_client_id=localhost:8083\n"; + config_file << "das_agent_server_id=localhost:8083\n"; config_file << "requests_buffer_file=test_buffer.bin\n"; - config_file << "context=test_context\n"; config_file.close(); - - agent = new LinkCreationAgent("test_config.cfg"); } void TearDown() override { - delete agent; remove("test_config.cfg"); remove("test_buffer.bin"); } @@ -35,7 +33,7 @@ class LinkCreationAgentTest : public ::testing::Test { -TEST_F(LinkCreationAgentTest, TestRun) { +TEST_F(LinkCreationAgentTest, TestRequest) { // Simulate a request vector request = {"query1", "LINK_CREATE", "query2", "10", "5", "test_context", "true"}; LinkCreationAgentRequest* lca_request = LinkCreationAgent::create_request(request); @@ -45,7 +43,18 @@ TEST_F(LinkCreationAgentTest, TestRun) { EXPECT_EQ(lca_request->repeat, 5); EXPECT_EQ(lca_request->context, "test_context"); EXPECT_EQ(lca_request->update_attention_broker, true); +} + + +TEST_F(LinkCreationAgentTest, TestConfig){ + agent = new LinkCreationAgent("test_config.cfg"); + delete agent; +} +TEST_F(LinkCreationAgentTest, TestOutputBuffer){ + agent = new LinkCreationAgent("test_config.cfg"); + delete agent; } + diff --git a/src/scripts/bazel_test.sh b/src/scripts/bazel_test.sh index d946bb5..d7f7290 100755 --- a/src/scripts/bazel_test.sh +++ b/src/scripts/bazel_test.sh @@ -2,7 +2,7 @@ (( JOBS=$(nproc)/2 )) BAZELISK_CMD=/opt/bazel/bazelisk -BAZELISK_TEST_CMD="${BAZELISK_CMD} test --jobs ${JOBS} --enable_bzlmod --test_output=errors" +BAZELISK_TEST_CMD="${BAZELISK_CMD} test --jobs ${JOBS} --enable_bzlmod --test_output=errors --build_tests_only" cd $WORKSPACE_DIR \ && $BAZELISK_TEST_CMD //...