@@ -28,7 +28,12 @@ def do_query(query, port=8000, session=None, node_id=None, wait=100):
28
28
return response
29
29
30
30
31
- def test_txn ():
31
+ def get_txn_state (resp ):
32
+ return (resp .get ("state" ) == "Succeeded" ,
33
+ resp .get ("session" ).get ("need_sticky" ),
34
+ resp .get ("session" ).get ("txn_state" ))
35
+
36
+ def test_txn_success ():
32
37
resp = do_query ("create or replace table t1(a int)" ).json ()
33
38
assert not (resp .get ("session" ).get ("need_sticky" )), resp
34
39
@@ -37,21 +42,54 @@ def test_txn():
37
42
node_id = resp .get ("node_id" )
38
43
session = resp .get ("session" )
39
44
40
- # can not find txn state in node 2
41
- resp = do_query ("insert into t1 values (1)" , port = 8002 , session = session ).json ()
42
- assert resp .get ("state" ) == "Failed" , resp .text
43
-
44
45
# forward to node 1
45
46
resp = do_query (
46
47
"insert into t1 values (2)" , port = 8002 , session = session , node_id = node_id
47
48
).json ()
48
- assert resp .get ("state" ) == "Succeeded" , resp
49
- assert resp .get ("session" ).get ("need_sticky" ), resp
49
+ assert get_txn_state (resp ) == (True , True , "Active" ), resp
50
50
51
51
# return need_sticky = false after commit
52
52
resp = do_query ("commit" ).json ()
53
+ assert get_txn_state (resp ) == (True , False , "AutoCommit" ), resp
54
+
55
+
56
+ def test_txn_fail ():
57
+ resp = do_query ("create or replace table t1(a int)" ).json ()
53
58
assert not (resp .get ("session" ).get ("need_sticky" )), resp
54
59
60
+ resp = do_query ("begin" ).json ()
61
+ assert resp .get ("session" ).get ("need_sticky" ), resp
62
+ node_id = resp .get ("node_id" )
63
+ session = resp .get ("session" )
64
+
65
+ # fail
66
+ resp = do_query (
67
+ "select 1/0" , session = session
68
+ ).json ()
69
+ assert get_txn_state (resp ) == (False , False , "Fail" ), resp
70
+
71
+ # fail because wrong node
72
+ resp = do_query (
73
+ "select 1" , port = 8002 , session = session
74
+ ).json ()
75
+ session = resp .get ("session" )
76
+ assert get_txn_state (resp ) == (False , False , "Fail" ), resp
77
+
78
+ # keep fail state until commit/abort
79
+ resp = do_query (
80
+ "select 1" , session = session , node_id = node_id
81
+ ).json ()
82
+ assert get_txn_state (resp ) == (False , False , "Fail" ), resp
83
+ session = resp .get ("session" )
84
+
85
+ # return need_sticky = false after commit
86
+ resp = do_query ("commit" , session = session ).json ()
87
+ assert get_txn_state (resp ) == (True , False , "AutoCommit" ), resp
88
+
89
+ # return need_sticky = false after abort
90
+ resp = do_query ("abort" , session = session ).json ()
91
+ assert get_txn_state (resp ) == (True , False , "AutoCommit" ), resp
92
+
55
93
56
94
def test_query ():
57
95
"""each query is sticky"""
@@ -104,7 +142,8 @@ def main():
104
142
return
105
143
106
144
test_query ()
107
- test_txn ()
145
+ test_txn_success ()
146
+ test_txn_fail ()
108
147
109
148
110
149
if __name__ == "__main__" :
0 commit comments