1919
2020"""Run tx_tester -f scripts against python/examples/broker.py."""
2121
22+ import glob
2223import os
24+ import re
2325import socket
2426import subprocess
2527import 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+
5277class 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
141158if __name__ == "__main__" :
0 commit comments