• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3815

01 Apr 2025 06:11AM UTC coverage: 34.09% (+3.9%) from 30.169%
#3815

push

travis-ci

happyguoxy
test:alter gcda dir

148617 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222625 of 489487 relevant lines covered (45.48%)

762982.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

39.99
/source/client/test/clientTests.cpp
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include <gtest/gtest.h>
17
#include <iostream>
18
#include "clientInt.h"
19
#include "osSemaphore.h"
20
#include "taoserror.h"
21
#include "tglobal.h"
22
#include "thash.h"
23

24
#pragma GCC diagnostic push
25
#pragma GCC diagnostic ignored "-Wwrite-strings"
26
#pragma GCC diagnostic ignored "-Wunused-function"
27
#pragma GCC diagnostic ignored "-Wunused-variable"
28
#pragma GCC diagnostic ignored "-Wsign-compare"
29

30
#include "executor.h"
31
#include "taos.h"
32

33
namespace {
34

35
void printSubResults(void* pRes, int32_t* totalRows) {
×
36
  char buf[1024] = {0};
×
37

38
  int32_t vgId = tmq_get_vgroup_id(pRes);
×
39
  int64_t offset = tmq_get_vgroup_offset(pRes);
×
40
  while (1) {
41
    TAOS_ROW row = taos_fetch_row(pRes);
×
42
    if (row == NULL) {
×
43
      break;
×
44
    }
45

46
    TAOS_FIELD* fields = taos_fetch_fields(pRes);
×
47
    if(fields == NULL) {
×
48
      std::cout << "fields is null" << std::endl;
×
49
      break;
×
50
    }
51
    int32_t numOfFields = taos_field_count(pRes);
×
52
    int32_t precision = taos_result_precision(pRes);
×
53
    (void)taos_print_row(buf, row, fields, numOfFields);
×
54
    *totalRows += 1;
×
55
    std::cout << "vgId:" << vgId << ", offset:" << offset << ", precision:" << precision << ", row content:" << buf
×
56
              << std::endl;
×
57
  }
×
58

59
//  taos_free_result(pRes);
60
}
×
61

62
void showDB(TAOS* pConn) {
×
63
  TAOS_RES* pRes = taos_query(pConn, "show databases");
×
64
  TAOS_ROW  pRow = NULL;
×
65

66
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
×
67
  int32_t     numOfFields = taos_num_fields(pRes);
×
68

69
  char str[512] = {0};
×
70
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
×
71
    int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
×
72
    (void)printf("%s\n", str);
×
73
  }
74
}
×
75

76
void printResult(TAOS_RES* pRes) {
×
77
  TAOS_ROW    pRow = NULL;
×
78
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
×
79
  int32_t     numOfFields = taos_num_fields(pRes);
×
80

81
  int32_t n = 0;
×
82
  char    str[512] = {0};
×
83
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
×
84
    //    int32_t* length = taos_fetch_lengths(pRes);
85
    //    for(int32_t i = 0; i < numOfFields; ++i) {
86
    //      (void)printf("(%d):%d " , i, length[i]);
87
    //    }
88
    //    (void)printf("\n");
89
    //
90
    //    int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
91
    //    (void)printf("%s\n", str);
92
    //    memset(str, 0, sizeof(str));
93
  }
94
}
×
95

96
void fetchCallback(void* param, void* res, int32_t numOfRow) {
×
97
#if 0
98
  (void)printf("numOfRow = %d \n", numOfRow);
99
  int         numFields = taos_num_fields(res);
100
  TAOS_FIELD *fields = taos_fetch_fields(res);
101
  TAOS       *_taos = (TAOS *)param;
102
  if (numOfRow > 0) {
103
    for (int i = 0; i < numOfRow; ++i) {
104
      TAOS_ROW row = taos_fetch_row(res);
105

106
      char temp[256] = {0};
107
      taos_print_row(temp, row, fields, numFields);
108
      (void)printf("%s\n", temp);
109
    }
110
    taos_fetch_rows_a(res, fetchCallback, _taos);
111
  } else {
112
    (void)printf("no more data, close the connection.\n");
113
//    taos_free_result(res);
114
//    taos_close(_taos);
115
//    taos_cleanup();
116
  }
117
#endif
118
  if (numOfRow == 0) {
×
119
    (void)printf("completed\n");
×
120
    return;
×
121
  }
122

123
  taos_fetch_raw_block_a(res, fetchCallback, param);
×
124
}
125

126
void queryCallback(void* param, void* res, int32_t code) {
1✔
127
  if (code != TSDB_CODE_SUCCESS) {
1!
128
    (void)printf("failed to execute, reason:%s\n", taos_errstr(res));
1✔
129
    taos_free_result(res);
1✔
130
    tsem_t *sem = (tsem_t *)param;
1✔
131
    tsem_post(sem);
1✔
132
    return;
1✔
133
  }
134
  (void)printf("start to fetch data\n");
×
135
  taos_fetch_raw_block_a(res, fetchCallback, param);
×
136
  taos_free_result(res);
×
137
  tsem_t *sem = (tsem_t *)param;
×
138
  tsem_post(sem);
×
139
}
140

141
void createNewTable(TAOS* pConn, int32_t index, int32_t numOfRows, int64_t startTs, const char* pVarchar) {
1✔
142
  char str[1024] = {0};
1✔
143
  (void)sprintf(str, "create table if not exists tu%d using st2 tags(%d)", index, index);
1✔
144

145
  TAOS_RES* pRes = taos_query(pConn, str);
1!
146
  if (taos_errno(pRes) != 0) {
1!
147
    (void)printf("failed to create table tu, reason:%s\n", taos_errstr(pRes));
×
148
  }
149
  taos_free_result(pRes);
1!
150

151
  if (startTs == 0) {
1!
152
    for (int32_t i = 0; i < numOfRows; i += 20) {
501✔
153
      char sql[1024] = {0};
500✔
154
      (void)sprintf(sql,
500✔
155
                    "insert into tu%d values(now+%da, %d)(now+%da, %d)(now+%da, %d)(now+%da, %d)"
156
                    "(now+%da, %d)(now+%da, %d)(now+%da, %d)(now+%da, %d)(now+%da, %d)(now+%da, %d)"
157
                    "(now+%da, %d)(now+%da, %d)(now+%da, %d)(now+%da, %d)"
158
                    "(now+%da, %d)(now+%da, %d)(now+%da, %d)(now+%da, %d)(now+%da, %d)(now+%da, %d)",
159
                    index, i, i, i + 1, i + 1, i + 2, i + 2, i + 3, i + 3, i + 4, i + 4, i + 5, i + 5, i + 6, i + 6, i + 7,
160
                    i + 7, i + 8, i + 8, i + 9, i + 9, i + 10, i + 10, i + 11, i + 11, i + 12, i + 12, i + 13, i + 13, i + 14,
161
                    i + 14, i + 15, i + 15, i + 16, i + 16, i + 17, i + 17, i + 18, i + 18, i + 19, i + 19);
162
      TAOS_RES* p = taos_query(pConn, sql);
500!
163
      if (taos_errno(p) != 0) {
500!
164
        (void)printf("failed to insert data, reason:%s\n", taos_errstr(p));
×
165
      }
166

167
      taos_free_result(p);
500!
168
    }
169
  } else {
170
    for (int32_t i = 0; i < numOfRows; i += 20) {
×
171
      char sql[1024*50] = {0};
×
172
      (void)sprintf(sql,
×
173
                    "insert into tu%d values(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, "
174
                    "%d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, "
175
                    "'%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')(%ld, %d, '%s')",
176
                    index, startTs, i, pVarchar, startTs + 1, i + 1, pVarchar, startTs + 2, i + 2, pVarchar, startTs + 3, i + 3, pVarchar, startTs + 4, i + 4,
177
                    pVarchar, startTs + 5, i + 5, pVarchar, startTs + 6, i + 6, pVarchar, startTs + 7, i + 7, pVarchar, startTs + 8, i + 8, pVarchar, startTs + 9, i + 9,
178
                    pVarchar, startTs + 10, i + 10, pVarchar, startTs + 11, i + 11, pVarchar, startTs + 12, i + 12, pVarchar, startTs + 13, i + 13, pVarchar, startTs + 14,
179
                    i + 14, pVarchar, startTs + 15, i + 15, pVarchar, startTs + 16, i + 16, pVarchar, startTs + 17, i + 17, pVarchar, startTs + 18, i + 18,
180
                    pVarchar, startTs + 19, i + 19, pVarchar);
181
      TAOS_RES* p = taos_query(pConn, sql);
×
182
      if (taos_errno(p) != 0) {
×
183
        (void)printf("failed to insert data, reason:%s\n", taos_errstr(p));
×
184
      }
185

186
//      startTs += 20;
187
      taos_free_result(p);
×
188
    }
189
  }
190
}
1✔
191

