From 810e12381af20ded7609eac6b78b17eb46d58565 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 27 Apr 2017 14:17:16 +0200 Subject: [PATCH] pytest: Simple wrapper around BitcoinProxy to avoid timeouts Running long integration tests could result in `bitcoind` dropping the connection inbetween calls, and since python-bitcoinlib does not reconnect and/or retry, all subsequent tests would fail as well. This patch switches to throwaway connections, each serving just one request. It's easier than to reach into the bitcoinlib to reconnect and reauth, but comes with some overhead, but I think it's acceptable for the few bitcoin calls we actually perform. --- tests/utils.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/utils.py b/tests/utils.py index 09a55d6c8..2d05a6a2e 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -108,6 +108,31 @@ class TailableProc(object): pos += 1 +class SimpleBitcoinProxy: + """Wrapper for BitcoinProxy to reconnect. + + Long wait times between calls to the Bitcoin RPC could result in + `bitcoind` closing the connection, so here we just create + throwaway connections. This is easier than to reach into the RPC + library to close, reopen and reauth upon failure. + """ + def __init__(self, url): + self.url = url + + def __getattr__(self, name): + if name.startswith('__') and name.endswith('__'): + # Python internal stuff + raise AttributeError + + # Create a callable to do the actual call + f = lambda *args: BitcoinProxy(self.url)._call(name, *args) + + # Make debuggers show rather than > + f.__name__ = name + return f + + class BitcoinD(TailableProc): def __init__(self, bitcoin_dir="/tmp/bitcoind-test", rpcport=18332): @@ -134,7 +159,7 @@ class BitcoinD(TailableProc): BITCOIND_CONFIG['rpcport'] = rpcport write_config(os.path.join(bitcoin_dir, 'bitcoin.conf'), BITCOIND_CONFIG) write_config(os.path.join(regtestdir, 'bitcoin.conf'), BITCOIND_CONFIG) - self.rpc = BitcoinProxy( + self.rpc = SimpleBitcoinProxy( "http://rpcuser:rpcpass@127.0.0.1:{}".format(self.rpcport)) def start(self):