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

Commit

Permalink
fix(driver): Redesign test driver
Browse files Browse the repository at this point in the history
In order to react to the situation that transactions' record will disapear,
after snapshot (both global one and local one), the order of test in driver
is redesigned.
Send transaction APIs are ahead of recieve transaction APIs, and the infomation
that are used in the recieve transaction APIs (i.e. tag, bundle hash) are
come from send transaction APIs.
  • Loading branch information
howjmay committed Apr 18, 2019
1 parent c8f238d commit 16cea5a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
4 changes: 4 additions & 0 deletions accelerator/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ typedef enum {
/**< flex_trits conversion error */
SC_CCLIENT_HASH = 0x06 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
/**< hash container operation error */
SC_CCLIENT_JSON_KEY = 0x07 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
/**< JSON key not found */
SC_CCLIENT_JSON_PARSE = 0x08 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
/**< json parsing error, might the wrong format */

// Serializer module
SC_SERIALIZER_JSON_CREATE = 0x01 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL,
Expand Down
9 changes: 6 additions & 3 deletions serializer/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,20 @@ static status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
static status_t ta_json_get_string(cJSON const* const json_obj,
char const* const obj_name,
char* const text) {
retcode_t ret = SC_OK;
status_t ret = SC_OK;
if (json_obj == NULL || obj_name == NULL || text == NULL) {
return SC_SERIALIZER_NULL;
}

cJSON* json_value = cJSON_GetObjectItemCaseSensitive(json_obj, obj_name);
if (json_value == NULL) {
return RC_CCLIENT_JSON_KEY;
return SC_CCLIENT_JSON_KEY;
}

if (cJSON_IsString(json_value) && (json_value->valuestring != NULL)) {
strcpy(text, json_value->valuestring);
} else {
return RC_CCLIENT_JSON_PARSE;
return SC_CCLIENT_JSON_PARSE;
}

return ret;
Expand Down
39 changes: 30 additions & 9 deletions tests/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
static ta_core_t ta_core;
struct timespec start_time, end_time;

char driver_tag_msg[NUM_TRYTES_TAG];
send_mam_res_t* res;

#if defined(ENABLE_STAT)
#define TEST_COUNT 100
#else
#define TEST_COUNT 1
#endif

static void gen_rand_tag(char* tag) {
const char tryte_alpahbet[] = "NOPQRSTUVWXYZ9ABCDEFGHIJKLM";

for (int i = 0; i < NUM_TRYTES_TAG; i++) {
tag[i] = tryte_alpahbet[rand() % 27];
}
}

double diff_time(struct timespec start, struct timespec end) {
struct timespec diff;
if (end.tv_nsec - start.tv_nsec < 0) {
Expand Down Expand Up @@ -81,14 +92,19 @@ void test_get_tips(void) {
}

void test_send_transfer(void) {
const char* json =
const char* pre_json =
"{\"value\":100,"
"\"message\":\"" TAG_MSG "\",\"tag\":\"" TAG_MSG
"\","
"\"message\":\"" TAG_MSG
"\",\"tag\":\"%s\","
"\"address\":\"" TRYTES_81_1 "\"}";
char* json_result;
double sum = 0;

gen_rand_tag(driver_tag_msg);
int json_len = strlen(pre_json);
char json[json_len + NUM_TRYTES_TAG];
sprintf(json, pre_json, driver_tag_msg);

for (size_t count = 0; count < TEST_COUNT; count++) {
test_time_start(&start_time);
TEST_ASSERT_EQUAL_INT32(
Expand Down Expand Up @@ -123,7 +139,7 @@ void test_find_transactions_by_tag(void) {
test_time_start(&start_time);

TEST_ASSERT_EQUAL_INT32(
SC_OK, api_find_transactions_by_tag(&ta_core.service, FIND_TAG_MSG,
SC_OK, api_find_transactions_by_tag(&ta_core.service, driver_tag_msg,
&json_result));
test_time_end(&start_time, &end_time, &sum);
free(json_result);
Expand All @@ -139,8 +155,8 @@ void test_find_transactions_obj_by_tag(void) {
test_time_start(&start_time);

TEST_ASSERT_EQUAL_INT32(
SC_OK, api_find_transactions_obj_by_tag(&ta_core.service, FIND_TAG_MSG,
&json_result));
SC_OK, api_find_transactions_obj_by_tag(&ta_core.service,
driver_tag_msg, &json_result));
test_time_end(&start_time, &end_time, &sum);
free(json_result);
}
Expand All @@ -151,12 +167,15 @@ void test_send_mam_message(void) {
double sum = 0;
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
char* json_result;
res = send_mam_res_new();

for (size_t count = 0; count < TEST_COUNT; count++) {
test_time_start(&start_time);
TEST_ASSERT_EQUAL_INT32(
SC_OK, api_mam_send_message(&ta_core.tangle, &ta_core.service, json,
&json_result));
send_mam_res_deserialize(json_result, res);

test_time_end(&start_time, &end_time, &sum);
free(json_result);
}
Expand All @@ -171,15 +190,17 @@ void test_receive_mam_message(void) {
test_time_start(&start_time);

TEST_ASSERT_EQUAL_INT32(
SC_OK, api_receive_mam_message(&ta_core.service, TEST_BUNDLE_HASH,
&json_result));
SC_OK, api_receive_mam_message(&ta_core.service,
(char*)res->bundle_hash, &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);
}

int main(void) {
srand(time(NULL));

UNITY_BEGIN();

ta_config_default_init(&ta_core.info, &ta_core.tangle, &ta_core.cache,
Expand All @@ -194,8 +215,8 @@ int main(void) {
RUN_TEST(test_get_transaction_object);
RUN_TEST(test_find_transactions_by_tag);
RUN_TEST(test_find_transactions_obj_by_tag);
RUN_TEST(test_receive_mam_message);
RUN_TEST(test_send_mam_message);
RUN_TEST(test_receive_mam_message);
ta_config_destroy(&ta_core.service);
return UNITY_END();
}

0 comments on commit 16cea5a

Please sign in to comment.