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

FakeClock.Sleep calling FakeClock.Step may be unexpected #309

Open
SOF3 opened this issue May 17, 2024 · 2 comments
Open

FakeClock.Sleep calling FakeClock.Step may be unexpected #309

SOF3 opened this issue May 17, 2024 · 2 comments

Comments

@SOF3
Copy link

SOF3 commented May 17, 2024

What happened:

Consider the following code (playground):

package main

import (
	"time"

	"k8s.io/utils/clock"
	clocktesting "k8s.io/utils/clock/testing"
)

func main() {
	clk := clocktesting.NewFakeClock(time.Now())
	go func(clk clock.Clock) {
		clk.Sleep(time.Second * 2)
		println("after 2 fakeclock seconds")
	}(clk)

	time.Sleep(time.Millisecond) // used to emulate race condition
	clk.Sleep(time.Second)
	println("after 1 fakeclock second")
}

The code above prints

after 2 fakeclock seconds
after 1 fakeclock second

When clk is changed to clock.RealClock{} (playground), the println lines are in correct order (1 -> 2).

What you expected to happen:

Intuitively, one would expect clk.Sleep(duration) to be identical to <-clk.After(duration). However, (*FakeClock).Sleep actually calls clk.Step instead, which immediately modifies the clock.

Suggested fix:

It would be a huge BC break to many testing packages if (*FakeClock).Sleep is changed to be passive. Documentation improvement would suffice, although deprecating Sleep would be great since Sleep doesn't always do what users intend. (imo, clk.Step is usually only intended in the main control flow of the unit test rather than other goroutines, so calling clk.Sleep from non-testing code is often unintended).

Alternatively, consider adding a ref-counted clock, where Sleep and After have identical behavior and the clock is only stepped when all refs are sleeping.

Environment:
k8s.io/utils: fe8a2dd

@SOF3 SOF3 changed the title Clock.Sleep is not well-documented FakeClock.Sleep calling FakeClock.Step may be unexpected May 17, 2024
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 15, 2024
@SOF3
Copy link
Author

SOF3 commented Aug 21, 2024

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 21, 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

No branches or pull requests

3 participants