Skip to content

Commit

Permalink
Merge pull request #272 from taosdata/fix/td-32481
Browse files Browse the repository at this point in the history
fix: stmt2 init reqid uses random number when not provided
  • Loading branch information
YamingPei authored Oct 14, 2024
2 parents 67f2256 + 0b19430 commit a7680ee
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
9 changes: 8 additions & 1 deletion taos/statement2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding:UTF-8
from taos.cinterface import *
from taos.error import StatementError
from taos.result import TaosResult
Expand Down Expand Up @@ -40,8 +41,14 @@ def taos_stmt2_async_exec(userdata, result_set, error_code):


class TaosStmt2Option:
def __init__(self, reqid: int, single_stb_insert: bool=False, single_table_bind_once: bool=False, **kwargs):
def __init__(self, reqid: int=None, single_stb_insert: bool=False, single_table_bind_once: bool=False, **kwargs):
self._impl = TaosStmt2OptionImpl()
if reqid is None:
reqid = utils.gen_req_id()
else:
if type(reqid) is not int:
raise StatementError(f"reqid type error, expected int type but get {type(reqid)}.")

self.reqid = reqid
self.single_stb_insert = single_stb_insert
self.single_table_bind_once = single_table_bind_once
Expand Down
45 changes: 45 additions & 0 deletions tests/test_cinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,48 @@ def execute_sql(conn, sql):
taos_free_result(res)


global_reqid = 0
def test_taos_stmt2_option_default_reqid():
if not taos.IS_V3:
return

import threading

def worker():
global global_reqid
for i in range(5):
option = taos.TaosStmt2Option()
# print(f"Thread {threading.get_ident()}: reqid = {option.reqid}")
with threading.Lock():
assert option.reqid != global_reqid
global_reqid = option.reqid

threads = []
for i in range(3):
t = threading.Thread(target=worker)
threads.append(t)
t.start()

for t in threads:
t.join()

print("pass test_taos_stmt2_option_default_reqid")


def test_taos_stmt2_option_reqid_wrong_type():
if not taos.IS_V3:
return

try:
option = taos.TaosStmt2Option(reqid=True)
# should not get here
assert False
except StatementError as err:
pass

print("pass test_taos_stmt2_option_reqid_wrong_type")


def test_taos_stmt2_init_without_option():
if not taos.IS_V3:
return
Expand Down Expand Up @@ -607,3 +649,6 @@ def test_taos_stmt2_query():


############################################ stmt2 end ############################################

if __name__ == "__main__":
pass

0 comments on commit a7680ee

Please sign in to comment.