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

Commit 16cea5a

Browse files
committed
fix(driver): Redesign test driver
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.
1 parent c8f238d commit 16cea5a

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

accelerator/errors.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ typedef enum {
7575
/**< flex_trits conversion error */
7676
SC_CCLIENT_HASH = 0x06 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
7777
/**< hash container operation error */
78+
SC_CCLIENT_JSON_KEY = 0x07 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
79+
/**< JSON key not found */
80+
SC_CCLIENT_JSON_PARSE = 0x08 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
81+
/**< json parsing error, might the wrong format */
7882

7983
// Serializer module
8084
SC_SERIALIZER_JSON_CREATE = 0x01 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL,

serializer/serializer.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,20 @@ static status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
7878
static status_t ta_json_get_string(cJSON const* const json_obj,
7979
char const* const obj_name,
8080
char* const text) {
81-
retcode_t ret = SC_OK;
81+
status_t ret = SC_OK;
82+
if (json_obj == NULL || obj_name == NULL || text == NULL) {
83+
return SC_SERIALIZER_NULL;
84+
}
8285

8386
cJSON* json_value = cJSON_GetObjectItemCaseSensitive(json_obj, obj_name);
8487
if (json_value == NULL) {
85-
return RC_CCLIENT_JSON_KEY;
88+
return SC_CCLIENT_JSON_KEY;
8689
}
8790

8891
if (cJSON_IsString(json_value) && (json_value->valuestring != NULL)) {
8992
strcpy(text, json_value->valuestring);
9093
} else {
91-
return RC_CCLIENT_JSON_PARSE;
94+
return SC_CCLIENT_JSON_PARSE;
9295
}
9396

9497
return ret;

tests/driver.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
static ta_core_t ta_core;
66
struct timespec start_time, end_time;
77

8+
char driver_tag_msg[NUM_TRYTES_TAG];
9+
send_mam_res_t* res;
10+
811
#if defined(ENABLE_STAT)
912
#define TEST_COUNT 100
1013
#else
1114
#define TEST_COUNT 1
1215
#endif
1316

17+
static void gen_rand_tag(char* tag) {
18+
const char tryte_alpahbet[] = "NOPQRSTUVWXYZ9ABCDEFGHIJKLM";
19+
20+
for (int i = 0; i < NUM_TRYTES_TAG; i++) {
21+
tag[i] = tryte_alpahbet[rand() % 27];
22+
}
23+
}
24+
1425
double diff_time(struct timespec start, struct timespec end) {
1526
struct timespec diff;
1627
if (end.tv_nsec - start.tv_nsec < 0) {
@@ -81,14 +92,19 @@ void test_get_tips(void) {
8192
}
8293

8394
void test_send_transfer(void) {
84-
const char* json =
95+
const char* pre_json =
8596
"{\"value\":100,"
86-
"\"message\":\"" TAG_MSG "\",\"tag\":\"" TAG_MSG
87-
"\","
97+
"\"message\":\"" TAG_MSG
98+
"\",\"tag\":\"%s\","
8899
"\"address\":\"" TRYTES_81_1 "\"}";
89100
char* json_result;
90101
double sum = 0;
91102

103+
gen_rand_tag(driver_tag_msg);
104+
int json_len = strlen(pre_json);
105+
char json[json_len + NUM_TRYTES_TAG];
106+
sprintf(json, pre_json, driver_tag_msg);
107+
92108
for (size_t count = 0; count < TEST_COUNT; count++) {
93109
test_time_start(&start_time);
94110
TEST_ASSERT_EQUAL_INT32(
@@ -123,7 +139,7 @@ void test_find_transactions_by_tag(void) {
123139
test_time_start(&start_time);
124140

125141
TEST_ASSERT_EQUAL_INT32(
126-
SC_OK, api_find_transactions_by_tag(&ta_core.service, FIND_TAG_MSG,
142+
SC_OK, api_find_transactions_by_tag(&ta_core.service, driver_tag_msg,
127143
&json_result));
128144
test_time_end(&start_time, &end_time, &sum);
129145
free(json_result);
@@ -139,8 +155,8 @@ void test_find_transactions_obj_by_tag(void) {
139155
test_time_start(&start_time);
140156

141157
TEST_ASSERT_EQUAL_INT32(
142-
SC_OK, api_find_transactions_obj_by_tag(&ta_core.service, FIND_TAG_MSG,
143-
&json_result));
158+
SC_OK, api_find_transactions_obj_by_tag(&ta_core.service,
159+
driver_tag_msg, &json_result));
144160
test_time_end(&start_time, &end_time, &sum);
145161
free(json_result);
146162
}
@@ -151,12 +167,15 @@ void test_send_mam_message(void) {
151167
double sum = 0;
152168
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
153169
char* json_result;
170+
res = send_mam_res_new();
154171

155172
for (size_t count = 0; count < TEST_COUNT; count++) {
156173
test_time_start(&start_time);
157174
TEST_ASSERT_EQUAL_INT32(
158175
SC_OK, api_mam_send_message(&ta_core.tangle, &ta_core.service, json,
159176
&json_result));
177+
send_mam_res_deserialize(json_result, res);
178+
160179
test_time_end(&start_time, &end_time, &sum);
161180
free(json_result);
162181
}
@@ -171,15 +190,17 @@ void test_receive_mam_message(void) {
171190
test_time_start(&start_time);
172191

173192
TEST_ASSERT_EQUAL_INT32(
174-
SC_OK, api_receive_mam_message(&ta_core.service, TEST_BUNDLE_HASH,
175-
&json_result));
193+
SC_OK, api_receive_mam_message(&ta_core.service,
194+
(char*)res->bundle_hash, &json_result));
176195
test_time_end(&start_time, &end_time, &sum);
177196
free(json_result);
178197
}
179198
printf("Average time of receive_mam_message: %lf\n", sum / TEST_COUNT);
180199
}
181200

182201
int main(void) {
202+
srand(time(NULL));
203+
183204
UNITY_BEGIN();
184205

185206
ta_config_default_init(&ta_core.info, &ta_core.tangle, &ta_core.cache,
@@ -194,8 +215,8 @@ int main(void) {
194215
RUN_TEST(test_get_transaction_object);
195216
RUN_TEST(test_find_transactions_by_tag);
196217
RUN_TEST(test_find_transactions_obj_by_tag);
197-
RUN_TEST(test_receive_mam_message);
198218
RUN_TEST(test_send_mam_message);
219+
RUN_TEST(test_receive_mam_message);
199220
ta_config_destroy(&ta_core.service);
200221
return UNITY_END();
201222
}

0 commit comments

Comments
 (0)