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

[Fake clock] Make Stop / Reset return false if Timer stopped #320

Conversation

antoninbas
Copy link
Contributor

What type of PR is this?

/kind bug

This is a bug in the fake clock implementation, but not one that impacts K8s users.

What this PR does / why we need it:

In order to conform to the standard library Timer, Stop and Reset should return false if the Timer has already been stopped, which was not the case previously.

We add unit tests to validate the new implementation. The updated TestFakeStop test case and the new TestFakeReset/reset_stopped_timer case fail with the old implementation.

We also simplify the implementation of Stop and Rest in the following way: there is no need to check f.waiter.fired to determine whether a Timer has expired. For Timers, this flag is set atomically with the waiter being removed from the waiters list. As a result, we only check for absence of the waiter from the list, which either indicates that the Timer has been stopped or that it has already expired (fired).

Which issue(s) this PR fixes:

Special notes for your reviewer:

Release note:


In order to conform to the standard library Timer, Stop and Reset should
return false if the Timer has already been stopped, which was not the
case previously.

We add unit tests to validate the new implementation. The updated
TestFakeStop test case and the new TestFakeReset/reset_stopped_timer
case fail with the old implementation.

We also simplify the implementation of Stop and Rest in the following
way: there is no need to check f.waiter.fired to determine whether a
Timer has expired. For Timers, this flag is set atomically with the
waiter being removed from the waiters list. As a result, we only check
for absence of the waiter from the list, which either indicates that the
Timer has been stopped or that it has already expired (fired).

Signed-off-by: Antonin Bas <[email protected]>
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Nov 22, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @antoninbas!

It looks like this is your first PR to kubernetes/utils 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/utils has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 22, 2024
@liggitt
Copy link
Member

liggitt commented Nov 22, 2024

thanks for the PR, will defer review to others, but opened kubernetes/kubernetes#128944 in the meantime to make sure unit tests in kubernetes/kubernetes still pass with this change

@antoninbas
Copy link
Contributor Author

Thanks @liggitt - I am happy to help look into any test failures that may arise

@antoninbas
Copy link
Contributor Author

antoninbas commented Nov 26, 2024

@liggitt it seems that the 2 test failures for your validation PR are related to dependencies, and may be expected in this case?

I can see that pull-kubernetes-unit ran sucessfully.

@antoninbas
Copy link
Contributor Author

@cheftako @dashpole Could I get a review for this?

@dims
Copy link
Member

dims commented Dec 10, 2024

/assign @MadhavJivrajani @thockin

if you want to take a peek, it looks good to me and the change works fine in k/k's unit tests as well.

Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

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

This change LGTM, but I think can be cleaned up further - agree or did I miss something?

clock/testing/fake_clock.go Outdated Show resolved Hide resolved
@antoninbas antoninbas requested a review from thockin December 10, 2024 02:37
@@ -198,12 +198,10 @@ func (f *FakeClock) setTimeLocked(t time.Time) {
if w.skipIfBlocked {
select {
case w.destChan <- t:
w.fired = true
default:
Copy link
Member

Choose a reason for hiding this comment

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

This field is still defined in the struct, L51

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, sorry about that
removed

@thockin thockin added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Dec 10, 2024
Removed unused fired field for waiters.

Signed-off-by: Antonin Bas <[email protected]>
@antoninbas antoninbas force-pushed the fake-clock-fix-stop-behavior-when-timer-already-stopped branch from e00eb21 to 89f8b1e Compare December 10, 2024 05:38
@thockin
Copy link
Member

thockin commented Dec 10, 2024

Thanks!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 10, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: antoninbas, thockin

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 10, 2024
@k8s-ci-robot k8s-ci-robot merged commit 24370be into kubernetes:master Dec 10, 2024
14 checks passed
@antoninbas
Copy link
Contributor Author

Thanks for the careful review @thockin !

@antoninbas antoninbas deleted the fake-clock-fix-stop-behavior-when-timer-already-stopped branch December 10, 2024 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants