Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat(test): Add test for tests for core funcs
Browse files Browse the repository at this point in the history
Functions that are not covered by `api/driver.c` are covered by
`driver_core.c`. And those common functions are moved from `driver.c`
to `common.h` and `common.c`.
  • Loading branch information
howjmay committed Apr 17, 2020
1 parent eda35e8 commit 5ab3ce5
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 163 deletions.
24 changes: 24 additions & 0 deletions tests/api/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
cc_library(
name = "common",
srcs = ["common.c"],
hdrs = ["common.h"],
deps = [
"//accelerator:ta_config",
"//common",
],
)

cc_test(
name = "driver",
srcs = [
Expand All @@ -7,6 +17,7 @@ cc_test(
"//accelerator/core:apis",
"//accelerator/core:proxy_apis",
"//tests:test_define",
"//tests/api:common",
],
)

Expand All @@ -20,5 +31,18 @@ cc_binary(
"//accelerator/core:apis",
"//accelerator/core:proxy_apis",
"//tests:test_define",
"//tests/api:common",
],
)

cc_test(
name = "driver_core",
srcs = [
"driver_core.c",
],
deps = [
"//accelerator/core",
"//tests:test_define",
"//tests/api:common",
],
)
81 changes: 81 additions & 0 deletions tests/api/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "common.h"
#include "accelerator/cli_info.h"
#include "stdlib.h"

static struct ta_cli_argument_s driver_cli_arguments_g[] = {
{"hash", required_argument, NULL, 'H', "testing transaction hashes"},
{"tag", required_argument, NULL, 'T', "testing transaction tag"},
{NULL, 0, NULL, 0, NULL}};

struct option* driver_cli_build_options() {
int driver_cli_arg_size = sizeof(driver_cli_arguments_g) / sizeof(driver_cli_arguments_g[0]);
struct option* driver_long_options =
(struct option*)realloc(cli_build_options(), (cli_cmd_num + 2) * sizeof(struct option));
for (int i = 0; i < driver_cli_arg_size; i++) {
driver_long_options[(cli_cmd_num - 1) + i].name = driver_cli_arguments_g[i].name;
driver_long_options[(cli_cmd_num - 1) + i].has_arg = driver_cli_arguments_g[i].has_arg;
driver_long_options[(cli_cmd_num - 1) + i].flag = driver_cli_arguments_g[i].flag;
driver_long_options[(cli_cmd_num - 1) + i].val = driver_cli_arguments_g[i].val;
}

return driver_long_options;
}

status_t driver_core_cli_init(ta_core_t* const core, int argc, char** argv, driver_test_cases_t* test_cases) {
int key = 0;
status_t ret = SC_OK;
struct option* long_options = driver_cli_build_options();

while ((key = getopt_long(argc, argv, "hvH:T:", long_options, NULL)) != -1) {
switch (key) {
case 'h':
ta_usage();
exit(EXIT_SUCCESS);
case 'v':
printf("%s\n", TA_VERSION);
exit(EXIT_SUCCESS);
case 'H':
// Take the arguments as testing transaction hashes
for (int i = 0; i < TXN_HASH_NUM; i++) {
if (!test_cases->txn_hash[i]) {
test_cases->txn_hash[i] = optarg;
}
}
break;
case 'T':
// Take the arguments as testing transaction tag
test_cases->tag = optarg;
break;
default:
ret = cli_core_set(core, key, optarg);
break;
}
if (ret != SC_OK) {
break;
}
}

free(long_options);
return ret;
}

static double diff_time(struct timespec start, struct timespec end) {
struct timespec diff;
if (end.tv_nsec - start.tv_nsec < 0) {
diff.tv_sec = end.tv_sec - start.tv_sec - 1;
diff.tv_nsec = end.tv_nsec - start.tv_nsec + 1000000000;
} else {
diff.tv_sec = end.tv_sec - start.tv_sec;
diff.tv_nsec = end.tv_nsec - start.tv_nsec;
}
return (diff.tv_sec + diff.tv_nsec / 1000000000.0);
}

void test_time_end(struct timespec* start, struct timespec* end, double* sum) {
clock_gettime(CLOCK_REALTIME, end);
double difference = diff_time(*start, *end);
#if defined(ENABLE_STAT)
printf("%lf\n", difference);
#endif
*sum += difference;
}
60 changes: 60 additions & 0 deletions tests/api/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2020 BiiLabs Co., Ltd. and Contributors
* All Rights Reserved.
* This is free software; you can redistribute it and/or modify it under the
* terms of the MIT license. A copy of the license can be found in the file
* "LICENSE" at the root of this distribution.
*/

#include <stdio.h>
#include <time.h>
#include "accelerator/config.h"
#include "common/ta_errors.h"

#define TXN_HASH_NUM 2

typedef struct driver_test_cases_s {
char* txn_hash[TXN_HASH_NUM];
char* tag;
} driver_test_cases_t;