192
void* queryThread(void* arg) {
1✔
193
  TAOS* pConn = taos_connect("192.168.0.209", "root", "taosdata", NULL, 0);
1✔
194
  if (pConn == NULL) {
1!
195
    (void)printf("failed to connect to db, reason:%s", taos_errstr(pConn));
1✔
196
    return NULL;
1✔
197
  }
198

199
  int64_t el = 0;
×
200

201
  for (int32_t i = 0; i < 5; ++i) {
×
202
    int64_t   st = taosGetTimestampUs();
×
203
    TAOS_RES* pRes = taos_query(pConn,
×
204
                                "SELECT _wstart as ts,max(usage_user) FROM benchmarkcpu.host_49 WHERE  ts >= "
205
                                "1451618560000 AND ts < 1451622160000 INTERVAL(1m) ;");
206
    if (taos_errno(pRes) != 0) {
×
207
      (void)printf("failed, reason:%s\n", taos_errstr(pRes));
×
208
    } else {
209
      printResult(pRes);
×
210
    }
211

212
    taos_free_result(pRes);
×
213
    el += (taosGetTimestampUs() - st);
×
214
    if (i % 1000 == 0 && i != 0) {
×
215
      (void)printf("total:%d, avg time:%.2fms\n", i, el / (double)(i * 1000));
×
216
    }
217
  }
218

219
  taos_close(pConn);
×
220
  return NULL;
×
221
}
222

223
int32_t numOfThreads = 1;
224

225
void tmq_commit_cb_print(tmq_t* pTmq, int32_t code, void* param) {
×
226
//  (void)printf("auto commit success, code:%d\n", code);
227
}
×
228

229
void* doConsumeData(void* param) {
1✔
230
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1✔
231
  tmq_conf_t* conf = tmq_conf_new();
1✔
232
  (void)tmq_conf_set(conf, "enable.auto.commit", "true");
1✔
233
  (void)tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
1✔
234
  (void)tmq_conf_set(conf, "group.id", "cgrpName41");
1✔
235
  (void)tmq_conf_set(conf, "td.connect.user", "root");
1✔
236
  (void)tmq_conf_set(conf, "td.connect.pass", "taosdata");
1✔
237
  (void)tmq_conf_set(conf, "auto.offset.reset", "earliest");
1✔
238
  (void)tmq_conf_set(conf, "experimental.snapshot.enable", "true");
1✔
239
  (void)tmq_conf_set(conf, "msg.with.table.name", "true");
1✔
240
  (void)tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, NULL);
1✔
241

242
  tmq_t* tmq = tmq_consumer_new(conf, NULL, 0);
1✔
243
  tmq_conf_destroy(conf);
1✔
244

245
  // 创建订阅 topics 列表
246
  tmq_list_t* topicList = tmq_list_new();
1✔
247
  (void)tmq_list_append(topicList, "topic_t2");
1✔
248

249
  // 启动订阅
250
  (void)tmq_subscribe(tmq, topicList);
1✔
251

252
  tmq_list_destroy(topicList);
1✔
253

254
  TAOS_FIELD* fields = NULL;
1✔
255
  int32_t     numOfFields = 0;
1✔
256
  int32_t     precision = 0;
1✔
257
  int32_t     totalRows = 0;
1✔
258
  int32_t     msgCnt = 0;
1✔
259
  int32_t     timeout = 25000;
1✔
260

261
  int32_t count = 0;
1✔
262

263
  while (1) {
264
    TAOS_RES* pRes = tmq_consumer_poll(tmq, timeout);
1✔
265
    if (pRes) {
1!
266
      char buf[1024] = {0};
×
267

268
      const char* topicName = tmq_get_topic_name(pRes);
×
269
      const char* dbName = tmq_get_db_name(pRes);
×
270
      int32_t     vgroupId = tmq_get_vgroup_id(pRes);
×
271

272
      (void)printf("topic: %s\n", topicName);
×
273
      (void)printf("db: %s\n", dbName);
×
274
      (void)printf("vgroup id: %d\n", vgroupId);
×
275

276
      while (1) {
277
        TAOS_ROW row = taos_fetch_row(pRes);
×
278
        if (row == NULL) {
×
279
          break;
×
280
        }
281

282
        fields = taos_fetch_fields(pRes);
×
283
        numOfFields = taos_field_count(pRes);
×
284
        precision = taos_result_precision(pRes);
×
285
        (void)taos_print_row(buf, row, fields, numOfFields);
×
286
        totalRows += 1;
×
287
        //        (void)printf("precision: %d, row content: %s\n", precision, buf);
288
      }
×
289

290
      taos_free_result(pRes);
×
291
    } else {
292
      break;
1✔
293
    }
294
  }
×
295

296
  (void)tmq_consumer_close(tmq);
1✔
297
  taos_close(pConn);
1✔
298
  (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
1✔
299
  return NULL;
1✔
300
}
301

302
}  // namespace
303

304
int main(int argc, char** argv) {
1✔
305
  testing::InitGoogleTest(&argc, argv);
1✔
306
  if (argc > 1) {
1!
307
    //numOfThreads = atoi(argv[1]);
308
    int32_t code = taosStr2int32(argv[1], &numOfThreads);
×
309
    if (code != 0) {
×
310
      return code;
×
311
    }
312

313

314
  }
315

316
  numOfThreads = TMAX(numOfThreads, 1);
1!
317
  (void)printf("the runing threads is:%d", numOfThreads);
1✔
318

319
  return RUN_ALL_TESTS();
1✔
320
}
321

322
TEST(clientCase, driverInit_Test) {
4✔
323
  // taosInitGlobalCfg();
324
  //  taos_init();
325
}
1✔
326

327
TEST(clientCase, connect_Test) {
4✔
328
  taos_options(TSDB_OPTION_CONFIGDIR, "~/first/cfg");
1✔
329
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1✔
330
  if (pConn == NULL) {
1!
331
    (void)printf("failed to connect to server, reason:%s\n", taos_errstr(NULL));
×
332
  }
333

334
  TAOS_RES* pRes = taos_query(pConn, "drop database abc1");
1✔
335
  if (taos_errno(pRes) != 0) {
1!
336
    (void)printf("error in drop db, reason:%s\n", taos_errstr(pRes));
1✔
337
  }
338
  taos_free_result(pRes);
1✔
339

340
  taos_close(pConn);
1✔
341
}
1✔
342

343
TEST(clientCase, create_user_Test) {
4✔
344
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
345
  ASSERT_NE(pConn, nullptr);
1!
346

347
  TAOS_RES* pRes = taos_query(pConn, "create user abc pass 'abc'");
1!
348
  if (taos_errno(pRes) != TSDB_CODE_SUCCESS) {
1!
349
    (void)printf("failed to create user, reason:%s\n", taos_errstr(pRes));
1!
350
  }
351

352
  taos_free_result(pRes);
1!
353
  taos_close(pConn);
1!
354
}
355

356
TEST(clientCase, create_account_Test) {
4✔
357
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
358
  ASSERT_NE(pConn, nullptr);
1!
359

360
  TAOS_RES* pRes = taos_query(pConn, "create account aabc pass 'abc'");
1!
361
  if (taos_errno(pRes) != TSDB_CODE_SUCCESS) {
1!
362
    (void)printf("failed to create user, reason:%s\n", taos_errstr(pRes));
1!
363
  }
364

365
  taos_free_result(pRes);
1!
366
  taos_close(pConn);
1!
367
}
368

369
TEST(clientCase, drop_account_Test) {
4✔
370
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
371
  ASSERT_NE(pConn, nullptr);
1!
372

373
  TAOS_RES* pRes = taos_query(pConn, "drop account aabc");
1!
374
  if (taos_errno(pRes) != TSDB_CODE_SUCCESS) {
1!
375
    (void)printf("failed to create user, reason:%s\n", taos_errstr(pRes));
1!
376
  }
377

378
  taos_free_result(pRes);
1!
379
  taos_close(pConn);
1!
380
}
381

382
TEST(clientCase, show_user_Test) {
4✔
383
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
384
  ASSERT_NE(pConn, nullptr);
1!
385

386
  TAOS_RES* pRes = taos_query(pConn, "show users");
1!
387
  TAOS_ROW  pRow = NULL;
1✔
388

389
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
390
  ASSERT_NE(pFields, nullptr);
1!
391
  int32_t     numOfFields = taos_num_fields(pRes);
1!
392

393
  char str[512] = {0};
1✔
394
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
2!
395
    int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
1!
396
    (void)printf("%s\n", str);
1!
397
  }
398

399
  taos_free_result(pRes);
1!
400
  taos_close(pConn);
1!
401
}
402

403
TEST(clientCase, drop_user_Test) {
4✔
404
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
405
  ASSERT_NE(pConn, nullptr);
1!
406

407
  TAOS_RES* pRes = taos_query(pConn, "drop user abc");
1!
408
  if (taos_errno(pRes) != TSDB_CODE_SUCCESS) {
1!
409
    (void)printf("failed to create user, reason:%s\n", taos_errstr(pRes));
1!
410
  }
411

412
  taos_free_result(pRes);
1!
413
  taos_close(pConn);
1!
414
}
415

416
TEST(clientCase, show_db_Test) {
4✔
417
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
418
  ASSERT_NE(pConn, nullptr);
1!
419

420
  TAOS_RES* pRes = taos_query(pConn, "show databases");
1!
421
  TAOS_ROW  pRow = NULL;
1✔
422

423
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
424
  ASSERT_NE(pFields,  nullptr);
1!
425
  int32_t     numOfFields = taos_num_fields(pRes);
1!
426

427
  char str[512] = {0};
1✔
428
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
3!
429
    int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
2!
430
    (void)printf("%s\n", str);
2!
431
  }
