Skip to content

NetworkClock: add manually advanced clock for deterministic tests - #74

Open
rpaulo wants to merge 1 commit into
apple:mainfrom
rpaulo:manual-clock
Open

NetworkClock: add manually advanced clock for deterministic tests#74
rpaulo wants to merge 1 commit into
apple:mainfrom
rpaulo:manual-clock

Conversation

@rpaulo

@rpaulo rpaulo commented Aug 10, 2026

Copy link
Copy Markdown
Contributor
  • Replace configurable-initial-value clock with a manual time backing
    store that tests can freeze and advance by hand
  • Route now and nowAbsolute through manual time when set, falling
    back to the system clock otherwise
  • Track continuous and absolute time separately, preserving nanosecond
    resolution beyond the system clock's microsecond truncation
  • Add tests covering freezing, advancing, and restoring the clock

- Replace configurable-initial-value clock with a manual time backing
  store that tests can freeze and advance by hand
- Route `now` and `nowAbsolute` through manual time when set, falling
  back to the system clock otherwise
- Track continuous and absolute time separately, preserving nanosecond
  resolution beyond the system clock's microsecond truncation
- Add tests covering freezing, advancing, and restoring the clock

@tfpauly tfpauly left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! Mainly just questioning if we can use a better availability check.

public struct Instant: InstantProtocol, CustomStringConvertible {
var time: NetworkDuration

#if !DisableDebugLogging

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit odd to tie this to debug logging — I assume this is just intended to compile stuff out on prod / release builds?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah pretty much to avoid the memory cost of adding a class. The CPU cost of the extra check in now() isn't that bad.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we tie it being a debug build or some other trait? I don't think it necessarily makes sense to have this be the logging trait.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't tie it to a debug build because we run tests in release mode AFAIK.
I didn't want a trait just for this, that's why I used the debug logging trait which is always enabled during the tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants