add unit test async stmt order

This commit is contained in:
pengrongkun94@qq.com 2025-02-13 11:02:11 +08:00
parent b55980d71d
commit b0bf9bb502
1 changed files with 179 additions and 60 deletions

View File

@ -37,7 +37,7 @@ namespace {
void checkError(TAOS_STMT2* stmt, int code) {
if (code != TSDB_CODE_SUCCESS) {
STscStmt2* pStmt = (STscStmt2*)stmt;
if (pStmt == nullptr || pStmt->sql.sqlStr == nullptr) {
if (pStmt == nullptr || pStmt->sql.sqlStr == nullptr || pStmt->exec.pRequest == nullptr) {
printf("stmt api error\n stats : %d\n errstr : %s\n", pStmt->sql.status, taos_stmt_errstr(stmt));
} else {
printf("stmt api error\n sql : %s\n stats : %d\n errstr : %s\n", pStmt->sql.sqlStr, pStmt->sql.status,
@ -1611,8 +1611,10 @@ TEST(stmt2Case, errcode) {
}
void stmtAsyncBindCb(void* param, TAOS_RES* pRes, int code) {
bool* finish = (bool*)param;
ASSERT_EQ(code, TSDB_CODE_SUCCESS);
taosMsleep(500);
*finish = true;
return;
}
@ -1622,10 +1624,17 @@ void stmtAsyncQueryCb2(void* param, TAOS_RES* pRes, int code) {
return;
}
void stmtAsyncBindCb2(void* param, TAOS_RES* pRes, int code) {
bool* finish = (bool*)param;
taosMsleep(500);
*finish = true;
return;
}
TEST(stmt2Case, async_order) {
int CTB_NUMS = 3;
int ROW_NUMS = 3;
int CYC_NUMS = 1;
int CTB_NUMS = 2;
int ROW_NUMS = 2;
int CYC_NUMS = 2;
TAOS* taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
TAOS_STMT2_OPTION option = {0, true, true, stmtAsyncQueryCb2, NULL};
@ -1651,72 +1660,182 @@ TEST(stmt2Case, async_order) {
sprintf(tmp, "create table stmt2_testdb_15.%s using stmt2_testdb_15.stb tags(0, 'after')", tbs[i]);
do_query(taos, tmp);
}
// params
TAOS_STMT2_BIND** paramv = (TAOS_STMT2_BIND**)taosMemoryMalloc(CTB_NUMS * sizeof(TAOS_STMT2_BIND*));
// col params
int64_t** ts = (int64_t**)taosMemoryMalloc(CTB_NUMS * sizeof(int64_t*));
char** b = (char**)taosMemoryMalloc(CTB_NUMS * sizeof(char*));
int* ts_len = (int*)taosMemoryMalloc(ROW_NUMS * sizeof(int));
int* b_len = (int*)taosMemoryMalloc(ROW_NUMS * sizeof(int));
for (int i = 0; i < ROW_NUMS; i++) {
ts_len[i] = sizeof(int64_t);
b_len[i] = 1;
}
for (int i = 0; i < CTB_NUMS; i++) {
ts[i] = (int64_t*)taosMemoryMalloc(ROW_NUMS * sizeof(int64_t));
b[i] = (char*)taosMemoryMalloc(ROW_NUMS * sizeof(char));
for (int j = 0; j < ROW_NUMS; j++) {
ts[i][j] = 1591060628000 + 100000 + j;
b[i][j] = 'a' + j;
}
}
// bind params
for (int i = 0; i < CTB_NUMS; i++) {
// create col params
paramv[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND));
paramv[i][0] = {TSDB_DATA_TYPE_TIMESTAMP, &ts[i][0], &ts_len[0], NULL, ROW_NUMS};
paramv[i][1] = {TSDB_DATA_TYPE_BINARY, &b[i][0], &b_len[0], NULL, ROW_NUMS};
}
// case 1 : bind_a->exec_a->bind_a->exec_a->...
for (int r = 0; r < CYC_NUMS; r++) {
// col params
int64_t** ts = (int64_t**)taosMemoryMalloc(CTB_NUMS * sizeof(int64_t*));
char** b = (char**)taosMemoryMalloc(CTB_NUMS * sizeof(char*));
int* ts_len = (int*)taosMemoryMalloc(ROW_NUMS * sizeof(int));
int* b_len = (int*)taosMemoryMalloc(ROW_NUMS * sizeof(int));
for (int i = 0; i < ROW_NUMS; i++) {
ts_len[i] = sizeof(int64_t);
b_len[i] = 1;
{
printf("case 1 : bind_a->exec_a->bind_a->exec_a->...\n");
for (int r = 0; r < CYC_NUMS; r++) {
// bind
TAOS_STMT2_BINDV bindv = {CTB_NUMS, tbs, NULL, paramv};
bool finish = false;
code = taos_stmt2_bind_param_a(stmt, &bindv, -1, stmtAsyncBindCb, (void*)&finish);
checkError(stmt, code);
// exec
code = taos_stmt2_exec(stmt, NULL);
checkError(stmt, code);
}
for (int i = 0; i < CTB_NUMS; i++) {
ts[i] = (int64_t*)taosMemoryMalloc(ROW_NUMS * sizeof(int64_t));
b[i] = (char*)taosMemoryMalloc(ROW_NUMS * sizeof(char));
for (int j = 0; j < ROW_NUMS; j++) {
ts[i][j] = 1591060628000 + r * 100000 + j;
b[i][j] = 'a' + j;
}
}
// bind params
TAOS_STMT2_BIND** paramv = (TAOS_STMT2_BIND**)taosMemoryMalloc(CTB_NUMS * sizeof(TAOS_STMT2_BIND*));
for (int i = 0; i < CTB_NUMS; i++) {
// create col params
paramv[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND));
paramv[i][0] = {TSDB_DATA_TYPE_TIMESTAMP, &ts[i][0], &ts_len[0], NULL, ROW_NUMS};
paramv[i][1] = {TSDB_DATA_TYPE_BINARY, &b[i][0], &b_len[0], NULL, ROW_NUMS};
}
// bind
TAOS_STMT2_BINDV bindv = {CTB_NUMS, tbs, NULL, paramv};
code = taos_stmt2_bind_param_a(stmt, &bindv, -1, stmtAsyncBindCb, NULL);
// code = taos_stmt2_bind_param(stmt, &bindv, -1);
checkError(stmt, code);
// exec
int affected = 0;
code = taos_stmt2_exec(stmt, &affected);
checkError(stmt, code);
for (int i = 0; i < CTB_NUMS; i++) {
taosMemoryFree(paramv[i]);
taosMemoryFree(ts[i]);
taosMemoryFree(b[i]);
}
taosMemoryFree(ts);
taosMemoryFree(b);
taosMemoryFree(ts_len);
taosMemoryFree(b_len);
taosMemoryFree(paramv);
}
// ASSERT_EQ(total_affected, CYC_NUMS * ROW_NUMS * CTB_NUMS);
// case 2 : bind_a->bind_a->bind_a->exec_a->...
// case 3 : bind->exec_a->bind->exec_a->...
// case 4 : bind_a->exec->bind_a->exec->...
// case 5 : bind_a->close
// case 6 : exec_a->close
{
printf("case 2 : bind_a->bind_a->bind_a->exec_a->...\n");
for (int r = 0; r < CYC_NUMS; r++) {
// bind params
TAOS_STMT2_BIND** paramv = (TAOS_STMT2_BIND**)taosMemoryMalloc(CTB_NUMS * sizeof(TAOS_STMT2_BIND*));
for (int i = 0; i < CTB_NUMS; i++) {
// create col params
paramv[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND));
paramv[i][0] = {TSDB_DATA_TYPE_TIMESTAMP, &ts[i][0], &ts_len[0], NULL, ROW_NUMS};
paramv[i][1] = {TSDB_DATA_TYPE_BINARY, &b[i][0], &b_len[0], NULL, ROW_NUMS};
}
// bind
TAOS_STMT2_BINDV bindv = {CTB_NUMS, tbs, NULL, paramv};
bool finish = false;
code = taos_stmt2_bind_param_a(stmt, &bindv, -1, stmtAsyncBindCb, (void*)&finish);
while (!finish) {
taosMsleep(100);
}
checkError(stmt, code);
}
// exec
code = taos_stmt2_exec(stmt, NULL);
checkError(stmt, code);
}
// case 3 : bind->exec_a->bind->exec_a->...
{
printf("case 3 : bind->exec_a->bind->exec_a->...\n");
for (int r = 0; r < CYC_NUMS; r++) {
// bind
TAOS_STMT2_BINDV bindv = {CTB_NUMS, tbs, NULL, paramv};
bool finish = false;
code = taos_stmt2_bind_param(stmt, &bindv, -1);
checkError(stmt, code);
// exec
code = taos_stmt2_exec(stmt, NULL);
checkError(stmt, code);
}
}
// case 4 : bind_a->close
{
printf("case 4 : bind_a->close\n");
// bind
TAOS_STMT2_BINDV bindv = {CTB_NUMS, tbs, NULL, paramv};
bool finish = false;
code = taos_stmt2_bind_param_a(stmt, &bindv, -1, stmtAsyncBindCb, (void*)&finish);
checkError(stmt, code);
taos_stmt2_close(stmt);
checkError(stmt, code);
}
// case 5 : bind_a->exec_a->close
{
printf("case 5 : bind_a->exec_a->close\n");
// init
TAOS_STMT2* stmt = taos_stmt2_init(taos, &option);
ASSERT_NE(stmt, nullptr);
int code = taos_stmt2_prepare(stmt, sql, 0);
checkError(stmt, code);
// bind
TAOS_STMT2_BINDV bindv = {CTB_NUMS, tbs, NULL, paramv};
bool finish = false;
code = taos_stmt2_bind_param_a(stmt, &bindv, -1, stmtAsyncBindCb, (void*)&finish);
checkError(stmt, code);
// exec
code = taos_stmt2_exec(stmt, NULL);
checkError(stmt, code);
// close
taos_stmt2_close(stmt);
checkError(stmt, code);
}
option = {0, false, false, NULL, NULL};
stmt = taos_stmt2_init(taos, &option);
ASSERT_NE(stmt, nullptr);
code = taos_stmt2_prepare(stmt, sql, 0);
checkError(stmt, code);
// case 6 : bind_a->exec->bind_a->exec->...
{
printf("case 6 : bind_a->exec->bind_a->exec->...\n");
// init
checkError(stmt, code);
for (int r = 0; r < CYC_NUMS; r++) {
// bind
TAOS_STMT2_BINDV bindv = {CTB_NUMS, tbs, NULL, paramv};
bool finish = false;
code = taos_stmt2_bind_param_a(stmt, &bindv, -1, stmtAsyncBindCb, (void*)&finish);
checkError(stmt, code);
// exec
code = taos_stmt2_exec(stmt, NULL);
checkError(stmt, code);
}
}
// case 7 (error:no wait error) : bind_a->bind_a
{
printf("case 7 (error:no wait error) : bind_a->bind_a\n");
// bind
TAOS_STMT2_BINDV bindv = {CTB_NUMS, tbs, NULL, paramv};
bool finish = false;
code = taos_stmt2_bind_param_a(stmt, &bindv, -1, stmtAsyncBindCb2, (void*)&finish);
checkError(stmt, code);
taosMsleep(200);
code = taos_stmt2_bind_param_a(stmt, &bindv, -1, stmtAsyncBindCb2, (void*)&finish);
ASSERT_EQ(code, TSDB_CODE_TSC_STMT_API_ERROR);
while (!finish) {
taosMsleep(100);
}
}
// close
taos_stmt2_close(stmt);
// free memory
for (int i = 0; i < CTB_NUMS; i++) {
taosMemoryFree(paramv[i]);
taosMemoryFree(ts[i]);
taosMemoryFree(b[i]);
}
taosMemoryFree(ts);
taosMemoryFree(b);
taosMemoryFree(ts_len);
taosMemoryFree(b_len);
taosMemoryFree(paramv);
for (int i = 0; i < CTB_NUMS; i++) {
taosMemoryFree(tbs[i]);
}
taosMemoryFree(tbs);
taos_stmt2_close(stmt);
}
#pragma GCC diagnostic pop