432
  taos_free_result(pRes);
1!
433
  taos_close(pConn);
1!
434
}
435

436
TEST(clientCase, create_db_Test) {
4✔
437
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
438
  ASSERT_NE(pConn, nullptr);
1!
439

440
  TAOS_RES* pRes = taos_query(pConn, "create database abc1 vgroups 2");
1!
441
  if (taos_errno(pRes) != 0) {
1!
442
    (void)printf("error in create db, reason:%s\n", taos_errstr(pRes));
×
443
  }
444

445
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
446
  ASSERT_TRUE(pFields == NULL);
1!
447

448
  int32_t numOfFields = taos_num_fields(pRes);
1!
449
  ASSERT_EQ(numOfFields, 0);
1!
450

451
  taos_free_result(pRes);
1!
452

453
  pRes = taos_query(pConn, "create database abc1 vgroups 4");
1!
454
  if (taos_errno(pRes) != 0) {
1!
455
    (void)printf("error in create db, reason:%s\n", taos_errstr(pRes));
1!
456
  }
457
  taos_free_result(pRes);
1!
458
  taos_close(pConn);
1!
459
}
460

461
TEST(clientCase, create_dnode_Test) {
4✔
462
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
463
  ASSERT_NE(pConn, nullptr);
1!
464

465
  TAOS_RES* pRes = taos_query(pConn, "create dnode abc1 port 7000");
1!
466
  if (taos_errno(pRes) != 0) {
1!
467
    (void)printf("error in create dnode, reason:%s\n", taos_errstr(pRes));
×
468
  }
469
  taos_free_result(pRes);
1!
470

471
  pRes = taos_query(pConn, "create dnode 1.1.1.1 port 9000");
1!
472
  if (taos_errno(pRes) != 0) {
1!
473
    (void)printf("failed to create dnode, reason:%s\n", taos_errstr(pRes));
×
474
  }
475
  taos_free_result(pRes);
1!
476

477
  taos_close(pConn);
1!
478
}
479

480
TEST(clientCase, drop_dnode_Test) {
4✔
481
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
482
  ASSERT_NE(pConn, nullptr);
1!
483

484
  TAOS_RES* pRes = taos_query(pConn, "drop dnode 3");
1!
485
  if (taos_errno(pRes) != 0) {
1!
486
    (void)printf("error in drop dnode, reason:%s\n", taos_errstr(pRes));
×
487
  }
488

489
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
490
  ASSERT_TRUE(pFields == NULL);
1!
491

492
  int32_t numOfFields = taos_num_fields(pRes);
1!
493
  ASSERT_EQ(numOfFields, 0);
1!
494
  taos_free_result(pRes);
1!
495

496
  pRes = taos_query(pConn, "drop dnode 4");
1!
497
  if (taos_errno(pRes) != 0) {
1!
498
    (void)printf("error in drop dnode, reason:%s\n", taos_errstr(pRes));
1!
499
  }
500

501
  taos_free_result(pRes);
1!
502
  taos_close(pConn);
1!
503
}
504

505
TEST(clientCase, use_db_test) {
4✔
506
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
507
  ASSERT_NE(pConn, nullptr);
1!
508

509
  TAOS_RES* pRes = taos_query(pConn, "use abc1");
1!
510
  if (taos_errno(pRes) != 0) {
1!
511
    (void)printf("error in use db, reason:%s\n", taos_errstr(pRes));
×
512
  }
513

514
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
515
  ASSERT_TRUE(pFields == NULL);
1!
516

517
  int32_t numOfFields = taos_num_fields(pRes);
1!
518
  ASSERT_EQ(numOfFields, 0);
1!
519

520
  taos_free_result(pRes);
1!
521
  taos_close(pConn);
1!
522
}
523

524
TEST(clientCase, create_stable_Test) {
4✔
525
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
526
  ASSERT_NE(pConn, nullptr);
1!
527

528
  TAOS_RES* pRes = taos_query(pConn, "create database if not exists abc1 vgroups 2");
1!
529
  if (taos_errno(pRes) != 0) {
1!
530
    (void)printf("error in create db, reason:%s\n", taos_errstr(pRes));
×
531
  }
532
  taos_free_result(pRes);
1!
533

534
  pRes = taos_query(pConn, "use abc1");
1!
535
  while (taos_errno(pRes) == TSDB_CODE_MND_DB_IN_CREATING || taos_errno(pRes) == TSDB_CODE_MND_DB_IN_DROPPING) {
1!
536
    taosMsleep(2000);
×
537
    pRes = taos_query(pConn, "use abc1");
×
538
  }
539
  taos_free_result(pRes);
1!
540

541
  pRes = taos_query(pConn, "create table if not exists abc1.st1(ts timestamp, k int) tags(a int)");
1!
542
  if (taos_errno(pRes) != 0) {
1!
543
    (void)printf("error in create stable, reason:%s\n", taos_errstr(pRes));
×
544
  }
545

546
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
547
  ASSERT_TRUE(pFields == NULL);
1!
548

549
  int32_t numOfFields = taos_num_fields(pRes);
1!
550
  ASSERT_EQ(numOfFields, 0);
1!
551
  taos_free_result(pRes);
1!
552

553
  taos_close(pConn);
1!
554
}
555

556
TEST(clientCase, create_table_Test) {
4✔
557
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
558
  ASSERT_NE(pConn, nullptr);
1!
559

560
  TAOS_RES* pRes = taos_query(pConn, "use abc1");
1!
561
  taos_free_result(pRes);
1!
562

563
  pRes = taos_query(pConn, "create table if not exists tm0(ts timestamp, k int)");
1!
564
  ASSERT_EQ(taos_errno(pRes), 0);
1!
565

566
  taos_free_result(pRes);
1!
567

568
  pRes = taos_query(pConn, "create table if not exists tm0(ts timestamp, k blob)");
1!
569
  ASSERT_NE(taos_errno(pRes), 0);
1!
570

571
  taos_free_result(pRes);
1!
572
  taos_close(pConn);
1!
573
}
574

575
TEST(clientCase, create_ctable_Test) {
4✔
576
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
577
  ASSERT_NE(pConn, nullptr);
1!
578

579
  TAOS_RES* pRes = taos_query(pConn, "use abc1");
1!
580
  if (taos_errno(pRes) != 0) {
1!
581
    (void)printf("failed to use db, reason:%s\n", taos_errstr(pRes));
×
582
  }
583
  taos_free_result(pRes);
1!
584

585
  pRes = taos_query(pConn, "create stable if not exists st1 (ts timestamp, k int ) tags(a int)");
1!
586
  if (taos_errno(pRes) != 0) {
1!
587
    (void)printf("failed to create stable, reason:%s\n", taos_errstr(pRes));
×
588
  }
589
  taos_free_result(pRes);
1!
590

591
  pRes = taos_query(pConn, "create table tu using st1 tags('2021-10-10 1:1:1');");
1!
592
  if (taos_errno(pRes) != 0) {
1!
593
    (void)printf("failed to create child table tm0, reason:%s\n", taos_errstr(pRes));
1!
594
  }
595

596
  taos_free_result(pRes);
1!
597
  taos_close(pConn);
1!
598
}
599

600
TEST(clientCase, show_stable_Test) {
4✔
601
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
602
  ASSERT_NE(pConn, nullptr);
1!
603

604
  TAOS_RES* pRes = taos_query(pConn, "show abc1.stables");
1!
605
  if (taos_errno(pRes) != 0) {
1!
606
    (void)printf("failed to show stables, reason:%s\n", taos_errstr(pRes));
×
607
    taos_free_result(pRes);
×
608
    ASSERT_TRUE(false);
×
609
  }
610

611
  TAOS_ROW    pRow = NULL;
1✔
612
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
613
  int32_t     numOfFields = taos_num_fields(pRes);
1!
614

615
  char str[512] = {0};
1✔
616
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
2!
617
    int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
1!
618
    (void)printf("%s\n", str);
1!
619
  }
620

621
  taos_free_result(pRes);
1!
622
  taos_close(pConn);
1!
623
}
624

625
TEST(clientCase, show_vgroup_Test) {
4✔
626
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
627
  ASSERT_NE(pConn, nullptr);
1!
628

629
  TAOS_RES* pRes = taos_query(pConn, "use abc1");
1!
630
  if (taos_errno(pRes) != 0) {
1!
631
    (void)printf("failed to use db, reason:%s\n", taos_errstr(pRes));
×
632
  }
633
  taos_free_result(pRes);
1!
634

635
  pRes = taos_query(pConn, "show vgroups");
1!
636
  if (taos_errno(pRes) != 0) {
1!
637
    (void)printf("failed to show vgroups, reason:%s\n", taos_errstr(pRes));
×
638
    taos_free_result(pRes);
×
639
    ASSERT_TRUE(false);
×
640
  }
641

642
  TAOS_ROW pRow = NULL;
1✔
643

644
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
645
  int32_t     numOfFields = taos_num_fields(pRes);
1!
646

647
  char str[512] = {0};
1✔
648
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
3!
649
    int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
2!
650
    (void)printf("%s\n", str);
2!
651
  }
