Refactor of BitcoinTestFramework main function.#26
Conversation
There was a problem hiding this comment.
nit: the convention seems to be that comments usually go the line above in the python test code
There was a problem hiding this comment.
Fixed in b1f803d03fec4cf4f66cb142ddde705b96aa8205.
There was a problem hiding this comment.
The second parameter (exit) isn't used in this repo. Can you remove it by having self.shutdown return the exit_code and then main() calls sys.exit().
There was a problem hiding this comment.
Fixed in e163c98169e0405255a2e6acdbb679494b5fa42d.
There was a problem hiding this comment.
I think it makes more logical sense to have the exception handling code in the main() function, and then just start shutdown() at the if self.success == TestStatus.FAILED and ... line
There was a problem hiding this comment.
I am not sure I fully understand your suggestion. If we move error handling into main, this is code that needs to be replicated in TestWrapper.
There was a problem hiding this comment.
why not:
try:
self.setup()
self.run_test()
except BaseException as e:
handle_exception(e)
finally:
exit_code = self.shutdown()
sys.exit(exit_code)There was a problem hiding this comment.
my preference would be to not have this as a separate function from main()
There was a problem hiding this comment.
Addressed in e163c98169e0405255a2e6acdbb679494b5fa42d.
There was a problem hiding this comment.
This isn't quite right. Since logging.shutdown() is called in TestFramework.shutdown(), no more logging should take place (see https://docs.python.org/3/library/logging.html#logging.shutdown).
A better way to do this would be to replace the logging.shutdown() call with:
handlers = self.log.handlers
for h in handlers:
h.flush()
h.flush()
self.log.removeHandler(h)That way, when setup is called again, the old handlers will have been removed and the new handlers will be added to the Logging object.
There was a problem hiding this comment.
Thanks for this suggestion. Added to e163c98169e0405255a2e6acdbb679494b5fa42.
|
You should also reword the second commit log to remove reference to |
Thinking about this some more, perhaps a good way to get this merged to Bitcoin Core would be to include the I'd hold off on that while we're still iterating on |
|
I am adding another TODO for this PR.
|
I don't think we want more than one instance of |
3f5baf0 to
774ecb1
Compare
jachiang
left a comment
There was a problem hiding this comment.
Refactor NetworkThread class in mininode.py, so each BitcoinTestFramework object initialises a different network thread.
I don't think we want more than one instance of
BitcoinTestFrameworkto exist at a time. @jachiang - do you have a reason for wanting this?
I will omit this from this PR, because it requires limiting one NetworkThread per BitcoinTestFramework, which I am unsure of how to best implement with minimal code changes.
There was a problem hiding this comment.
Do these need to be flushed before being removed?
There was a problem hiding this comment.
why not:
try:
self.setup()
self.run_test()
except BaseException as e:
handle_exception(e)
finally:
exit_code = self.shutdown()
sys.exit(exit_code)
jnewbery
left a comment
There was a problem hiding this comment.
A few comments inline. I also think the first commit comment ("Removal of residual NetworkThread state.") could be expanded a bit to explain why you're making that change.
Once the documentation is added, I think this is ready to open as a PR against bitcoin/bitcoin
There was a problem hiding this comment.
Is this new traceback functionality necessary? Can it be separated into a new commit so you're not moving and changing code in the same commit?
There was a problem hiding this comment.
Not Ideal, I agree. It seems the logger will only capture the traceback if error logging is called directly inside except (see https://stackoverflow.com/a/5191885 and https://docs.python.org/3/library/logging.html#logging.error). Since we are invoking error/exception logging elsewhere, we reconstruct the traceback string from the exception object.
There was a problem hiding this comment.
pep8 import ordering please.
More generally, can you run flake8 over this new file and resolve any style issues?
There was a problem hiding this comment.
is this required? num_nodes defaults to 3 in the setup() method.
There was a problem hiding this comment.
Have you tried changing the method signature to catch *kwargs and then set the defaults below, to avoid this huge function signature?
There was a problem hiding this comment.
Yes, it is much cleaner as you suggested. Thank you!
25ebea1 to
5f01275
Compare
The asyncio.new_event_loop() instance is now removed from the NetworkThread class during shutdown. This enables a NetworkThread instance to be restarted after being closed. The current NetworkThread class guards against an existing new_event_loop during initialization.
Setup and shutdown code now moved into dedicated methods. Test "success" is added as a BitcoinTestFramework member, which can be accessed outside of main.
In order for BitcoinTestFramework to correctly restart after shutdown, the previous logging handlers need to be removed, or else logging will continue in the previous temp directory. "Flush" ensures buffers are emptied, and "close" ensures file handler close logging file.
TestNode objects need to be removed during shutdown, as setup_nodes does not remove previous TestNode objects from previous test runs during setup.
This allows a BitcoinTestFramework child class to set test parameters in an overridden setup() rather than in an overridden set_test_params().
A BitcoinTestFramework child class which can be imported by an external user or project. TestWrapper.setup() initiates an underlying BitcoinTestFramework object with bitcoind subprocesses, rpc interfaces and test logging. TestWrapper.shutdown() safely tears down the BitcoinTestFramework object.
5f01275 to
bf4fc41
Compare
|
@jnewbery Sorry this took a while, but I've addressed all feedback, reworked commits for easier review and added a documentation file for the TestWrapper class. I will review again tomorrow and then open a PR against master. |
|
PR opened against bitcoin/bitcoin here: bitcoin#17288 |
In order to wrap the BitcoinTestFramework class in a user-defined class, which can be started and shutdown, the code in BitcoinTestFramework main has been encapsulated in respective functions. Additionally, test-run specific state in temp-directory and NetworkThread is now reset after shutdown.
Behavioural changes:
Potential exceptions raised from any code in setup section of the code are now handled. Previously, only exceptions raised by code beginning here was handled.
TODO's:
Refactor NetworkThread class in mininode.py, so each BitcoinTestFramework object initialises a different network thread.