Skip to content

Commit

Permalink
Tests building
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiebrissow committed Jan 29, 2025
1 parent c7493ae commit 3dd2269
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 137 deletions.
45 changes: 27 additions & 18 deletions src/cpp/link_creation_agent/agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
Expand All @@ -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<string> 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);
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 11 additions & 9 deletions src/cpp/link_creation_agent/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>
#include <thread>
#include <vector>
#include <mutex>

#include "DASNode.h"
#include "RemoteIterator.h"
Expand Down Expand Up @@ -81,21 +82,20 @@ 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
*/
void stop();

// 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

Expand All @@ -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
140 changes: 43 additions & 97 deletions src/cpp/link_creation_agent/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
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
33 changes: 21 additions & 12 deletions src/cpp/tests/link_creation_agent_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "agent.h"
#include <gtest/gtest.h>
#include <thread>
#include <fstream>
#include <chrono>

using namespace std;
Expand All @@ -11,31 +12,28 @@ 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");
}
};



TEST_F(LinkCreationAgentTest, TestRun) {
TEST_F(LinkCreationAgentTest, TestRequest) {
// Simulate a request
vector<string> request = {"query1", "LINK_CREATE", "query2", "10", "5", "test_context", "true"};
LinkCreationAgentRequest* lca_request = LinkCreationAgent::create_request(request);
Expand All @@ -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;
}


2 changes: 1 addition & 1 deletion src/scripts/bazel_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 //...
Expand Down

0 comments on commit 3dd2269

Please sign in to comment.