652

653
  taos_free_result(pRes);
1!
654
  taos_close(pConn);
1!
655
}
656

657
TEST(clientCase, create_multiple_tables) {
4✔
658
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
659
  ASSERT_NE(pConn, nullptr);
1!
660

661
  TAOS_RES* pRes = taos_query(pConn, "create database if not exists abc1");
1!
662
  if (taos_errno(pRes) != 0) {
1!
663
    (void)printf("failed to create db, reason:%s\n", taos_errstr(pRes));
×
664
    taos_free_result(pRes);
×
665
    taos_close(pConn);
×
666
    return;
×
667
  }
668
  taos_free_result(pRes);
1!
669

670
  pRes = taos_query(pConn, "use abc1");
1!
671
  while (taos_errno(pRes) == TSDB_CODE_MND_DB_IN_CREATING || taos_errno(pRes) == TSDB_CODE_MND_DB_IN_DROPPING) {
1!
672
    taosMsleep(2000);
×
673
    pRes = taos_query(pConn, "use abc1");
×
674
  }
675
  if (taos_errno(pRes) != 0) {
1!
676
    (void)printf("failed to use db, reason:%s\n", taos_errstr(pRes));
×
677
    taos_free_result(pRes);
×
678
    taos_close(pConn);
×
679
    return;
×
680
  }
681

682
  taos_free_result(pRes);
1!
683

684
  pRes = taos_query(pConn, "create stable if not exists st1 (ts timestamp, k int) tags(a int)");
1!
685
  if (taos_errno(pRes) != 0) {
1!
686
    (void)printf("failed to create stable tables, reason:%s\n", taos_errstr(pRes));
×
687
  }
688

689
  taos_free_result(pRes);
1!
690

691
  pRes = taos_query(pConn, "create table if not exists t_2 using st1 tags(1)");
1!
692
  if (taos_errno(pRes) != 0) {
1!
693
    (void)printf("failed to create multiple tables, reason:%s\n", taos_errstr(pRes));
×
694
    taos_free_result(pRes);
×
695
    ASSERT_TRUE(false);
×
696
  }
697

698
  taos_free_result(pRes);
1!
699
  pRes = taos_query(pConn, "create table if not exists t_3 using st1 tags(2)");
1!
700
  if (taos_errno(pRes) != 0) {
1!
701
    (void)printf("failed to create multiple tables, reason:%s\n", taos_errstr(pRes));
×
702
    taos_free_result(pRes);
×
703
    ASSERT_TRUE(false);
×
704
  }
705

706
  TAOS_ROW    pRow = NULL;
1✔
707
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
708
  int32_t     numOfFields = taos_num_fields(pRes);
1!
709

710
  char str[512] = {0};
1✔
711
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
1!
712
    int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
×
713
    (void)printf("%s\n", str);
×
714
  }
715

716
  taos_free_result(pRes);
1!
717

718
  for (int32_t i = 0; i < 500; i += 2) {
251✔
719
    char sql[512] = {0};
250✔
720
    (void)snprintf(sql, tListLen(sql), "create table t_x_%d using st1 tags(2) t_x_%d using st1 tags(5)", i, i + 1);
250✔
721
    TAOS_RES* pres = taos_query(pConn, sql);
250!
722
    if (taos_errno(pres) != 0) {
250!
723
      (void)printf("failed to create table %d\n, reason:%s", i, taos_errstr(pres));
×
724
    }
725
    taos_free_result(pres);
250!
726
  }
727

728
  taos_close(pConn);
1!
729
}
730

731
TEST(clientCase, show_table_Test) {
4✔
732
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
733
  ASSERT_NE(pConn, nullptr);
1!
734

735
  TAOS_RES* pRes = taos_query(pConn, "show tables");
1!
736
  if (taos_errno(pRes) != 0) {
1!
737
    (void)printf("failed to show tables, reason:%s\n", taos_errstr(pRes));
1!
738
  }
739
  taos_free_result(pRes);
1!
740

741
  pRes  = taos_query(pConn, "use abc1");
1!
742
  taos_free_result(pRes);
1!
743

744
  pRes = taos_query(pConn, "show tables");
1!
745
  if (taos_errno(pRes) != 0) {
1!
746
    (void)printf("failed to show tables, reason:%s\n", taos_errstr(pRes));
×
747
    taos_free_result(pRes);
×
748
  }
749

750
  TAOS_ROW    pRow = NULL;
1✔
751
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
752
  int32_t     numOfFields = taos_num_fields(pRes);
1!
753

754
  int32_t count = 0;
1✔
755
  char    str[512] = {0};
1✔
756

757
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
504!
758
    int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
503!
759
    if(code > 0) {
503!
760
      (void)printf("%d: %s\n", ++count, str);
503!
761
    }
762
  }
763

764
  taos_free_result(pRes);
1!
765
  taos_close(pConn);
1!
766
}
767

768
TEST(clientCase, generated_request_id_test) {
4✔
769
  SHashObj* phash = taosHashInit(10000, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
1✔
770

771
  for (int32_t i = 0; i < 50000; ++i) {
50,001✔
772
    uint64_t v = generateRequestId();
50,000!
773
    void*    result = taosHashGet(phash, &v, sizeof(v));
50,000!
774
    if (result != nullptr) {
50,000✔
775
      //      (void)printf("0x%llx, index:%d\n", v, i);
776
    }
777
    ASSERT_EQ(result, nullptr);
50,000!
778
    (void)taosHashPut(phash, &v, sizeof(v), NULL, 0);
50,000!
779
  }
780

781
  taosHashCleanup(phash);
1✔
782
}
783

784
TEST(clientCase, insert_test) {
4✔
785
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
786
  ASSERT_NE(pConn, nullptr);
1!
787

788
  TAOS_RES* pRes = taos_query(pConn, "use abc1");
1!
789
  taos_free_result(pRes);
1!
790

791
  pRes = taos_query(pConn, "insert into t_2 values(now, 1)");
1!
792
  if (taos_errno(pRes) != 0) {
1!
793
    (void)printf("failed to create into table t_2, reason:%s\n", taos_errstr(pRes));
×
794
    taos_free_result(pRes);
×
795
    ASSERT_TRUE(false);
×
796
  }
797

798
  taos_free_result(pRes);
1!
799
  taos_close(pConn);
1!
800
}
801

802
TEST(clientCase, projection_query_tables) {
4✔
803
  taos_options(TSDB_OPTION_CONFIGDIR, "/home/lisa/first/cfg");
1!
804

805
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
806
  ASSERT_NE(pConn, nullptr);
1!
807

808
  TAOS_RES* pRes = NULL;
1✔
809

810
  pRes= taos_query(pConn, "use abc1");
1!
811
  taos_free_result(pRes);
1!
812

813
  pRes = taos_query(pConn, "select forecast(k,'algo=arima,wncheck=0') from t1 where ts<='2024-11-15 1:7:44'");
1!
814
  if (taos_errno(pRes) != 0) {
1!
815
    (void)printf("failed to create table tu, reason:%s\n", taos_errstr(pRes));
1!
816
  }
817
  taos_free_result(pRes);
1!
818

819
  pRes = taos_query(pConn, "create table tu using st2 tags(2)");
1!
820
  if (taos_errno(pRes) != 0) {
1!
821
    (void)printf("failed to create table tu, reason:%s\n", taos_errstr(pRes));
1!
822
  }
823
  taos_free_result(pRes);
1!
824

825
  pRes = taos_query(pConn, "create table st2 (ts timestamp, v1 int) tags(t1 int)");
1!
826
  if (taos_errno(pRes) != 0) {
1!
827
    (void)printf("failed to create table tu, reason:%s\n", taos_errstr(pRes));
×
828
  }
829
  taos_free_result(pRes);
1!
830

831
  int64_t start = 1685959190000;
1✔
832
  const char* pstr =
1✔
833
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgh"
834
      "ijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop"
835
      "qrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
836
      "yzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdef"
837
      "ghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz!@#$%^&&*&^^%$#@!qQWERTYUIOPASDFGHJKL:"
838
      "QWERTYUIOP{}";
839

840
  for(int32_t i = 0; i < 1; ++i) {
2✔
841
    char str[1024] = {0};
1✔
842
    (void)sprintf(str, "create table if not exists tu%d using st2 tags(%d)", i, i);
1✔
843

844
    TAOS_RES* px = taos_query(pConn, str);
1!
845
    if (taos_errno(px) != 0) {
1!
846
      (void)printf("failed to create table tu, reason:%s\n", taos_errstr(px));
×
847
    }
848
    taos_free_result(px);
1!
849
  }
850

851
  for(int32_t j = 0; j < 1; ++j) {
2✔
852
    start += 20;
1✔
853
    for (int32_t i = 0; i < 1; ++i) {
2✔
854
      createNewTable(pConn, i, 10000, 0, pstr);
1!
855
    }
856
  }
857

858
  pRes = taos_query(pConn, "select * from abc1.st2");
1!
859
  if (taos_errno(pRes) != 0) {
1!
860
    (void)printf("failed to select from table, reason:%s\n", taos_errstr(pRes));
×
861
    taos_free_result(pRes);
×
862
    ASSERT_TRUE(false);
×
863
  }
864

865
  TAOS_ROW    pRow = NULL;
1✔
866
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
867
  int32_t     numOfFields = taos_num_fields(pRes);
1!
868

869
  char str[512] = {0};
1✔
870
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
10,001!
871
    //      int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
872
    //      (void)printf("%s\n", str);
873
  }
874

875
  taos_free_result(pRes);
1!
876
  taos_close(pConn);
1!
877
}
878

