Consider this:
0--- i/test/functional/wallet_keypool.py
1+++ w/test/functional/wallet_keypool.py
2@@ -124,16 +124,13 @@ class KeyPoolTest(BitcoinTestFramework):
3
4 # refill keypool with three new addresses
5 nodes[0].walletpassphrase('test', 1)
6 nodes[0].keypoolrefill(3)
7
8 # test walletpassphrase timeout
9- # CScheduler relies on condition_variable::wait_until() which is slightly
10- # less accurate than libevent's event trigger. We'll give it 2
11- # seconds to execute a 1 second scheduled event.
12- time.sleep(2)
13+ nodes[0].wait_until(lambda: nodes[0].getwalletinfo()["unlocked_until"] == 0)
14 assert_equal(nodes[0].getwalletinfo()["unlocked_until"], 0)
15
16 # drain the keypool
17 for _ in range(3):
18 nodes[0].getnewaddress()
19 assert_raises_rpc_error(-12, "Keypool ran out", nodes[0].getnewaddress)
It makes the test ~1 second faster - does not wait needlessly when the passphrase has already been cleared after about 1 second in normal environments. Also is more reliable because it would adapt to slow environments.