Skip to content

Commit bb8c9b3

Browse files
author
MarcoFalke
committed
Merge #14275: tests: Write the notification message to different files to avoid race condition in feature_notifications.py
67654b6 tests: write the notification to different files to avoid race condition (Chun Kuan Lee) Pull request description: This PR change the behavior that `feature_notifications.py` would write to different files instead of writing to the same file to avoid race condition. Tree-SHA512: 78406167cc6a3f570134b0ee76d2be1440bc1498cd7b1be72fae16d0ab86950e26ef3bf6008796016e5418231400c6492f0e062909dd882646541ecb7a70fb30
2 parents 4dac24d + 67654b6 commit bb8c9b3

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ test_script:
5555
- ps: src\bench_bitcoin.exe -evals=1 -scaling=0
5656
- ps: python test\util\bitcoin-util-test.py
5757
- cmd: python test\util\rpcauth-test.py
58-
- cmd: python test\functional\test_runner.py --force --quiet --combinedlogslen=4000 --exclude "feature_notifications,wallet_multiwallet,wallet_multiwallet.py --usecli"
58+
- cmd: python test\functional\test_runner.py --force --quiet --combinedlogslen=4000 --exclude "wallet_multiwallet,wallet_multiwallet.py --usecli"
5959
deploy: off

test/functional/feature_notifications.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,73 +17,71 @@ def skip_test_if_missing_module(self):
1717
self.skip_if_no_wallet()
1818

1919
def setup_network(self):
20-
self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
21-
self.block_filename = os.path.join(self.options.tmpdir, "blocks.txt")
22-
self.tx_filename = os.path.join(self.options.tmpdir, "transactions.txt")
20+
self.alertnotify_dir = os.path.join(self.options.tmpdir, "alertnotify")
21+
self.blocknotify_dir = os.path.join(self.options.tmpdir, "blocknotify")
22+
self.walletnotify_dir = os.path.join(self.options.tmpdir, "walletnotify")
23+
os.mkdir(self.alertnotify_dir)
24+
os.mkdir(self.blocknotify_dir)
25+
os.mkdir(self.walletnotify_dir)
2326

2427
# -alertnotify and -blocknotify on node0, walletnotify on node1
2528
self.extra_args = [["-blockversion=2",
26-
"-alertnotify=echo %%s >> %s" % self.alert_filename,
27-
"-blocknotify=echo %%s >> %s" % self.block_filename],
29+
"-alertnotify=echo > {}".format(os.path.join(self.alertnotify_dir, '%s')),
30+
"-blocknotify=echo > {}".format(os.path.join(self.blocknotify_dir, '%s'))],
2831
["-blockversion=211",
2932
"-rescan",
30-
"-walletnotify=echo %%s >> %s" % self.tx_filename]]
33+
"-walletnotify=echo > {}".format(os.path.join(self.walletnotify_dir, '%s'))]]
3134
super().setup_network()
3235

3336
def run_test(self):
3437
self.log.info("test -blocknotify")
3538
block_count = 10
3639
blocks = self.nodes[1].generate(block_count)
3740

38-
# wait at most 10 seconds for expected file size before reading the content
39-
wait_until(lambda: os.path.isfile(self.block_filename) and os.stat(self.block_filename).st_size >= (block_count * 65), timeout=10)
41+
# wait at most 10 seconds for expected number of files before reading the content
42+
wait_until(lambda: len(os.listdir(self.blocknotify_dir)) == block_count, timeout=10)
4043

41-
# file content should equal the generated blocks hashes
42-
with open(self.block_filename, 'r', encoding="utf-8") as f:
43-
assert_equal(sorted(blocks), sorted(l.strip() for l in f.read().splitlines()))
44+
# directory content should equal the generated blocks hashes
45+
assert_equal(sorted(blocks), sorted(os.listdir(self.blocknotify_dir)))
4446

4547
self.log.info("test -walletnotify")
46-
# wait at most 10 seconds for expected file size before reading the content
47-
wait_until(lambda: os.path.isfile(self.tx_filename) and os.stat(self.tx_filename).st_size >= (block_count * 65), timeout=10)
48+
# wait at most 10 seconds for expected number of files before reading the content
49+
wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
4850

49-
# file content should equal the generated transaction hashes
51+
# directory content should equal the generated transaction hashes
5052
txids_rpc = list(map(lambda t: t['txid'], self.nodes[1].listtransactions("*", block_count)))
51-
with open(self.tx_filename, 'r', encoding="ascii") as f:
52-
assert_equal(sorted(txids_rpc), sorted(l.strip() for l in f.read().splitlines()))
53-
os.remove(self.tx_filename)
53+
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.walletnotify_dir)))
54+
for tx_file in os.listdir(self.walletnotify_dir):
55+
os.remove(os.path.join(self.walletnotify_dir, tx_file))
5456

5557
self.log.info("test -walletnotify after rescan")
5658
# restart node to rescan to force wallet notifications
5759
self.restart_node(1)
5860
connect_nodes_bi(self.nodes, 0, 1)
5961

60-
wait_until(lambda: os.path.isfile(self.tx_filename) and os.stat(self.tx_filename).st_size >= (block_count * 65), timeout=10)
62+
wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
6163

62-
# file content should equal the generated transaction hashes
64+
# directory content should equal the generated transaction hashes
6365
txids_rpc = list(map(lambda t: t['txid'], self.nodes[1].listtransactions("*", block_count)))
64-
with open(self.tx_filename, 'r', encoding="ascii") as f:
65-
assert_equal(sorted(txids_rpc), sorted(l.strip() for l in f.read().splitlines()))
66+
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.walletnotify_dir)))
6667

6768
# Mine another 41 up-version blocks. -alertnotify should trigger on the 51st.
6869
self.log.info("test -alertnotify")
6970
self.nodes[1].generate(41)
7071
self.sync_all()
7172

7273
# Give bitcoind 10 seconds to write the alert notification
73-
wait_until(lambda: os.path.isfile(self.alert_filename) and os.path.getsize(self.alert_filename), timeout=10)
74+
wait_until(lambda: len(os.listdir(self.alertnotify_dir)), timeout=10)
7475

75-
with open(self.alert_filename, 'r', encoding='utf8') as f:
76-
alert_text = f.read()
76+
for notify_file in os.listdir(self.alertnotify_dir):
77+
os.remove(os.path.join(self.alertnotify_dir, notify_file))
7778

7879
# Mine more up-version blocks, should not get more alerts:
7980
self.nodes[1].generate(2)
8081
self.sync_all()
8182

82-
with open(self.alert_filename, 'r', encoding='utf8') as f:
83-
alert_text2 = f.read()
84-
8583
self.log.info("-alertnotify should not continue notifying for more unknown version blocks")
86-
assert_equal(alert_text, alert_text2)
84+
assert_equal(len(os.listdir(self.alertnotify_dir)), 0)
8785

8886
if __name__ == '__main__':
8987
NotificationsTest().main()

0 commit comments

Comments
 (0)