879
TEST(clientCase, tsbs_perf_test) {
4✔
880
  TdThread qid[20] = {0};
1✔
881

882
  for (int32_t i = 0; i < numOfThreads; ++i) {
2✔
883
    (void)taosThreadCreate(&qid[i], NULL, queryThread, NULL);
1!
884
  }
885
  for (int32_t i = 0; i < numOfThreads; ++i) {
2✔
886
    (void)taosThreadJoin(qid[i], NULL);
1!
887
  }
888
}
1✔
889

890
TEST(clientCase, projection_query_stables) {
4✔
891
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
892
  ASSERT_NE(pConn, nullptr);
1!
893

894
  TAOS_RES* pRes = taos_query(pConn, "explain select * from dbvg.st where tbname='ct1'");
1!
895

896
  TAOS_ROW    pRow = NULL;
1✔
897
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
898
  int32_t     numOfFields = taos_num_fields(pRes);
1!
899

900
  int32_t numOfRows = 0;
1✔
901
  int32_t i = 0;
1✔
902
  int32_t prev = 0;
1✔
903

904
  char    str[512] = {0};
1✔
905
  while (1) {
906
    pRow = taos_fetch_row(pRes);
1!
907
    if (pRow == NULL) {
1!
908
      break;
1✔
909
    }
910
    i += numOfRows;
×
911

912
    if ( (i / 1000000) > prev) {
×
913
      (void)printf("%d\n", i);
×
914
      prev = i/1000000;
×
915
    }
916
    //(void)printf("%d\n", i);
917
  }
918

919
  taos_free_result(pRes);
1!
920
  taos_close(pConn);
1!
921
}
922

923
TEST(clientCase, agg_query_tables) {
4✔
924
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
925
  ASSERT_NE(pConn, nullptr);
1!
926

927
  TAOS_RES* pRes = taos_query(pConn, "use abc1");
1!
928
  if (taos_errno(pRes) != 0) {
1!
929
    (void)printf("failed to use db, reason:%s\n", taos_errstr(pRes));
×
930
    taos_free_result(pRes);
×
931
    ASSERT_TRUE(false);
×
932
  }
933
  taos_free_result(pRes);
1!
934

935
  int64_t st = 1685959293299;
1✔
936
  for (int32_t i = 0; i < 5; ++i) {
6✔
937
    char s[256] = {0};
5✔
938

939
    while (1) {
940
      (void)sprintf(s, "insert into t1 values(%ld, %d)", st + i, i);
5✔
941
      pRes = taos_query(pConn, s);
5!
942

943
      int32_t ret = taos_errno(pRes);
5!
944

945
      if (ret != 0) {
5!
946
        (void)printf("failed to insert into table, reason:%s\n", taos_errstr(pRes));
5!
947
      }
948
      taos_free_result(pRes);
5!
949
      break;
5✔
950
    }
951
  }
952
  taos_close(pConn);
1!
953
}
954

955
// --- copy the following script in the shell to setup the environment ---
956
// 
957
// create database test;
958
// use test;
959
// create table m1(ts timestamp, k int) tags(a int);
960
// create table tm0 using m1 tags(1);
961
// create table tm1 using m1 tags(2);
962
// insert into tm0 values('2021-1-1 1:1:1.120', 1) ('2021-1-1 1:1:2.9', 2) tm1 values('2021-1-1 1:1:1.120', 11) ('2021-1-1
963
// 1:1:2.99', 22);
964

965
TEST(clientCase, async_api_test) {
4✔
966
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
967
  ASSERT_NE(pConn, nullptr);
1!
968

969
  TAOS_RES* pRes = taos_query(pConn, "use abc1");
1!
970
  if (taos_errno(pRes) != 0) {
1!
971
    (void)printf("failed to use db, reason:%s\n", taos_errstr(pRes));
×
972
    taos_free_result(pRes);
×
973
    taos_close(pConn);
×
974
    return;
×
975
  }
976
  taos_free_result(pRes);
1!
977

978
  pRes = taos_query(pConn, "insert into tu(ts) values('2022-02-27 12:12:61')");
1!
979
  if (taos_errno(pRes) != 0) {
1!
980
    (void)printf("failed, reason:%s\n", taos_errstr(pRes));
1!
981
  }
982

983
  int32_t     n = 0;
1✔
984
  TAOS_ROW    pRow = NULL;
1✔
985
  TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
986
  int32_t     numOfFields = taos_num_fields(pRes);
1!
987

988
  char str[512] = {0};
1✔
989
  while ((pRow = taos_fetch_row(pRes)) != NULL) {
1!
990
    int32_t* length = taos_fetch_lengths(pRes);
×
991
    for (int32_t i = 0; i < numOfFields; ++i) {
×
992
      (void)printf("(%d):%d ", i, length[i]);
×
993
    }
994
    (void)printf("\n");
×
995

996
    int32_t code = taos_print_row(str, pRow, pFields, numOfFields);
×
997
    (void)printf("%s\n", str);
×
998
    (void)memset(str, 0, sizeof(str));
×
999
  }
1000
  taos_free_result(pRes);
1!
1001
  tsem_t sem;
1002
  (void)tsem_init(&sem, 0, 0);
1!
1003
  taos_query_a(pConn, "select count(*) from tu", queryCallback, &sem);
1!
1004
  tsem_wait(&sem);
1!
1005
  tsem_destroy(&sem);
1!
1006
  taos_close(pConn);
1!
1007
}
1008

1009
TEST(clientCase, update_test) {
4✔
1010
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
1011
  ASSERT_NE(pConn, nullptr);
1!
1012

1013
  TAOS_RES* pRes = taos_query(pConn, "select cast(0 as timestamp)-1y");
1!
1014
  if (taos_errno(pRes) != TSDB_CODE_SUCCESS) {
1!
1015
    (void)printf("failed to create database, code:%s", taos_errstr(pRes));
×
1016
    taos_free_result(pRes);
×
1017
    taos_close(pConn);
×
1018
    return;
×
1019
  }
1020

1021
  taos_free_result(pRes);
1!
1022

1023
  pRes = taos_query(pConn, "use abc1");
1!
1024
  if (taos_errno(pRes) != TSDB_CODE_SUCCESS) {
1!
1025
    (void)printf("failed to use db, code:%s", taos_errstr(pRes));
×
1026
    taos_free_result(pRes);
×
1027
    taos_close(pConn);
×
1028
    return;
×
1029
  }
1030
  taos_free_result(pRes);
1!
1031

1032
  pRes = taos_query(pConn, "create table tup (ts timestamp, k int);");
1!
1033
  if (taos_errno(pRes) != 0) {
1!
1034
    (void)printf("failed to create table, reason:%s", taos_errstr(pRes));
×
1035
  }
1036

1037
  taos_free_result(pRes);
1!
1038

1039
  char s[256] = {0};
1✔
1040
  for (int32_t i = 0; i < 10; ++i) {
11✔
1041
    (void)sprintf(s, "insert into tup values(now+%da, %d)", i, i);
10✔
1042
    pRes = taos_query(pConn, s);
10!
1043
    taos_free_result(pRes);
10!
1044
  }
1045
  taos_close(pConn);
1!
1046
}
1047

1048
TEST(clientCase, sub_db_test) {
4✔
1049
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
1050
  ASSERT_NE(pConn, nullptr);
1!
1051

1052
  //  TAOS_RES* pRes = taos_query(pConn, "create topic topic_t1 as select * from t1");
1053
  //  if (taos_errno(pRes) != TSDB_CODE_SUCCESS) {
1054
  //    (void)printf("failed to create topic, code:%s", taos_errstr(pRes));
1055
  //    taos_free_result(pRes);
1056
  //    return;
1057
  //  }
1058

1059
  tmq_conf_t* conf = tmq_conf_new();
1!
1060
  (void)tmq_conf_set(conf, "enable.auto.commit", "true");
1!
1061
  (void)tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
1!
1062
  (void)tmq_conf_set(conf, "group.id", "cgrpNamedb");
1!
1063
  (void)tmq_conf_set(conf, "td.connect.user", "root");
1!
1064
  (void)tmq_conf_set(conf, "td.connect.pass", "taosdata");
1!
1065
  (void)tmq_conf_set(conf, "auto.offset.reset", "earliest");
1!
1066
  (void)tmq_conf_set(conf, "experimental.snapshot.enable", "false");
1!
1067
  (void)tmq_conf_set(conf, "msg.with.table.name", "true");
1!
1068
  (void)tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, NULL);
1!
1069

1070
  tmq_t* tmq = tmq_consumer_new(conf, NULL, 0);
1!
1071
  tmq_conf_destroy(conf);
