ms-runner/tests/transport_test.c

114 lines
3.6 KiB
C

#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;
}