test: Turn ElapseSteady into SteadyClockContext #34432

pull maflcko wants to merge 1 commits into bitcoin:master from maflcko:2601-test-steady-ctx changing 2 files +21 −7
  1. maflcko commented at 6:08 pm on January 28, 2026: member

    ElapseSteady was introduced a while back, but is only used in one place. It makes more sense if this were a context manager, so that mocktime does not leak from one test into the next.

    So turn it into a context manager, rename it and allow easy time advancement via e.g. steady_ctx += 1h.

  2. DrahtBot added the label Tests on Jan 28, 2026
  3. DrahtBot commented at 6:09 pm on January 28, 2026: contributor

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

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK l0rinc, ismaelsadeeq

    If your review is incorrectly listed, please copy-paste <!–meta-tag:bot-skip–> into the comment that the bot should ignore.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #32430 (test: Add and use ElapseTime helper by maflcko)

    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.

  4. maflcko force-pushed on Jan 28, 2026
  5. in src/test/util/time.h:31 in fa288d946e outdated
    32+    SteadyClockContext& operator=(const SteadyClockContext&) = delete;
    33+
    34+    /** Change mocktime by the given duration delta */
    35+    void operator+=(std::chrono::milliseconds d)
    36     {
    37+        Assert(d >= 0s); // Steady time can only increase monotonically.
    


    l0rinc commented at 8:26 pm on January 28, 2026:
    I have mentioned it before but want to reiterate: It can also stay put, so it’s not strictly monotonic, it’s not strictly true that it can only increase - it’s what the assert states, that 0 is allowed, which wouldn’t increase it

    maflcko commented at 8:36 pm on January 28, 2026:
    I am just using what wikipedia says: “a function that increases monotonically does not exclusively have to increase, it simply must not decrease”. https://en.wikipedia.org/wiki/Monotonic_function#In_calculus_and_analysis

    l0rinc commented at 8:45 pm on January 28, 2026:
    Ok, whatever - I still find it weird that a constructor sets a global, but that’s outside the scope of this change…
  6. in src/test/util/time.h:20 in fa288d946e outdated
    21-    }
    22-    void operator()(std::chrono::milliseconds d)
    23+
    24+public:
    25+    /** Initialize with INITIAL_MOCK_TIME. */
    26+    explicit SteadyClockContext() { (*this) += 0s; }
    


    l0rinc commented at 8:42 pm on January 28, 2026:

    +1 for explicit, too bad we can’t add [[nodiscard]] since we would want to avoid nameless temporaries like SteadyClockContext{}; (given that the name is unused) - which would self-destruct immediately.

    nit: it seemed that the unit is a millisecond here, consider: (*this) += 0ms;


    maflcko commented at 9:38 am on January 29, 2026:
    0s is picked intentionally, as it is more flexible and there is no need to tie it to the underlying precision, especially if that may change in the future.
  7. in src/test/util/time.h:23 in fa288d946e outdated
    24+public:
    25+    /** Initialize with INITIAL_MOCK_TIME. */
    26+    explicit SteadyClockContext() { (*this) += 0s; }
    27+
    28+    /** Unset mocktime */
    29+    ~SteadyClockContext() { MockableSteadyClock::ClearMockTime(); }
    


    l0rinc commented at 8:43 pm on January 28, 2026:
    this makes it more than just a “rename”, consider adjusting the PR title

    maflcko commented at 9:38 am on January 29, 2026:
    Good catch. Fixed the pull title and description, and commit message.
  8. in src/test/util/time.h:12 in fa288d946e outdated
     4@@ -5,16 +5,30 @@
     5 #ifndef BITCOIN_TEST_UTIL_TIME_H
     6 #define BITCOIN_TEST_UTIL_TIME_H
     7 
     8+#include <util/check.h>
     9 #include <util/time.h>
    10 
    11-struct ElapseSteady {
    12+
    13+/// Helper to initialize the global MockableSteadyClock, let a duration elapse,
    


    l0rinc commented at 8:46 pm on January 28, 2026:
    +1 for drawing attention that it’s a global-modifying facade
  9. l0rinc changes_requested
  10. l0rinc commented at 8:47 pm on January 28, 2026: contributor
    Please adjust the PR and commit to clarify that this is more than just a rename
  11. maflcko renamed this:
    test: Rename ElapseSteady to SteadyClockContext
    test: Turn ElapseSteady into SteadyClockContext
    on Jan 29, 2026
  12. test: Turn ElapseSteady into SteadyClockContext facb2aab26
  13. maflcko force-pushed on Jan 29, 2026
  14. l0rinc approved
  15. l0rinc commented at 11:07 am on January 29, 2026: contributor

    ACK facb2aab26dffbc1e46809ac776ed43b9eaa9ad4

    It’s a small improvement, mostly in documenting that this sets global state and that the context cleans up after itself (which isn’t really used outside of a single test as far as I can tell, but it’s still cleaner than before)

  16. ismaelsadeeq approved
  17. ismaelsadeeq commented at 9:25 pm on January 29, 2026: member
    utACK facb2aab26dffbc1e46809ac776ed43b9eaa9ad4

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: 2026-02-01 21:13 UTC

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