1!
1072

1073
  // 创建订阅 topics 列表
1074
  tmq_list_t* topicList = tmq_list_new();
1!
1075
  (void)tmq_list_append(topicList, "topic_t1");
1!
1076
//  tmq_list_append(topicList, "topic_s2");
1077

1078
  // 启动订阅
1079
  (void)tmq_subscribe(tmq, topicList);
1!
1080
  tmq_list_destroy(topicList);
1!
1081

1082
  TAOS_FIELD* fields = NULL;
1✔
1083
  int32_t     numOfFields = 0;
1✔
1084
  int32_t     precision = 0;
1✔
1085
  int32_t     totalRows = 0;
1✔
1086
  int32_t     msgCnt = 0;
1✔
1087
  int32_t     timeout = 5000;
1✔
1088

1089
  int32_t count = 0;
1✔
1090

1091
  while (1) {
1092
    TAOS_RES* pRes = tmq_consumer_poll(tmq, timeout);
1!
1093
    if (pRes) {
1!
1094
      char    buf[1024];
1095
      int32_t rows = 0;
×
1096

1097
      const char* topicName = tmq_get_topic_name(pRes);
×
1098
      const char* dbName = tmq_get_db_name(pRes);
×
1099
      int32_t     vgroupId = tmq_get_vgroup_id(pRes);
×
1100

1101
      (void)printf("topic: %s\n", topicName);
×
1102
      (void)printf("db: %s\n", dbName);
×
1103
      (void)printf("vgroup id: %d\n", vgroupId);
×
1104

1105
      if (count++ > 200) {
×
1106
        (void)tmq_unsubscribe(tmq);
×
1107
        break;
×
1108
      }
1109

1110
      while (1) {
1111
        TAOS_ROW row = taos_fetch_row(pRes);
×
1112
        if (row == NULL) break;
×
1113

1114
        fields = taos_fetch_fields(pRes);
×
1115
        ASSERT_NE(fields, nullptr);
×
1116
        numOfFields = taos_field_count(pRes);
×
1117
        precision = taos_result_precision(pRes);
×
1118
        rows++;
×
1119
        (void)taos_print_row(buf, row, fields, numOfFields);
×
1120
        (void)printf("precision: %d, row content: %s\n", precision, buf);
×
1121
      }
×
1122
      taos_free_result(pRes);
×
1123
    } else {
1124
      break;
1✔
1125
    }
1126
  }
×
1127

1128
  (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
1!
1129
  taos_close(pConn);
1!
1130
}
1131

