Skip to content

Commit 3503a69

Browse files
author
MarcoFalke
committed
Merge #15963: [tests] Make random seed logged and settable
a407b6f [tests] Make random seed logged and settable (John Newbery) Pull request description: This allows tests which use randomness to be reproducibly run on failure. ACKs for commit a407b6: jonatack: re-ACK a407b6f jb55: great! utACK a407b6f Tree-SHA512: e1e89e6e76d11ddec71a8f0f077227e4b46303f80461b170900d3f95d4dcc4187b0d1decfd63562ea970aaaf530ef032a3e64ed1669aac29033d95161855fda3
2 parents 6f4ba64 + a407b6f commit 3503a69

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

test/functional/test_framework/test_framework.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import argparse
1111
import os
1212
import pdb
13+
import random
1314
import shutil
1415
import sys
1516
import tempfile
@@ -129,6 +130,8 @@ def main(self):
129130
help="use bitcoin-cli instead of RPC for all commands")
130131
parser.add_argument("--perf", dest="perf", default=False, action="store_true",
131132
help="profile running nodes with perf for the duration of the test")
133+
parser.add_argument("--randomseed", type=int,
134+
help="set a random seed for deterministically reproducing a previous test run")
132135
self.add_options(parser)
133136
self.options = parser.parse_args()
134137

@@ -158,6 +161,22 @@ def main(self):
158161
self.options.tmpdir = tempfile.mkdtemp(prefix=TMPDIR_PREFIX)
159162
self._start_logging()
160163

164+
# Seed the PRNG. Note that test runs are reproducible if and only if
165+
# a single thread accesses the PRNG. For more information, see
166+
# https://docs.python.org/3/library/random.html#notes-on-reproducibility.
167+
# The network thread shouldn't access random. If we need to change the
168+
# network thread to access randomness, it should instantiate its own
169+
# random.Random object.
170+
seed = self.options.randomseed
171+
172+
if seed is None:
173+
seed = random.randrange(sys.maxsize)
174+
else:
175+
self.log.debug("User supplied random seed {}".format(seed))
176+
177+
random.seed(seed)
178+
self.log.debug("PRNG seed is: {}".format(seed))
179+
161180
self.log.debug('Setting up network thread')
162181
self.network_thread = NetworkThread()
163182
self.network_thread.start()

0 commit comments

Comments
 (0)