/**
* Set option list for `get_long()`
*
* @return
* - pointer of a `struct option` array on success
* - NULL on error
*/
struct option* driver_cli_build_options();

/**
* Initializes configurations with default values for driver tests
*
* @param core[in] Pointer to Tangle-accelerator core configuration structure
* @param argc[in] Pointer to Tangle-accelerator core configuration structure
* @param argv[in] Pointer to Tangle-accelerator core configuration structure
* @param test_cases[in] Pointer to Tangle-accelerator core configuration structure
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t driver_core_cli_init(ta_core_t* const core, int argc, char** argv, driver_test_cases_t* test_cases);

/**
* Start testing clock
*
* @param start[out] `struct timespec` object for recording starting time.
*
*/
static inline void test_time_start(struct timespec* start) { clock_gettime(CLOCK_REALTIME, start); }

/**
* Initializes configurations with default values
*
* @param start[in] `struct timespec` object for recording starting time.
* @param end[out] `struct timespec` object for recording ending time.
* @param start[out] The total elapsing time.
*
*/
void test_time_end(struct timespec* start, struct timespec* end, double* sum);
147 changes: 7 additions & 140 deletions tests/api/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@
#include <time.h>
#include "accelerator/core/apis.h"
#include "accelerator/core/proxy_apis.h"
#include "common.h"
#include "tests/test_define.h"

static driver_test_cases_t test_case;
static ta_core_t ta_core;
struct timespec start_time, end_time;

char* test_txn_hash[2] = {NULL, NULL};
int test_txn_hash_size = sizeof(test_txn_hash) / sizeof(char*);
char* test_tag = NULL;
static struct ta_cli_argument_s driver_cli_arguments_g[] = {
{"hash", required_argument, NULL, 's', "testing transaction hashes"},
{"tag", required_argument, NULL, 'g', "testing transaction tag"},
{NULL, 0, NULL, 0, NULL}};

ta_send_mam_res_t* res;