1132
TEST(clientCase, tmq_commit) {
4✔
1133
//  taos_options(TSDB_OPTION_CONFIGDIR, "~/first/cfg");
1134

1135
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
1136
  ASSERT_NE(pConn, nullptr);
1!
1137

1138
  tmq_conf_t* conf = tmq_conf_new();
1!
1139

1140
  (void)tmq_conf_set(conf, "enable.auto.commit", "false");
1!
1141
  (void)tmq_conf_set(conf, "auto.commit.interval.ms", "2000");
1!
1142
  (void)tmq_conf_set(conf, "group.id", "group_id_2");
1!
1143
  (void)tmq_conf_set(conf, "td.connect.user", "root");
1!
1144
  (void)tmq_conf_set(conf, "td.connect.pass", "taosdata");
1!
1145
  (void)tmq_conf_set(conf, "auto.offset.reset", "earliest");
1!
1146
  (void)tmq_conf_set(conf, "msg.with.table.name", "true");
1!
1147

1148
  tmq_t* tmq = tmq_consumer_new(conf, NULL, 0);
1!
1149
  tmq_conf_destroy(conf);
1!
1150

1151
  char topicName[128] = "tp";
1✔
1152
  // 创建订阅 topics 列表
1153
  tmq_list_t* topicList = tmq_list_new();
1!
1154
  (void)tmq_list_append(topicList, topicName);
1!
1155

1156
  // 启动订阅
1157
  (void)tmq_subscribe(tmq, topicList);
1!
1158
  tmq_list_destroy(topicList);
1!
1159

1160
  int32_t     totalRows = 0;
1✔
1161
  int32_t     msgCnt = 0;
1✔
1162
  int32_t     timeout = 2000;
1✔
1163

1164
  tmq_topic_assignment* pAssign = NULL;
1✔
1165
  int32_t numOfAssign = 0;
1✔
1166

1167
  int32_t code = tmq_get_topic_assignment(tmq, topicName, &pAssign, &numOfAssign);
1!
1168
  if (code != 0) {
1!
1169
    (void)printf("error occurs:%s\n", tmq_err2str(code));
1!
1170
    tmq_free_assignment(pAssign);
1!
1171
    (void)tmq_consumer_close(tmq);
1!
1172
    taos_close(pConn);
1!
1173
    (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
1!
1174
    return;
1✔
1175
  }
1176

1177
  for(int i = 0; i < numOfAssign; i++){
×
1178
    tmq_topic_assignment* pa = &pAssign[i];
×
1179
    std::cout << "assign i:" << i << ", vgId:" << pa->vgId << ", offset:" << pa->currentOffset << ", start:%"
×
1180
              << pa->begin << ", end:%" << pa->end << std::endl;
×
1181

1182
    int64_t committed = tmq_committed(tmq, topicName, pa->vgId);
×
1183
    std::cout << "committed vgId:" << pa->vgId << " committed:" << committed << std::endl;
×
1184

1185
    int64_t position = tmq_position(tmq, topicName, pa->vgId);
×
1186
    std::cout << "position vgId:" << pa->vgId << ", position:" << position << std::endl;
×
1187

1188
    (void)tmq_offset_seek(tmq, topicName, pa->vgId, 1);
×
1189
    position = tmq_position(tmq, topicName, pa->vgId);
×
1190
    std::cout << "after seek 1, position vgId:" << pa->vgId << " position:" << position << std::endl;
×
1191
  }
1192

1193
  while (1) {
1194
    (void)printf("start to poll\n");
×
1195
    TAOS_RES* pRes = tmq_consumer_poll(tmq, timeout);
×
1196
    if (pRes) {
×
1197
      printSubResults(pRes, &totalRows);
×
1198
    } else {
1199
      break;
×
1200
    }
1201

1202
    (void)tmq_commit_sync(tmq, pRes);
×
1203
    for(int i = 0; i < numOfAssign; i++) {
×
1204
      int64_t committed = tmq_committed(tmq, topicName, pAssign[i].vgId);
×
1205
      std::cout << "committed vgId:" << pAssign[i].vgId << " , committed:" << committed << std::endl;
×
1206
      if(committed > 0){
×
1207
        int32_t code = tmq_commit_offset_sync(tmq, topicName, pAssign[i].vgId, 4);
×
1208
        (void)printf("tmq_commit_offset_sync vgId:%d, offset:4, code:%d\n", pAssign[i].vgId, code);
×
1209
        int64_t committed = tmq_committed(tmq, topicName, pAssign[i].vgId);
×
1210

1211
        std::cout << "after tmq_commit_offset_sync, committed vgId:" << pAssign[i].vgId << ", committed:" << committed
×
1212
                  << std::endl;
×
1213
      }
1214
    }
1215
    if (pRes != NULL) {
×
1216
      taos_free_result(pRes);
×
1217
    }
1218

1219
//    tmq_offset_seek(tmq, "tp", pAssign[0].vgId, pAssign[0].begin);
1220
  }
×
1221

1222
  tmq_free_assignment(pAssign);
×
1223

1224
  (void)tmq_consumer_close(tmq);
×
1225
  taos_close(pConn);
×
1226
  (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
×
1227
}
1228
namespace {
1229
void doPrintInfo(tmq_topic_assignment* pa, int32_t index) {
×
1230
  std::cout << "assign i:" << index << ", vgId:" << pa->vgId << ", offset:%" << pa->currentOffset << ", start:%"
×
1231
            << pa->begin << ", end:%" << pa->end << std::endl;
×
1232
}
×
1233
}
1234
TEST(clientCase, td_25129) {
4✔
1235
//  taos_options(TSDB_OPTION_CONFIGDIR, "~/first/cfg");
1236

1237
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
1238
  ASSERT_NE(pConn, nullptr);
1!
1239

1240
  tmq_conf_t* conf = tmq_conf_new();
1!
1241

1242
  (void)tmq_conf_set(conf, "enable.auto.commit", "false");
1!
1243
  (void)tmq_conf_set(conf, "auto.commit.interval.ms", "2000");
1!
1244
  (void)tmq_conf_set(conf, "group.id", "group_id_2");
1!
1245
  (void)tmq_conf_set(conf, "td.connect.user", "root");
1!
1246
  (void)tmq_conf_set(conf, "td.connect.pass", "taosdata");
1!
1247
  (void)tmq_conf_set(conf, "auto.offset.reset", "earliest");
1!
1248
  (void)tmq_conf_set(conf, "msg.with.table.name", "true");
1!
1249

1250
  tmq_t* tmq = tmq_consumer_new(conf, NULL, 0);
1!
1251
  ASSERT_NE(tmq, nullptr);
1!
1252
  tmq_conf_destroy(conf);
1!
1253

1254
  char topicName[128] = "tp";
1✔
1255
  // 创建订阅 topics 列表
1256
  tmq_list_t* topicList = tmq_list_new();
1!
1257
  (void)tmq_list_append(topicList, topicName);
1!
1258

1259
  // 启动订阅
1260
  (void)tmq_subscribe(tmq, topicList);
1!
1261
  tmq_list_destroy(topicList);
1!
1262

1263
  TAOS_FIELD* fields = NULL;
1✔
1264
  int32_t     numOfFields = 0;
1✔
1265
  int32_t     precision = 0;
1✔
1266
  int32_t     totalRows = 0;
1✔
1267
  int32_t     msgCnt = 0;
1✔
1268
  int32_t     timeout = 2000;
1✔
1269

1270
  int32_t count = 0;
1✔
1271

1272
  tmq_topic_assignment* pAssign = NULL;
1✔
1273
  int32_t numOfAssign = 0;
1✔
1274

1275
  int32_t code = tmq_get_topic_assignment(tmq, topicName, &pAssign, &numOfAssign);
1!
1276
  if (code != 0) {
1!
1277
    (void)printf("error occurs:%s\n", tmq_err2str(code));
1!
1278
    tmq_free_assignment(pAssign);
1!
1279
    (void)tmq_consumer_close(tmq);
1!
1280
    taos_close(pConn);
1!
1281
    (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
1!
1282
    return;
1✔
1283
  }
1284

1285
  for(int i = 0; i < numOfAssign; i++){
×
1286
    doPrintInfo(&pAssign[i], i);
×
1287
  }
1288

1289
//  tmq_offset_seek(tmq, "tp", pAssign[0].vgId, 4);
1290
  tmq_free_assignment(pAssign);
×
1291

1292
  code = tmq_get_topic_assignment(tmq, topicName, &pAssign, &numOfAssign);
×
1293
  if (code != 0) {
×
1294
    (void)printf("error occurs:%s\n", tmq_err2str(code));
×
1295
    tmq_free_assignment(pAssign);
×
1296
    (void)tmq_consumer_close(tmq);
×
1297
    taos_close(pConn);
×
1298
    (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
×
1299
    return;
×
1300
  }
1301

1302
  for(int i = 0; i < numOfAssign; i++){
×
1303
    doPrintInfo(&pAssign[i], i);
×
1304
  }
1305

1306
  tmq_free_assignment(pAssign);
×
1307

1308
  code = tmq_get_topic_assignment(tmq, topicName, &pAssign, &numOfAssign);
×
1309
  if (code != 0) {
×
1310
    (void)printf("error occurs:%s\n", tmq_err2str(code));
×
1311
    tmq_free_assignment(pAssign);
×
1312
    (void)tmq_consumer_close(tmq);
×
1313
    taos_close(pConn);
×
1314
    (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
×
1315
    return;
×
1316
  }
1317

1318
  for(int i = 0; i < numOfAssign; i++){
×
1319
    int64_t committed = tmq_committed(tmq, topicName, pAssign[i].vgId);
×
1320
    doPrintInfo(&pAssign[i], i);
×
1321
  }
1322

1323
  while (1) {
1324
    (void)printf("start to poll\n");
×
1325
    TAOS_RES* pRes = tmq_consumer_poll(tmq, timeout);
×
1326
    if (pRes) {
×
1327
      char buf[128];
1328

1329
      const char* topicName = tmq_get_topic_name(pRes);
×
1330
//      const char* dbName = tmq_get_db_name(pRes);
1331
//      int32_t     vgroupId = tmq_get_vgroup_id(pRes);
1332
//
1333
//      (void)printf("topic: %s\n", topicName);
1334
//      (void)printf("db: %s\n", dbName);
1335
//      (void)printf("vgroup id: %d\n", vgroupId);
1336

1337
      printSubResults(pRes, &totalRows);
×
1338

1339
      code = tmq_get_topic_assignment(tmq, topicName, &pAssign, &numOfAssign);
×
1340
      if (code != 0) {
×
1341
        (void)printf("error occurs:%s\n", tmq_err2str(code));
×
1342
        tmq_free_assignment(pAssign);
×
1343
        (void)tmq_consumer_close(tmq);
×
1344
        taos_close(pConn);
×
1345
        (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
×
1346
        return;
×
1347
      }
1348

1349
      for(int i = 0; i < numOfAssign; i++){
×
1350
        doPrintInfo(&pAssign[i], i);
×
1351
      }
1352
    } else {
1353
      for(int i = 0; i < numOfAssign; i++) {
×
1354
        (void)tmq_offset_seek(tmq, topicName, pAssign[i].vgId, pAssign[i].currentOffset);
×
1355
      }
1356
      (void)tmq_commit_sync(tmq, pRes);
×
1357
      break;
×
1358
    }
1359

1360
//    tmq_commit_sync(tmq, pRes);
1361
    if (pRes != NULL) {
×
1362
      taos_free_result(pRes);
×
1363
      //      if ((++count) > 1) {
1364
      //        break;
1365
      //      }
1366
    } else {
1367
      break;
×
1368
    }
1369

1370
//    tmq_offset_seek(tmq, "tp", pAssign[0].vgId, pAssign[0].begin);
1371
  }
×
1372

1373
  tmq_free_assignment(pAssign);
×
1374

1375
  code = tmq_get_topic_assignment(tmq, "tp", &pAssign, &numOfAssign);
×
1376
  if (code != 0) {
×
1377
    (void)printf("error occurs:%s\n", tmq_err2str(code));
×
1378
    tmq_free_assignment(pAssign);
×
1379
    (void)tmq_consumer_close(tmq);
×
1380
    taos_close(pConn);
×
1381
    (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
×
1382
    return;
×
1383
  }
1384

1385
  for(int i = 0; i < numOfAssign; i++){
×
1386
    doPrintInfo(&pAssign[i], i);
×
1387
  }
1388

1389
  tmq_free_assignment(pAssign);
×
1390
  (void)tmq_consumer_close(tmq);
×
1391
  taos_close(pConn);
×
1392
  (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
×
1393
}
1394

1395
TEST(clientCase, sub_tb_test) {
4✔
1396
  (void)taos_options(TSDB_OPTION_CONFIGDIR, "~/first/cfg");
1!
1397

1398
  TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
1399
  ASSERT_NE(pConn, nullptr);
1!
1400

1401
  tmq_conf_t* conf = tmq_conf_new();
1!
1402

1403
  int32_t ts = taosGetTimestampMs()%INT32_MAX;
1✔
1404
  char consumerGroupid[128] = {0};
1✔
1405
  (void)sprintf(consumerGroupid, "group_id_%d", ts);
1✔
1406

1407
  (void)tmq_conf_set(conf, "enable.auto.commit", "true");
1!
1408
  (void)tmq_conf_set(conf, "auto.commit.interval.ms", "2000");
1!
1409
  (void)tmq_conf_set(conf, "group.id", consumerGroupid);
1!
1410
  (void)tmq_conf_set(conf, "td.connect.user", "root");
1!
1411
  (void)tmq_conf_set(conf, "td.connect.pass", "taosdata");
1!
1412
  (void)tmq_conf_set(conf, "auto.offset.reset", "earliest");
1!
1413
  (void)tmq_conf_set(conf, "experimental.snapshot.enable", "false");
1!
1414
  (void)tmq_conf_set(conf, "msg.with.table.name", "true");
1!
1415
  (void)tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, NULL);
1!
1416

1417
  tmq_t* tmq = tmq_consumer_new(conf, NULL, 0);
1!
1418
  ASSERT_NE(tmq, nullptr);
1!
1419
  tmq_conf_destroy(conf);
1!
1420

1421
  // 创建订阅 topics 列表
1422
  tmq_list_t* topicList = tmq_list_new();
1!
1423
  (void)tmq_list_append(topicList, "t1");
1!
1424

1425
  // 启动订阅
1426
  (void)tmq_subscribe(tmq, topicList);
1!
1427
  tmq_list_destroy(topicList);
1!
1428

1429
  TAOS_FIELD* fields = NULL;
1✔
1430
  int32_t     numOfFields = 0;
1✔
1431
  int32_t     precision = 0;
1✔
1432
  int32_t     totalRows = 0;
1✔
1433
  int32_t     msgCnt = 0;
1✔
1434
  int32_t     timeout = 2500000;
1✔
1435

1436
  int32_t count = 0;
1✔
1437

1438
  tmq_topic_assignment* pAssign = NULL;
1✔
1439
  int32_t numOfAssign = 0;
1✔
1440

1441
  int32_t code = tmq_get_topic_assignment(tmq, "t1", &pAssign, &numOfAssign);
1!
1442
  if (code != 0) {
1!
1443
    (void)printf("error occurs:%s\n", tmq_err2str(code));
1!
1444
    (void)tmq_consumer_close(tmq);
1!
1445
    taos_close(pConn);
1!
1446
    (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
1!
1447
    return;
1✔
1448
  }
1449

1450
  (void)tmq_offset_seek(tmq, "t1", pAssign[0].vgId, 4);
×
1451

1452
  code = tmq_get_topic_assignment(tmq, "t1", &pAssign, &numOfAssign);
×
1453
  if (code != 0) {
×
1454
    (void)printf("error occurs:%s\n", tmq_err2str(code));
×
1455
    (void)tmq_consumer_close(tmq);
×
1456
    taos_close(pConn);
×
1457
    (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
×
1458
    return;
×
1459
  }
1460

1461
  while (1) {
1462
    TAOS_RES* pRes = tmq_consumer_poll(tmq, timeout);
×
1463
    if (pRes) {
×
1464
      char buf[128] = {0};
×
1465

1466
      const char* topicName = tmq_get_topic_name(pRes);
×
1467
//      const char* dbName = tmq_get_db_name(pRes);
1468
//      int32_t     vgroupId = tmq_get_vgroup_id(pRes);
1469
//
1470
//      (void)printf("topic: %s\n", topicName);
1471
//      (void)printf("db: %s\n", dbName);
1472
//      (void)printf("vgroup id: %d\n", vgroupId);
1473

1474
      printSubResults(pRes, &totalRows);
×
1475
    } else {
1476
//      tmq_offset_seek(tmq, "topic_t1", pAssign[0].vgroupHandle, pAssign[0].begin);
1477
//      break;
1478
    }
1479

1480
    (void)tmq_commit_sync(tmq, pRes);
×
1481
    if (pRes != NULL) {
×
1482
      taos_free_result(pRes);
×
1483
      //      if ((++count) > 1) {
1484
      //        break;
1485
      //      }
1486
    } else {
1487
      break;
×
1488
    }
1489

1490
    (void)tmq_offset_seek(tmq, "topic_t1", pAssign[0].vgId, pAssign[0].begin);
×
1491
  }
×
1492

1493
  (void)tmq_consumer_close(tmq);
×
1494
  taos_close(pConn);
×
1495
  (void)fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
×
1496
}
1497

1498
TEST(clientCase, sub_tb_mt_test) {
4✔
1499
  char *user = NULL;
1✔
1500
  char *auth = NULL;
1✔
1501
  char *ip = NULL;
1✔
1502
  int port = 0;
1✔
1503
  char key[512] = {0};
1✔
1504
  (void)snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port);
1✔
1505

1506
  (void)taos_options(TSDB_OPTION_CONFIGDIR, "~/first/cfg");
1!
1507
  TdThread qid[20] = {0};
1✔
1508

1509
  for (int32_t i = 0; i < 1; ++i) {
2✔
1510
    (void)taosThreadCreate(&qid[i], NULL, doConsumeData, NULL);
1!
1511
  }
1512

1513
  for (int32_t i = 0; i < 1; ++i) {
2✔
1514
    (void)taosThreadJoin(qid[i], NULL);
1!
1515
  }
1516
}
1✔
1517

1518
TEST(clientCase, timezone_Test) {
4✔
1519
  {
1520
    // taos_options(  TSDB_OPTION_TIMEZONE, "UTC-8");
1521
    int code = taos_options(TSDB_OPTION_TIMEZONE, "UTC-8");
1!
1522
    ASSERT_TRUE(code ==  0);
1!
1523
    TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
1524
    ASSERT_NE(pConn, nullptr);
1!
1525

1526
    TAOS_RES* pRes = taos_query(pConn, "drop database if exists db_timezone");
1!
1527
    ASSERT_EQ(taos_errno(pRes), TSDB_CODE_SUCCESS);
1!
1528
    taos_free_result(pRes);
1!
1529

1530
    pRes = taos_query(pConn, "create database db_timezone");
1!
1531
    ASSERT_EQ(taos_errno(pRes), TSDB_CODE_SUCCESS);
1!
1532
    taos_free_result(pRes);
1!
1533

1534
    pRes = taos_query(pConn, "create table db_timezone.t1 (ts timestamp, v int)");
1!
1535
    while (taos_errno(pRes) == TSDB_CODE_MND_DB_IN_CREATING || taos_errno(pRes) == TSDB_CODE_MND_DB_IN_DROPPING) {
1!
1536
      taosMsleep(2000);
×
1537
      pRes = taos_query(pConn, "create table db_timezone.t1 (ts timestamp, v int)");
×
1538
    }
1539
    ASSERT_EQ(taos_errno(pRes), TSDB_CODE_SUCCESS);
1!
1540
    taos_free_result(pRes);
1!
1541

1542
    char sql[256] = {0};
1✔
1543
    (void)sprintf(sql, "insert into db_timezone.t1 values('2023-09-16 17:00:00', 1)");
1✔
1544
    pRes = taos_query(pConn, sql);
1!
1545
    ASSERT_EQ(taos_errno(pRes), TSDB_CODE_SUCCESS);
1!
1546
    taos_free_result(pRes);
1!
1547

1548
    pRes = taos_query(pConn, "select * from db_timezone.t1 where ts == '2023-09-16 17:00:00'");
1!
1549
    ASSERT_EQ(taos_errno(pRes), TSDB_CODE_SUCCESS);
1!
1550

1551
    TAOS_ROW    pRow = NULL;
1✔
1552
    TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
1553
    int32_t     numOfFields = taos_num_fields(pRes);
1!
1554

1555
    char str[512] = {0};
1✔
1556
    int rows =  0;
1✔
1557
    while ((pRow = taos_fetch_row(pRes)) != NULL) {
2!
1558
      rows++;
1✔
1559
    }
1560
    ASSERT_TRUE(rows == 1);
1!
1561

1562
    taos_free_result(pRes);
1!
1563

1564
    taos_close(pConn);
1!
1565
  }
1566

1567
  {
1568
    // taos_options(  TSDB_OPTION_TIMEZONE, "UTC+8");
1569
    int code = taos_options(TSDB_OPTION_TIMEZONE, "UTC+8");
1!
1570
    ASSERT_TRUE(code ==  0);
1!
1571
    TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
1572
    ASSERT_NE(pConn, nullptr);
1!
1573

1574
    TAOS_RES* pRes = taos_query(pConn, "select * from db_timezone.t1 where ts == '2023-09-16 01:00:00'");
1!
1575
    ASSERT_EQ(taos_errno(pRes), TSDB_CODE_SUCCESS);
1!
1576

1577
    TAOS_ROW    pRow = NULL;
1✔
1578
    TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
1579
    int32_t     numOfFields = taos_num_fields(pRes);
1!
1580

1581
    int rows =  0;
1✔
1582
    char str[512] = {0};
1✔
1583
    while ((pRow = taos_fetch_row(pRes)) != NULL) {
2!
1584
      rows++;
1✔
1585
    }
1586
    ASSERT_TRUE(rows == 1);
1!
1587

1588
    taos_free_result(pRes);
1!
1589

1590
    char sql[256] = {0};
1✔
1591
    (void)sprintf(sql, "insert into db_timezone.t1 values('2023-09-16 17:00:01', 1)");
1✔
1592
    pRes = taos_query(pConn, sql);
1!
1593
    ASSERT_EQ(taos_errno(pRes), TSDB_CODE_SUCCESS);
1!
1594

1595
    taos_free_result(pRes);
1!
1596

1597
    taos_close(pConn);
1!
1598
  }
1599

1600
  {
1601
    // taos_options(  TSDB_OPTION_TIMEZONE, "UTC+0");
1602
    int code = taos_options(TSDB_OPTION_TIMEZONE, "UTC+0");
1!
1603
    ASSERT_TRUE(code ==  0);
1!
1604
    TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0);
1!
1605
    ASSERT_NE(pConn, nullptr);
1!
1606

1607
    TAOS_RES* pRes = taos_query(pConn, "select * from db_timezone.t1 where ts == '2023-09-16 09:00:00'");
1!
1608
    ASSERT_EQ(taos_errno(pRes), TSDB_CODE_SUCCESS);
1!
1609

1610
    TAOS_ROW    pRow = NULL;
1✔
1611
    TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
1612
    int32_t     numOfFields = taos_num_fields(pRes);
1!
1613

1614
    int rows =  0;
1✔
1615
    char str[512] = {0};
1✔
1616
    while ((pRow = taos_fetch_row(pRes)) != NULL) {
2!
1617
      rows++;
1✔
1618
    }
1619
    ASSERT_TRUE(rows == 1);
1!
1620
    taos_free_result(pRes);
1!
1621

1622
    {
1623
      TAOS_RES* pRes = taos_query(pConn, "select * from db_timezone.t1 where ts == '2023-09-17 01:00:01'");
1!
1624
      ASSERT_EQ(taos_errno(pRes), TSDB_CODE_SUCCESS);
1!
1625

1626
      TAOS_ROW    pRow = NULL;
1✔
1627
      TAOS_FIELD* pFields = taos_fetch_fields(pRes);
1!
1628
      int32_t     numOfFields = taos_num_fields(pRes);
1!
1629

1630
      int  rows = 0;
1✔
1631
      char str[512] = {0};
1✔
1632
      while ((pRow = taos_fetch_row(pRes)) != NULL) {
2!
1633
        rows++;
1✔
1634
      }
1635
      ASSERT_TRUE(rows == 1);
1!
1636
      taos_free_result(pRes);
1!
1637
    }
1638

1639
    taos_close(pConn);
1!
1640
  }
1641
}
1642
#pragma GCC diagnostic pop
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc