Skip to content

Commit 4550049

Browse files
committed
Reorganize BitcoinTestFramework class
1 parent b7dd44c commit 4550049

File tree

1 file changed

+62
-40
lines changed

1 file changed

+62
-40
lines changed

test/functional/test_framework/test_framework.py

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@
4242
from .authproxy import JSONRPCException
4343

4444
class BitcoinTestFramework(object):
45+
"""Base class for a bitcoin test script.
46+
47+
Individual bitcoin test scripts should subclass this class and override the following methods:
48+
49+
- __init__()
50+
- add_options()
51+
- setup_chain()
52+
- setup_network()
53+
- run_test()
54+
55+
The main() method should not be overridden.
56+
57+
This class also contains various public and private helper methods."""
58+
59+
# Methods to override in subclass test scripts.
4560

4661
TEST_EXIT_PASSED = 0
4762
TEST_EXIT_FAILED = 1
@@ -52,9 +67,6 @@ def __init__(self):
5267
self.setup_clean_chain = False
5368
self.nodes = None
5469

55-
def run_test(self):
56-
raise NotImplementedError
57-
5870
def add_options(self, parser):
5971
pass
6072

@@ -65,24 +77,6 @@ def setup_chain(self):
6577
else:
6678
self._initialize_chain(self.options.tmpdir, self.num_nodes, self.options.cachedir)
6779

68-
def start_node(self, i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None, stderr=None):
69-
return start_node(i, dirname, extra_args, rpchost, timewait, binary, stderr)
70-
71-
def start_nodes(self, num_nodes, dirname, extra_args=None, rpchost=None, timewait=None, binary=None):
72-
return start_nodes(num_nodes, dirname, extra_args, rpchost, timewait, binary)
73-
74-
def stop_node(self, num_node):
75-
stop_node(self.nodes[num_node], num_node)
76-
77-
def stop_nodes(self):
78-
stop_nodes(self.nodes)
79-
80-
def setup_nodes(self):
81-
extra_args = None
82-
if hasattr(self, "extra_args"):
83-
extra_args = self.extra_args
84-
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args)
85-
8680
def setup_network(self):
8781
self.setup_nodes()
8882

@@ -93,27 +87,16 @@ def setup_network(self):
9387
connect_nodes_bi(self.nodes, i, i + 1)
9488
self.sync_all()
9589

96-
def split_network(self):
97-
"""
98-
Split the network of four nodes into nodes 0/1 and 2/3.
99-
"""
100-
disconnect_nodes(self.nodes[1], 2)
101-
disconnect_nodes(self.nodes[2], 1)
102-
self.sync_all([self.nodes[:2], self.nodes[2:]])
103-
104-
def sync_all(self, node_groups=None):
105-
if not node_groups:
106-
node_groups = [self.nodes]
90+
def setup_nodes(self):
91+
extra_args = None
92+
if hasattr(self, "extra_args"):
93+
extra_args = self.extra_args
94+
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args)
10795

108-
[sync_blocks(group) for group in node_groups]
109-
[sync_mempools(group) for group in node_groups]
96+
def run_test(self):
97+
raise NotImplementedError
11098

111-
def join_network(self):
112-
"""
113-
Join the (previously split) network halves together.
114-
"""
115-
connect_nodes_bi(self.nodes, 1, 2)
116-
self.sync_all()
99+
# Main function. This should not be overridden by the subclass test scripts.
117100

118101
def main(self):
119102

@@ -209,6 +192,45 @@ def main(self):
209192
logging.shutdown()
210193
sys.exit(self.TEST_EXIT_FAILED)
211194

195+
# Public helper methods. These can be accessed by the subclass test scripts.
196+
197+
def start_node(self, i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None, stderr=None):
198+
return start_node(i, dirname, extra_args, rpchost, timewait, binary, stderr)
199+
200+
def start_nodes(self, num_nodes, dirname, extra_args=None, rpchost=None, timewait=None, binary=None):
201+
return start_nodes(num_nodes, dirname, extra_args, rpchost, timewait, binary)
202+
203+
def stop_node(self, num_node):
204+
stop_node(self.nodes[num_node], num_node)
205+
206+
def stop_nodes(self):
207+
stop_nodes(self.nodes)
208+
209+
def split_network(self):
210+
"""
211+
Split the network of four nodes into nodes 0/1 and 2/3.
212+
"""
213+
disconnect_nodes(self.nodes[1], 2)
214+
disconnect_nodes(self.nodes[2], 1)
215+
self.sync_all([self.nodes[:2], self.nodes[2:]])
216+
217+
def join_network(self):
218+
"""
219+
Join the (previously split) network halves together.
220+
"""
221+
connect_nodes_bi(self.nodes, 1, 2)
222+
self.sync_all()
223+
224+
def sync_all(self, node_groups=None):
225+
if not node_groups:
226+
node_groups = [self.nodes]
227+
228+
for group in node_groups:
229+
sync_blocks(group)
230+
sync_mempools(group)
231+
232+
# Private helper methods. These should not be accessed by the subclass test scripts.
233+
212234
def _start_logging(self):
213235
# Add logger and logging handlers
214236
self.log = logging.getLogger('TestFramework')

0 commit comments

Comments
 (0)