Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contexts that use FakeClock rather than the system time. #92

Merged
merged 5 commits into from
Nov 28, 2024

Conversation

DPJacques
Copy link
Collaborator

@DPJacques DPJacques commented Oct 19, 2024

These contexts work similar to the standard library's context.WithTimeout and context.WithDeadline, expect they use a clockwork.FakeClock for deadline determination.

By nature, the generated contexts ignore parent cancelation if the parent's cancelation returned conext.DeadlineExceeded. However, an unfortunate but necessary nuances is that once the parent is cancelled with conext.DeadlineExceeded, future calls to cancel the parent due to otherwise normal calling of the parent's cancel function are not communicated to the child. As far as I can tell, there is no way to cleanly, reliably communicate that.

Once this occurs, and the child has ignored the parent cancellation, the only way to cancel the child is to advance its fake clock past the context's deadline or call the cancel function returned when the context was created.

Other changes that were made in support of this change or as targets of opportunity:

  • newFakeTimer was made a standalone function since it needed to be used in 2 places.
  • Functions with the pattern new*AtTime were introduced to ensure the FakeClock mutex is held when we need to call time.Time.Sub(FakeClock.Now()) and pass that to a function that also needs to hold the FakeClock mutex. The common use case for this is supporting functions that take an absolute time rather than a duration.
  • Use slices.DeleteFunc where we can.

Not tested, just a progress commit.
@DPJacques DPJacques changed the title Initial FakeContext-based context implementation. Add contexts that use FakeClock rather than the system time. Oct 19, 2024
@DPJacques DPJacques marked this pull request as ready for review October 19, 2024 18:00
context.go Outdated Show resolved Hide resolved
@DPJacques DPJacques reopened this Nov 28, 2024
Copy link

@3vilhamster 3vilhamster left a comment

Choose a reason for hiding this comment

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

API is nice, and it fits into my initial request!
I am looking forward to using it inside the cadence-workflow.

@DPJacques DPJacques merged commit 91d2c0a into jonboulle:master Nov 28, 2024
9 checks passed
@DPJacques
Copy link
Collaborator Author

Merged because there are a few other clean ups and fixes I'd like to do before bumping the version.

@DPJacques DPJacques deleted the context branch November 28, 2024 23:33
@DPJacques DPJacques mentioned this pull request Dec 3, 2024
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