Skip to content

Commit 1d60a83

Browse files
committed
More comprehensive C++ API transaction tests
1 parent 01825e4 commit 1d60a83

21 files changed

Lines changed: 210 additions & 47 deletions

tests/cpp/test_tx_tester.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
"""Run tx_tester -f scripts against python/examples/broker.py."""
2121

22+
import glob
2223
import os
24+
import re
2325
import socket
2426
import subprocess
2527
import time
@@ -49,6 +51,29 @@ def _wait_for_port(host, port, attempts=100, interval=0.1):
4951
raise RuntimeError(f"timed out waiting for {host}:{port}")
5052

5153

54+
def _discover_tx_test_scripts():
55+
script_dir = os.environ.get("TX_TESTER_SCRIPTS")
56+
if not script_dir:
57+
return []
58+
pattern = os.path.join(script_dir, "*.tx_test")
59+
return sorted(os.path.basename(path) for path in glob.glob(pattern))
60+
61+
62+
def _test_method_name(script_name):
63+
stem = script_name.removesuffix(".tx_test")
64+
safe = re.sub(r"[^0-9A-Za-z_]", "_", stem)
65+
return f"test_{safe}"
66+
67+
68+
def _make_tx_test_method(script_name):
69+
def test_method(self):
70+
self.run_tx_test(script_name)
71+
72+
test_method.__name__ = _test_method_name(script_name)
73+
test_method.__doc__ = f"Run tx_tester script {script_name}."
74+
return test_method
75+
76+
5277
class TxTesterBrokerTest(unittest.TestCase):
5378
"""Shared broker fixture for tx_tester script files."""
5479

@@ -75,6 +100,7 @@ def setUpClass(cls):
75100
f"port {cls.port} already in use; stop other brokers before running"
76101
)
77102

103+
# Future: TX_TESTER_BROKER_CMD / TX_TESTER_BROKER_URL for other brokers.
78104
cls.broker = subprocess.Popen(
79105
[cls.python, cls.broker_script, "-t", cls.txn_timeout],
80106
stdout=subprocess.PIPE,
@@ -118,24 +144,15 @@ def run_tx_test(self, script_name):
118144
)
119145

120146

121-
class TestDeclare(TxTesterBrokerTest):
122-
def test_declare(self):
123-
self.run_tx_test("declare.tx_test")
124-
125-
126-
class TestCommitTimeout(TxTesterBrokerTest):
127-
def test_commit_timeout(self):
128-
self.run_tx_test("commit_timeout.tx_test")
129-
147+
class TestTxScriptSuite(TxTesterBrokerTest):
148+
"""One test method per *.tx_test script (methods added at import time)."""
130149

131-
class TestExplicitAbort(TxTesterBrokerTest):
132-
def test_explicit_abort(self):
133-
self.run_tx_test("explicit_abort.tx_test")
150+
pass
134151

135152

136-
class Test1(TxTesterBrokerTest):
137-
def test1(self):
138-
self.run_tx_test("test1.tx_test")
153+
for _script_name in _discover_tx_test_scripts():
154+
_method = _make_tx_test_method(_script_name)
155+
setattr(TestTxScriptSuite, _method.__name__, _method)
139156

140157

141158
if __name__ == "__main__":
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Setup: seed the examples queue with messages for later receive tests.
2+
# (Not an AMQP transaction test; prepares broker state for the suite.)
3+
send 25
4+
wait sent
5+
wait outgoing
6+
quit
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# AMQP 1.0 §4.6.2 — declare (amqp:declare:list) returns declared disposition with txn-id.
2+
declare
3+
wait declared
4+
expect ok $last_declare
5+
expect true $txn_declared
6+
expect @not @empty $txn_id
7+
quit
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# AMQP 1.0 §4.6.3 — discharge with fail=false (commit) completes successfully.
2+
declare
3+
wait declared
4+
commit
5+
wait discharged
6+
expect committed $last_discharge
7+
expect false $txn_declared
8+
expect @empty $error
9+
quit
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# AMQP 1.0 §4.6.3 — discharge with fail=true (rollback) completes without error.
2+
declare
3+
wait declared
4+
abort
5+
wait discharged
6+
expect aborted $last_discharge
7+
expect false $txn_declared
8+
expect @empty $error
9+
quit
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# AMQP 1.0 §4.6.3 + amqp:transaction:timeout — broker rejects commit after txn age exceeds limit.
2+
declare
3+
wait declared
4+
wait 3
5+
reset_status
6+
commit
7+
wait discharged
8+
expect aborted $last_discharge
9+
expect @not @empty $error
10+
expect false $txn_declared
11+
quit
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# AMQP 1.0 §4.4 / amqp:transactional-state:list — provisional accept finalized on commit.
2+
declare
3+
wait declared
4+
fetch 1
5+
wait fetched 1
6+
accept
7+
expect 1 $unsettled_in
8+
commit
9+
wait discharged
10+
expect committed $last_discharge
11+
expect 0 $unsettled_in
12+
expect @empty $error
13+
quit
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# AMQP 1.0 §4.6.3 — rollback of provisional accept; message remains available for delivery.
2+
declare
3+
wait declared
4+
fetch 1
5+
wait fetched 1
6+
accept
7+
expect 1 $unsettled_in
8+
abort
9+
wait discharged
10+
expect aborted $last_discharge
11+
expect 0 $unsettled_in
12+
expect @empty $error
13+
declare
14+
wait declared
15+
fetch 1
16+
wait 5 fetched 1
17+
expect 1 $fetch
18+
quit
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# AMQP 1.0 §4.4 / amqp:transactional-state:list — provisional reject finalized on commit.
2+
declare
3+
wait declared
4+
fetch 1
5+
wait fetched 1
6+
reject
7+
expect 1 $unsettled_in
8+
commit
9+
wait discharged
10+
expect committed $last_discharge
11+
expect 0 $unsettled_in
12+
expect @empty $error
13+
quit
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# AMQP 1.0 §4.4 / amqp:transactional-state:list — provisional release finalized on commit (requeue).
2+
declare
3+
wait declared
4+
fetch 1
5+
wait fetched 1
6+
release
7+
expect 1 $unsettled_in
8+
commit
9+
wait discharged
10+
expect committed $last_discharge
11+
expect 0 $unsettled_in
12+
declare
13+
wait declared
14+
fetch 1
15+
wait 5 fetched 1
16+
expect 1 $fetch
17+
quit

0 commit comments

Comments
 (0)