add tests, initial protobuf and rabbitmq support, ...

This commit is contained in:
dragonmuffin 2025-03-04 22:03:00 +05:00
parent 54adfcee2a
commit 7510c2a7b3
13 changed files with 421 additions and 39 deletions

11
tests/Makefile Normal file
View file

@ -0,0 +1,11 @@
.PHONY: test_all
test: tests/resource_handler_test tests/transport_test
tests/resource_handler_test: resource_handler/resource_handler.h resource_handler/resource_handler.c tests/resource_handler_test.c
$(CC) -o $@ resource_handler/resource_handler.c tests/resource_handler_test.c -I . -g -fsanitize=address -fsanitize=leak -fno-omit-frame-pointer
tests/transport_test: transport/transport.c transport/transport.h transport/utils.c tests/transport_test.c
$(CC) -o $@ gen/runner/v1/runner.pb-c.c tests/transport_test.c -I . -I gen -g -fsanitize=address -fsanitize=leak -fno-omit-frame-pointer -lrabbitmq -lprotobuf-c
test_all:
echo 1

BIN
tests/resource_handler_test Executable file

Binary file not shown.

View file

@ -0,0 +1,38 @@
#include <string.h>
#include <stdio.h>
#include "resource_handler/resource_handler.h"
void check_extract_numbers(char *input, int correct_len, int *correct_buf) {
int test_buf[MAX_CORES];
int test_len = extract_numbers(input, test_buf);
//int correct_buf[] = {1,2,3,4,5,7,8};
//int correct_len = 7;
if(test_len==correct_len && memcmp(test_buf, correct_buf, test_len)==0) {
printf("test on string \"%s\" passed\n", input);
} else {
printf("test on string \"%s\" failed:\ncorrect len: %d,got len: %d\ngot result:", input, correct_len, test_len);
for(int i=0;i<test_len;i++) {
printf("%d ", *(test_buf+i));
}
printf("\n");
}
}
int main() {
int res1[]={1,2,3,4,5,7,8};
check_extract_numbers("1-5,7-8", 7, res1);
int res2[]={1};
check_extract_numbers("1", 1, res2);
int res3[]={};
check_extract_numbers("", 0, res3);
int* cores;
int num_cores;
num_cores=get_isolated_cores(&cores);
printf("your system has %d isolated cores: ", num_cores);
for(int i = 0; i < num_cores; i++) {
printf("%d,", *(cores+i));
}
printf("\n");
free(cores);
return 0;
}

BIN
tests/transport_test Executable file

Binary file not shown.

114
tests/transport_test.c Normal file
View file

@ -0,0 +1,114 @@
#define _GNU_SOURCE
#include "gen/runner/v1/runner.pb-c.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rabbitmq-c/amqp.h>
#include <rabbitmq-c/tcp_socket.h>
struct connection_data {
char const *hostname;
int port;
char const *exchange;
char const *routingkey;
amqp_socket_t *socket;
amqp_connection_state_t conn;
};
void prepare_connection(int argc, char const *const *argv, struct connection_data *condata) {
condata->socket=NULL;
int status;
if (argc < 5) {
fprintf(stderr, "Usage: transport_test host port exchange routingkey\n");
exit(1);
}
condata->hostname = argv[1];
condata->port = atoi(argv[2]);
condata->exchange = argv[3];
condata->routingkey = argv[4];
condata->conn = amqp_new_connection();
condata->socket = amqp_tcp_socket_new(condata->conn);
status = amqp_socket_open(condata->socket, condata->hostname, condata->port);
amqp_login(condata->conn, "/", 0, AMQP_DEFAULT_FRAME_SIZE, 0, AMQP_SASL_METHOD_PLAIN, "rmuser", "rmpassword"), "Logging in";
amqp_channel_open(condata->conn, 1);
amqp_get_rpc_reply(condata->conn), "Opening channel";
}
void send_to_rbmq(struct connection_data condata, char const* msg)
{
amqp_basic_properties_t props;
props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
props.content_type = amqp_cstring_bytes("text/plain");
props.delivery_mode = 2;
amqp_basic_publish(condata.conn, 1, amqp_cstring_bytes(condata.exchange), amqp_cstring_bytes(condata.routingkey), 0, 0, &props, amqp_cstring_bytes(msg)), "Publishing";
}
void build_serialize(int32_t solution_id, char* binding_key, int32_t language, char* solution, void** buf) {
struct Runner__V1__Build build = RUNNER__V1__BUILD__INIT;
build.solution_id = solution_id;
build.binding_key = binding_key;
build.language = language;
build.solution = solution;
struct Runner__V1__Instruction inst = RUNNER__V1__INSTRUCTION__INIT;
inst.instruction_case = RUNNER__V1__INSTRUCTION__INSTRUCTION_BUILD;
inst.build = &build;
size_t len=runner__v1__instruction__get_packed_size(&inst);
*buf=malloc(len+1);
memset(*buf,0,len+1);
runner__v1__instruction__pack(&inst, *buf);
}
void run_serialize(int32_t solution_id, int32_t test_id, char* binding_key, void** buf) {
struct Runner__V1__Run run= RUNNER__V1__RUN__INIT;
run.solution_id = solution_id;
run.test_id = test_id;
run.binding_key = binding_key;
struct Runner__V1__Instruction inst = RUNNER__V1__INSTRUCTION__INIT;
inst.instruction_case = RUNNER__V1__INSTRUCTION__INSTRUCTION_RUN;
inst.run = &run;
size_t len=runner__v1__instruction__get_packed_size(&inst);
*buf=malloc(len+1);
memset(*buf,0,len+1);
runner__v1__instruction__pack(&inst, *buf);
}
int main(int argc, char const *const *argv) {
struct connection_data condata;
prepare_connection(argc,argv,&condata);
void *messagebody;
char* binding_key;
asprintf(&binding_key,"bktest");
char* solution;
asprintf(&solution,"soltest");
build_serialize(0,binding_key,1,solution,&messagebody);
free(binding_key);
free(solution);
send_to_rbmq(condata,messagebody);
free(messagebody);
asprintf(&binding_key,"bktest");
run_serialize(0,1,binding_key,&messagebody);
free(binding_key);
send_to_rbmq(condata,messagebody);
free(messagebody);
/* closing connection */
amqp_channel_close(condata.conn, 1, AMQP_REPLY_SUCCESS), "Closing channel";
amqp_connection_close(condata.conn, AMQP_REPLY_SUCCESS), "Closing connection";
amqp_destroy_connection(condata.conn), "Ending connection";
return 0;
}