Skip to content

Commit 15a21c2

Browse files
ken2812221furszy
authored andcommitted
tests: write the notification to different files to avoid race condition
1 parent 466e97a commit 15a21c2

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

test/functional/feature_notifications.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,71 @@ def set_test_params(self):
1818
self.setup_clean_chain = True
1919

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

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

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

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

42-
# file content should equal the generated blocks hashes
43-
with open(self.block_filename, 'r') as f:
44-
assert_equal(sorted(blocks), sorted(f.read().splitlines()))
45+
# directory content should equal the generated blocks hashes
46+
assert_equal(sorted(blocks), sorted(os.listdir(self.blocknotify_dir)))
4547

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

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

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

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

63-
# file content should equal the generated transaction hashes
65+
# directory content should equal the generated transaction hashes
6466
txids_rpc = list(map(lambda t: t['txid'], self.nodes[1].listtransactions("*", block_count)))
65-
with open(self.tx_filename, 'r') as f:
66-
assert_equal(sorted(txids_rpc), sorted(f.read().splitlines()))
67+
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.walletnotify_dir)))
6768

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

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

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

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

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

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

0 commit comments

Comments
 (0)