static struct proxy_apis_s {
const char* name;
const char* json; // args which are passed to IRI API
Expand Down Expand Up @@ -59,29 +51,6 @@ static struct identity_s {
} identities[TEST_COUNT];
#endif

double diff_time(struct timespec start, struct timespec end) {
struct timespec diff;
if (end.tv_nsec - start.tv_nsec < 0) {
diff.tv_sec = end.tv_sec - start.tv_sec - 1;
diff.tv_nsec = end.tv_nsec - start.tv_nsec + 1000000000;
} else {
diff.tv_sec = end.tv_sec - start.tv_sec;
diff.tv_nsec = end.tv_nsec - start.tv_nsec;
}
return (diff.tv_sec + diff.tv_nsec / 1000000000.0);
}

void test_time_start(struct timespec* start) { clock_gettime(CLOCK_REALTIME, start); }

void test_time_end(struct timespec* start, struct timespec* end, double* sum) {
clock_gettime(CLOCK_REALTIME, end);
double difference = diff_time(*start, *end);
#if defined(ENABLE_STAT)
printf("%lf\n", difference);
#endif
*sum += difference;
}

void test_generate_address(void) {
char* json_result;
double sum = 0;
Expand Down Expand Up @@ -178,7 +147,7 @@ void test_find_transaction_objects(void) {
const char* pre_json = "{\"hashes\":[\"%s\",\"%s\"]}";
int json_len = (NUM_TRYTES_HASH + 1) * 2 + strlen(pre_json) + 1;
char* json = (char*)malloc(sizeof(char) * json_len);
snprintf(json, json_len, pre_json, test_txn_hash[0], test_txn_hash[1]);
snprintf(json, json_len, pre_json, test_case.txn_hash[0], test_case.txn_hash[1]);
char* json_result;
double sum = 0;

Expand All @@ -199,7 +168,7 @@ void test_find_transactions_by_tag(void) {
for (size_t count = 0; count < TEST_COUNT; count++) {
test_time_start(&start_time);

TEST_ASSERT_EQUAL_INT32(SC_OK, api_find_transactions_by_tag(&ta_core.iota_service, test_tag, &json_result));
TEST_ASSERT_EQUAL_INT32(SC_OK, api_find_transactions_by_tag(&ta_core.iota_service, test_case.tag, &json_result));
test_time_end(&start_time, &end_time, &sum);
free(json_result);
}
Expand Down Expand Up @@ -257,58 +226,14 @@ void test_find_transactions_obj_by_tag(void) {
for (size_t count = 0; count < TEST_COUNT; count++) {
test_time_start(&start_time);

TEST_ASSERT_EQUAL_INT32(SC_OK, api_find_transactions_obj_by_tag(&ta_core.iota_service, test_tag, &json_result));
TEST_ASSERT_EQUAL_INT32(SC_OK,
api_find_transactions_obj_by_tag(&ta_core.iota_service, test_case.tag, &json_result));
test_time_end(&start_time, &end_time, &sum);
free(json_result);
}
printf("Average time of find_tx_obj_by_tag: %lf\n", sum / TEST_COUNT);
}

void test_send_mam_message(void) {
double sum = 0;
const char* json =
"{\"seed\":\"" TRYTES_81_1
"\",\"channel_ord\":" STR(TEST_CHANNEL_ORD) ",\"message\":\"" TEST_PAYLOAD "\",\"ch_mss_depth\":" STR(
TEST_CH_DEPTH) ",\"ep_mss_depth\":" STR(TEST_EP_DEPTH) ",\"ntru_pk\":\"" TEST_NTRU_PK
"\",\"psk\":\"" TRYTES_81_2 "\"}";
char* json_result = NULL;
res = send_mam_res_new();
status_t ret = SC_OK;
bool result = false;

for (size_t count = 0; count < TEST_COUNT; count++) {
test_time_start(&start_time);
ret = api_send_mam_message(&ta_core.ta_conf, &ta_core.iota_conf, &ta_core.iota_service, json, &json_result);
if (ret == SC_OK || ret == SC_MAM_ALL_MSS_KEYS_USED) {
result = true;
}
TEST_ASSERT_TRUE(result);

send_mam_res_deserialize(json_result, res);

test_time_end(&start_time, &end_time, &sum);
free(json_result);
json_result = NULL;
}
free(json_result);
printf("Average time of send_mam_message: %lf\n", sum / TEST_COUNT);
}

void test_receive_mam_message(void) {
char* json_result;
double sum = 0;

for (size_t count = 0; count < TEST_COUNT; count++) {
test_time_start(&start_time);

TEST_ASSERT_EQUAL_INT32(
SC_OK, api_recv_mam_message(&ta_core.iota_conf, &ta_core.iota_service, (char*)res->chid, &json_result));
test_time_end(&start_time, &end_time, &sum);
free(json_result);
}
printf("Average time of receive_mam_message: %lf\n", sum / TEST_COUNT);
}

void test_get_iri_status() {
char* json_result;
double sum = 0;
Expand Down Expand Up @@ -338,63 +263,6 @@ void test_proxy_apis() {
printf("Average time of %s: %lf\n", proxy_apis_g[i].name, sum / TEST_COUNT);
}
}
static inline struct option* driver_cli_build_options() {
int driver_cli_arg_size = sizeof(driver_cli_arguments_g) / sizeof(driver_cli_arguments_g[0]);
struct option* driver_long_options =
(struct option*)realloc(cli_build_options(), (cli_cmd_num + 2) * sizeof(struct option));
for (int i = 0; i < driver_cli_arg_size; i++) {
driver_long_options[(cli_cmd_num - 1) + i].name = driver_cli_arguments_g[i].name;
driver_long_options[(cli_cmd_num - 1) + i].has_arg = driver_cli_arguments_g[i].has_arg;
driver_long_options[(cli_cmd_num - 1) + i].flag = driver_cli_arguments_g[i].flag;
driver_long_options[(cli_cmd_num - 1) + i].val = driver_cli_arguments_g[i].val;
}

return driver_long_options;
};
status_t driver_core_cli_init(ta_core_t* const core, int argc, char** argv) {
int key = 0;
status_t ret = SC_OK;
struct option* long_options = driver_cli_build_options();

while ((key = getopt_long(argc, argv, "hvs:g:", long_options, NULL)) != -1) {
switch (key) {
case 'h':
ta_usage();
exit(EXIT_SUCCESS);
case 'v':
printf("%s\n", TA_VERSION);
exit(EXIT_SUCCESS);
case QUIET:
// Turn on quiet mode
quiet_mode = true;

// Enable backend_redis logger
br_logger_init();
break;
case 's':
// Take the arguments as testing transaction hashes
for (int i = 0; i < test_txn_hash_size; i++) {
if (!test_txn_hash[i]) {
test_txn_hash[i] = optarg;
}
}
break;
case 'g':
// Take the arguments as testing transaction tag
test_tag = optarg;
break;
default:
ret = cli_core_set(core, key, optarg);
break;
}
if (ret != SC_OK) {
break;
}
}

free(long_options);
return ret;
}

int main(int argc, char* argv[]) {
UNITY_BEGIN();
Expand All @@ -409,11 +277,10 @@ int main(int argc, char* argv[]) {
timer_logger_init();

ta_core_default_init(&ta_core);
driver_core_cli_init(&ta_core, argc, argv);
driver_core_cli_init(&ta_core, argc, argv, &test_case);
ta_core_set(&ta_core);

printf("Total samples for each API test: %d\n", TEST_COUNT);

RUN_TEST(test_generate_address);
RUN_TEST(test_get_tips_pair);
RUN_TEST(test_get_tips);
Expand Down
Loading

0 comments on commit 5ab3ce5

Please sign in to comment.