test: Use high-level python types

Using the built-in open() and pathlib is identical and requires less code.

Also, remove redundant sync_blocks call.
This commit is contained in:
MarcoFalke 2025-01-21 11:26:55 +01:00
parent eb243ff06c
commit fa9593efc2
No known key found for this signature in database

View file

@ -1,13 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Copyright (c) 2022 The Bitcoin Core developers # Copyright (c) 2022-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test removing undeleted pruned blk files on startup.""" """Test removing undeleted pruned blk files on startup."""
import platform import platform
import os
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
class FeatureRemovePrunedFilesOnStartupTest(BitcoinTestFramework): class FeatureRemovePrunedFilesOnStartupTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.num_nodes = 1 self.num_nodes = 1
@ -18,7 +18,6 @@ class FeatureRemovePrunedFilesOnStartupTest(BitcoinTestFramework):
for _ in range(n): for _ in range(n):
self.generate(self.nodes[0], 250) self.generate(self.nodes[0], 250)
self.generate(self.nodes[0], blocks % 250) self.generate(self.nodes[0], blocks % 250)
self.sync_blocks()
def run_test(self): def run_test(self):
blk0 = self.nodes[0].blocks_path / "blk00000.dat" blk0 = self.nodes[0].blocks_path / "blk00000.dat"
@ -26,30 +25,31 @@ class FeatureRemovePrunedFilesOnStartupTest(BitcoinTestFramework):
blk1 = self.nodes[0].blocks_path / "blk00001.dat" blk1 = self.nodes[0].blocks_path / "blk00001.dat"
rev1 = self.nodes[0].blocks_path / "rev00001.dat" rev1 = self.nodes[0].blocks_path / "rev00001.dat"
self.mine_batches(800) self.mine_batches(800)
fo1 = os.open(blk0, os.O_RDONLY)
fo2 = os.open(rev1, os.O_RDONLY) self.log.info("Open some files to check that this may delay deletion")
fd1 = os.fdopen(fo1) fd1 = open(blk0, "rb")
fd2 = os.fdopen(fo2) fd2 = open(rev1, "rb")
self.nodes[0].pruneblockchain(600) self.nodes[0].pruneblockchain(600)
# Windows systems will not remove files with an open fd # Windows systems will not remove files with an open fd
if platform.system() != 'Windows': if platform.system() != 'Windows':
assert not os.path.exists(blk0) assert not blk0.exists()
assert not os.path.exists(rev0) assert not rev0.exists()
assert not os.path.exists(blk1) assert not blk1.exists()
assert not os.path.exists(rev1) assert not rev1.exists()
else: else:
assert os.path.exists(blk0) assert blk0.exists()
assert not os.path.exists(rev0) assert not rev0.exists()
assert not os.path.exists(blk1) assert not blk1.exists()
assert os.path.exists(rev1) assert rev1.exists()
# Check that the files are removed on restart once the fds are closed self.log.info("Check that the files are removed on restart once the fds are closed")
fd1.close() fd1.close()
fd2.close() fd2.close()
self.restart_node(0) self.restart_node(0)
assert not os.path.exists(blk0) assert not blk0.exists()
assert not os.path.exists(rev1) assert not rev1.exists()
if __name__ == '__main__': if __name__ == '__main__':
FeatureRemovePrunedFilesOnStartupTest(__file__).main() FeatureRemovePrunedFilesOnStartupTest(__file__).main()