test: Update wait_until usage in tests not to use the one from utils #19752

pull slmtpz wants to merge 2 commits into bitcoin:master from slmtpz:master changing 27 files +82 −92
  1. slmtpz commented at 6:19 pm on August 17, 2020: contributor

    Replace global (from test_framework/util.py) wait_until() usages with the ones provided by BitcoinTestFramework and P2PInterface classes.

    The motivation behind this change is that the util.wait_until() expects a timeout, timeout_factor and lock and it is not aware of the context of the test framework. BitcoinTestFramework offers a wait_until() which has an understandable amount of default timeout and a shared timeout_factor. Moreover, on top of these, mininode.wait_until() also has a shared lock.

    closes #19080

  2. DrahtBot added the label Tests on Aug 17, 2020
  3. MarcoFalke commented at 2:26 pm on August 18, 2020: member
    Very nice. Concept ACK
  4. practicalswift commented at 5:32 pm on August 18, 2020: contributor

    Nice first-time contribution! Warm welcome as a contributor @slmtpz :)

    Concept ACK

  5. slmtpz commented at 5:35 pm on August 18, 2020: contributor

    Nice first-time contribution! Warm welcome as a contributor @slmtpz :)

    Concept ACK

    Thank you, looking forward to more contributions in future!

  6. DrahtBot added the label Needs rebase on Aug 20, 2020
  7. slmtpz force-pushed on Aug 20, 2020
  8. DrahtBot removed the label Needs rebase on Aug 20, 2020
  9. DrahtBot commented at 8:08 pm on August 20, 2020: member

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #19700 (wallet: Replace -zapwallettxes with wallet tool command by achow101)
    • #19671 (wallet: Remove -zapwallettxes by achow101)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  10. in test/functional/p2p_leak.py:22 in c3e1a30741 outdated
    18@@ -19,11 +19,7 @@
    19 )
    20 from test_framework.mininode import mininode_lock, P2PInterface
    21 from test_framework.test_framework import BitcoinTestFramework
    22-from test_framework.util import (
    


    glozow commented at 2:57 am on August 21, 2020:
    Style nit: I think it’s preferred to keep the list on newlines with commas at the end, otherwise we have a situation like this where the diff goes from 1 line to 6. Is there a reason you’re changing it?

    slmtpz commented at 6:09 pm on August 25, 2020:
    I was considering line lengths. It seemed a bit awkward to keep this structure even if there is only one import. However, I totally agree with you on separating with new lines so git history stays clean. What about the imports where this structure is not used? While I’m here, should I put them in a list?

    MarcoFalke commented at 6:21 pm on August 25, 2020:
    When the list is longer than one item and it was previously on multiple lines, it probably makes sense to keep it on multiple lines
  11. in test/functional/p2p_compactblocks.py:92 in c3e1a30741 outdated
    90 
    91         This is used when we want to send a message into the node that we expect
    92         will get us disconnected, eg an invalid block."""
    93         self.send_message(message)
    94-        wait_until(lambda: not self.is_connected, timeout=timeout, lock=mininode_lock)
    95+        self.wait_until(lambda: not self.is_connected, timeout=timeout, check_connected=False)
    


    glozow commented at 3:12 am on August 21, 2020:
    Hm, is there a reason for removing mininode_lock and adding check_connected=False?

    glozow commented at 3:33 am on August 21, 2020:
    Also, wait_for_disconnect would be better.

    slmtpz commented at 7:35 pm on August 25, 2020:
    Used wait_for_disconnect instead, thanks!
  12. in test/functional/p2p_sendheaders.py:176 in c3e1a30741 outdated
    170@@ -174,7 +171,7 @@ def check_last_headers_announcement(self, headers):
    171         """Test whether the last headers announcements received are right.
    172            Headers may be announced across more than one message."""
    173         test_function = lambda: (len(self.recent_headers_announced) >= len(headers))
    174-        wait_until(test_function, timeout=60, lock=mininode_lock)
    175+        self.wait_until(test_function)
    


    glozow commented at 3:13 am on August 21, 2020:
    why are you removing the timeout and lock here?

    slmtpz commented at 6:58 pm on August 25, 2020:
    This class extends P2PInterface not BitcoinTestFramework. mininode one already has a lock. As for timeout, it already has a default value of 60.
  13. in test/functional/p2p_sendheaders.py:188 in c3e1a30741 outdated
    182@@ -186,7 +183,7 @@ def check_last_inv_announcement(self, inv):
    183         inv should be a list of block hashes."""
    184 
    185         test_function = lambda: self.block_announced
    186-        wait_until(test_function, timeout=60, lock=mininode_lock)
    187+        self.wait_until(test_function)
    


    glozow commented at 3:13 am on August 21, 2020:
    same

    slmtpz commented at 6:59 pm on August 25, 2020:
    This class extends P2PInterface not BitcoinTestFramework. mininode one already has a lock. As for timeout, it already has a default value of 60.
  14. in test/functional/p2p_compactblocks.py:297 in c3e1a30741 outdated
    293@@ -294,7 +294,7 @@ def test_compactblock_construction(self, test_node, use_witness_address=True):
    294         block.rehash()
    295 
    296         # Wait until the block was announced (via compact blocks)
    297-        wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=mininode_lock)
    298+        self.wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30, lock=mininode_lock)
    


    glozow commented at 3:26 am on August 21, 2020:

    I’m not sure if this is one of the goals of this PR, but in this situation I think it’s best to use test_node.wait_until().

    Same with the rest of the test.


    slmtpz commented at 6:47 pm on August 25, 2020:
    I was inclined to use the one from BitcoinTestFramework since I thought it would be good to keep consistency. However, in this case, since we are waiting for a change in test_node, using that one makes more sense, did I get it right? :)

    slmtpz commented at 7:35 pm on August 25, 2020:
    Updated.
  15. in test/functional/wallet_resendwallettransactions.py:36 in c3e1a30741 outdated
    32@@ -33,7 +33,7 @@ def run_test(self):
    33         time.sleep(1.1)
    34 
    35         # Can take a few seconds due to transaction trickling
    36-        wait_until(lambda: node.p2p.tx_invs_received[txid] >= 1, lock=mininode_lock)
    37+        self.wait_until(lambda: node.p2p.tx_invs_received[txid] >= 1, lock=mininode_lock)
    


    glozow commented at 3:30 am on August 21, 2020:

    Here, wait_for_broadcast(txid) would be more appropriate

    (txid needs to not be converted to int though)


    slmtpz commented at 7:36 pm on August 25, 2020:
    It looks cleaner now! Please have a look.
  16. in test/functional/wallet_resendwallettransactions.py:67 in c3e1a30741 outdated
    63@@ -64,7 +64,7 @@ def run_test(self):
    64         # Transaction should be rebroadcast approximately 24 hours in the future,
    65         # but can range from 12-36. So bump 36 hours to be sure.
    66         node.setmocktime(now + 36 * 60 * 60)
    67-        wait_until(lambda: node.p2ps[1].tx_invs_received[txid] >= 1, lock=mininode_lock)
    68+        self.wait_until(lambda: node.p2ps[1].tx_invs_received[txid] >= 1, lock=mininode_lock)
    


    glozow commented at 3:30 am on August 21, 2020:
    same

    slmtpz commented at 7:36 pm on August 25, 2020:
    Updated
  17. glozow commented at 3:37 am on August 21, 2020: member

    Concept ACK, welcome! 😊 this is awesome!

    I complain about this pretty often 😅 The util wait_until function doesn’t have any context from the TestFramework instance (i.e. timeout and timeout_factor). Sometimes when it’s used, the test writer forgets to pass in the mininode_lock which is even worse. I think it would be a good idea to add a comment to the wait_until function in test_framework.util that recommends using something else, because it’s almost never the best option - this is just my opinion though. (Btw, I recommend adding something along these lines to the PR description to explain what motivates these changes).

    We actually have more options than just TestFramework wait_until. There are a few places where one of the mininode helper functions (wait_for_disconnect, wait_for_broadcast, etc) or the mininode wait_until would be more appropriate.

    There seem to be some changes that are unrelated to changing which wait_until is used. I didn’t look into them too closely, but could you give an explanation and/or put them in a separate PR?

    Also a style nit: there’s a few places where you’re refactoring the imports in addition to removing wait_until, which I don’t think is the style convention and makes the diff larger than it needs to be.

  18. slmtpz force-pushed on Aug 25, 2020
  19. in test/functional/rpc_invalidateblock.py:15 in 7a66c78b4d outdated
     8@@ -9,10 +9,8 @@
     9 from test_framework.util import (
    10     assert_equal,
    11     connect_nodes,
    12-    wait_until,
    13 )
    14 
    15-
    


    MarcoFalke commented at 6:22 pm on August 25, 2020:
    nit: accidental change

    slmtpz commented at 6:25 pm on August 25, 2020:
    Corrected.
  20. slmtpz force-pushed on Aug 25, 2020
  21. slmtpz force-pushed on Aug 25, 2020
  22. slmtpz force-pushed on Aug 25, 2020
  23. slmtpz force-pushed on Aug 25, 2020
  24. slmtpz commented at 7:40 pm on August 25, 2020: contributor

    Concept ACK, welcome! 😊 this is awesome!

    I complain about this pretty often 😅 The util wait_until function doesn’t have any context from the TestFramework instance (i.e. timeout and timeout_factor). Sometimes when it’s used, the test writer forgets to pass in the mininode_lock which is even worse. I think it would be a good idea to add a comment to the wait_until function in test_framework.util that recommends using something else, because it’s almost never the best option - this is just my opinion though. (Btw, I recommend adding something along these lines to the PR description to explain what motivates these changes).

    We actually have more options than just TestFramework wait_until. There are a few places where one of the mininode helper functions (wait_for_disconnect, wait_for_broadcast, etc) or the mininode wait_until would be more appropriate.

    There seem to be some changes that are unrelated to changing which wait_until is used. I didn’t look into them too closely, but could you give an explanation and/or put them in a separate PR?

    Also a style nit: there’s a few places where you’re refactoring the imports in addition to removing wait_until, which I don’t think is the style convention and makes the diff larger than it needs to be.

    Thanks for the thorough review and double thanks for your suggestions! Going deeper, things had started to build up in my mind. Rebase + force pushed the changes you suggested. As for your other proposal to add a comment in util.wait_until(), please look at this second commit. PR description is also updated. Should I update commit body as well (after your approval of the motivation for this change stated in the PR description) ?

  25. slmtpz requested review from MarcoFalke on Aug 25, 2020
  26. slmtpz force-pushed on Aug 25, 2020
  27. kallewoof commented at 2:28 am on August 26, 2020: member

    Big concept ACK: much cleaner!

    utACK 0a2d50714f056955ac92f01346bbdc8d5c3019c7

    One thing that tripped me up when reviewing is that there are two separate wait_until methods, one (in P2PInterface) which has a default lock (mininode_lock) and one (in BitcoinTestFramework) which doesn’t. This is why you see the lock=mininode_lock in a few places, while it’s removed in a bunch of others. (Check super class.)

  28. in test/functional/example_test.py:205 in 0a2d50714f outdated
    201@@ -203,7 +202,7 @@ def run_test(self):
    202 
    203         # wait_until() will loop until a predicate condition is met. Use it to test properties of the
    204         # P2PInterface objects.
    205-        wait_until(lambda: sorted(blocks) == sorted(list(self.nodes[2].p2p.block_receive_map.keys())), timeout=5, lock=mininode_lock)
    206+        self.wait_until(lambda: sorted(blocks) == sorted(list(self.nodes[2].p2p.block_receive_map.keys())), timeout=5, lock=mininode_lock)
    


    MarcoFalke commented at 6:25 am on August 26, 2020:
    0        self.nodes[2].p2p.wait_until(lambda: sorted(blocks) == sorted(list(self.nodes[2].p2p.block_receive_map.keys())), timeout=5)
    

    Shouldn’t this be


    slmtpz commented at 6:38 am on August 26, 2020:
    Is there a big difference between two? I was inclined to use the one from framework wherever possible.

    MarcoFalke commented at 6:42 am on August 26, 2020:

    It seems a bit convolved to import the lock, which needs to be taken, then call a method, which passes the imported lock to be taken.

    Simply calling a member function that takes the lock when needed without importing or passing it seems less error prone, as there is no way it can be missed. Also, it makes the calling code shorter and more readable


    slmtpz commented at 6:51 am on August 26, 2020:
    Updated, thanks for clear explanation.
  29. in test/functional/feature_versionbits_warning.py:93 in 0a2d50714f outdated
    89@@ -91,14 +90,14 @@ def run_test(self):
    90 
    91         # Generating one block guarantees that we'll get out of IBD
    92         node.generatetoaddress(1, node_deterministic_address)
    93-        wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'], timeout=10, lock=mininode_lock)
    94+        self.wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'], timeout=10, lock=mininode_lock)
    


    MarcoFalke commented at 6:26 am on August 26, 2020:
    0        self.wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'], timeout=10)
    

    why the lock?


    slmtpz commented at 6:39 am on August 26, 2020:
    The class of this method inherits from BitcoinTestFramework which does not have a default lock.

    MarcoFalke commented at 6:43 am on August 26, 2020:
    but why is the lock needed? Why does it need to be taken here?

    slmtpz commented at 6:44 am on August 26, 2020:
    I frankly don’t know. It was there before, so I didn’t touch it.
  30. in test/functional/p2p_segwit.py:2116 in 0a2d50714f outdated
    2112@@ -2114,7 +2113,7 @@ def test_wtxid_relay(self):
    2113         # Check wtxidrelay feature negotiation message through connecting a new peer
    2114         def received_wtxidrelay():
    2115             return (len(self.wtx_node.last_wtxidrelay) > 0)
    2116-        wait_until(received_wtxidrelay, timeout=60, lock=mininode_lock)
    2117+        self.wait_until(received_wtxidrelay, lock=mininode_lock)
    


    MarcoFalke commented at 6:28 am on August 26, 2020:
    0        self.wtx_node.wait_until(received_wtxidrelay)
    

    slmtpz commented at 6:52 am on August 26, 2020:
    Updated.
  31. in test/functional/p2p_sendheaders.py:300 in 0a2d50714f outdated
    296@@ -298,7 +297,7 @@ def test_nonnull_locators(self, test_node, inv_node):
    297                 test_node.send_header_for_blocks([new_block])
    298                 test_node.wait_for_getdata([new_block.sha256])
    299                 test_node.send_and_ping(msg_block(new_block))  # make sure this block is processed
    300-                wait_until(lambda: inv_node.block_announced, timeout=60, lock=mininode_lock)
    301+                self.wait_until(lambda: inv_node.block_announced, lock=mininode_lock)
    


    MarcoFalke commented at 6:30 am on August 26, 2020:
    0                inv_node.wait_until(lambda: inv_node.block_announced)
    

    slmtpz commented at 6:52 am on August 26, 2020:
    Updated.
  32. in test/functional/p2p_tx_download.py:140 in 0a2d50714f outdated
    136@@ -138,20 +137,20 @@ def test_in_flight_max(self):
    137             p.tx_getdata_count = 0
    138 
    139         p.send_message(msg_inv([CInv(t=MSG_WTX, h=i) for i in txids]))
    140-        wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT, lock=mininode_lock)
    141+        self.wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT, lock=mininode_lock)
    


    MarcoFalke commented at 6:30 am on August 26, 2020:
    0        p.wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT)
    

    slmtpz commented at 6:52 am on August 26, 2020:
    Updated.
  33. in test/functional/p2p_tx_download.py:146 in 0a2d50714f outdated
    143             assert_equal(p.tx_getdata_count, MAX_GETDATA_IN_FLIGHT)
    144 
    145         self.log.info("Now check that if we send a NOTFOUND for a transaction, we'll get one more request")
    146         p.send_message(msg_notfound(vec=[CInv(t=MSG_WTX, h=txids[0])]))
    147-        wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT + 1, timeout=10, lock=mininode_lock)
    148+        self.wait_until(lambda: p.tx_getdata_count >= MAX_GETDATA_IN_FLIGHT + 1, timeout=10, lock=mininode_lock)
    


    MarcoFalke commented at 6:30 am on August 26, 2020:
    same

    slmtpz commented at 6:52 am on August 26, 2020:
    Updated.
  34. in test/functional/p2p_tx_download.py:153 in 0a2d50714f outdated
    151 
    152         WAIT_TIME = TX_EXPIRY_INTERVAL // 2 + TX_EXPIRY_INTERVAL
    153         self.log.info("if we wait about {} minutes, we should eventually get more requests".format(WAIT_TIME / 60))
    154         self.nodes[0].setmocktime(int(time.time() + WAIT_TIME))
    155-        wait_until(lambda: p.tx_getdata_count == MAX_GETDATA_IN_FLIGHT + 2)
    156+        self.wait_until(lambda: p.tx_getdata_count == MAX_GETDATA_IN_FLIGHT + 2)
    


    MarcoFalke commented at 6:30 am on August 26, 2020:
    same

    slmtpz commented at 6:52 am on August 26, 2020:
    Updated.
  35. MarcoFalke approved
  36. MarcoFalke commented at 6:32 am on August 26, 2020: member

    ACK

    8823fd6c702f3c7c97218b1dbeda2b9386873d69

  37. slmtpz force-pushed on Aug 26, 2020
  38. DrahtBot added the label Needs rebase on Aug 26, 2020
  39. test: Update wait_until usage in tests not to use the one from utils
    Replace "wait_until()" usage from utils, with the ones from BitcoinTestFramework and P2PInterface.
    closes #19080
    1343c86c7c
  40. test: Add docstring to wait_until() in util.py to warn about its usage d841301010
  41. slmtpz force-pushed on Aug 26, 2020
  42. slmtpz requested review from kallewoof on Aug 26, 2020
  43. slmtpz requested review from MarcoFalke on Aug 26, 2020
  44. DrahtBot removed the label Needs rebase on Aug 26, 2020
  45. kallewoof commented at 8:39 pm on August 26, 2020: member
    utACK d841301010914203fb5ef02627c76fad99cb11f1
  46. MarcoFalke commented at 6:21 am on August 27, 2020: member

    Thanks for keeping this updated and addressing all the feedback!

    ACK d841301010914203fb5ef02627c76fad99cb11f1 🦆

    Signature:

     0-----BEGIN PGP SIGNED MESSAGE-----
     1Hash: SHA512
     2
     3ACK d841301010914203fb5ef02627c76fad99cb11f1 🦆
     4-----BEGIN PGP SIGNATURE-----
     5
     6iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
     7pUjVKAwAzGdH57AIPkBd3pQiCIm0PMYONNZEur6hn6VVGnLOatyGbLlTQLaw/kwu
     8VZl6jutSgIusoKNUkjJLgPV2yiQF3PN9xrhS5IwGfGXYX9nvdwy4T/dhmLv6XUuE
     9HO0+pM7fYE6XoyvK+YPV60uIkb3cgX8hVOU8ACd7GuIESkGiLnxN4zYxZN2Zyrvo
    10yPUcOAKGXXJom6wzXXUQUZSp9BE5HJexWydui4iduwwkX2n+8yJKOYQ0Hv/kzqo1
    11uD2gB+GmVUzpfNs3RUkLot2gtl0yK5bqYAVmyCfNXTppRD1yt1JnvxWooFeeW02t
    12kL5JRg6aFy/PkM+tfTU/uZ3kEuSI0STHYJu5O0Ev9nG2cWKHbERP+LLIIiy0crns
    13UyyQ9rSaKjMucDlNfbXEg95zf2pRxE9hvqFwBQQT5VC06Pfn6k4Xmi5UfpwRlTSa
    144uSSz/dtO5FMvbST5NCXtTOutkFmDmkLyHGOtynLs0Px7GrHtyhn0mL9OnnLbeqV
    15/mQJZu0x
    16=HU8g
    17-----END PGP SIGNATURE-----
    

    Timestamp of file with hash 8841ab10d7ee7a298b7cd8d05a44bfb9c3aaaa6e2c48b70e1d0adeb3afe2f62e -

  47. MarcoFalke merged this on Aug 27, 2020
  48. MarcoFalke closed this on Aug 27, 2020

  49. sidhujag referenced this in commit 6a9ac03311 on Aug 28, 2020
  50. Fabcien referenced this in commit 914e834144 on Sep 16, 2021
  51. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-17 12:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me