There is some imprecision probably in the internal HTTPRPCTimer class (haven't exactly figured out where). But we can't expect that waiting exactly 2 seconds right after calling walletpassphrase(2) will result in a locked wallet due to the nature how we internally handle threads/timers. The wallet_encryption test fails regularly in CIs.
Backport of core PR16420
https://github.com/bitcoin/bitcoin/pull/16420/files