Repository navigation
Increase margin of error in an otherwise unsound test - #4843
Conversation
|
Using the fake clock for this test would be the most ideal here... I'm not too sure why it wasn't like that in the first place 👀 |
|
|
||
| <-wait | ||
| assert.InDelta(t, 100*time.Millisecond, end.Sub(start), float64(1*time.Millisecond)) | ||
| assert.InDelta(t, 100*time.Millisecond, end.Sub(start), float64(500*time.Millisecond)) |
There was a problem hiding this comment.
500ms is a significant variance from the target of 100ms, and instead of the variance being 1%, it's now 500% of the target value 😅
There was a problem hiding this comment.
I can't derive an actionable outcome from this review comment. I agree that 500 > 100 and that 1 * 500 == 500. I think the PR comment above was actionable, so I'll address that here instead:
Using the fake clock for this test would be the most ideal here
I disagree; this test only makes sense to me as a sanity check that afterFunc works in practice. If we use a fake clock we're testing whether the fake clock works which sounds like a test for "k8s.io/utils/clock" and not us.
I think the alternative to using a real clock for this test is to delete the test.
There was a problem hiding this comment.
My point is that the test case is checking to ensure it waits 100ms, however the allowable 'variance' here is now 500ms which is far greater than the amount of time being tested for.
So the 'action' I'm suggesting is to reduce that amount of time to something more appropriate given we're trying to check 100ms has passed (say, 10ms or 50ms).
With a 500ms variance, if the scheduler were to wait just 10ms, would the test still pass? As 10ms is within 500ms of 100ms
There was a problem hiding this comment.
Thank you for clarifying.
I think the PR description is pretty clear on why I don't think this test is sound in a fundamental way. Specifically point 3:
When we write code which uses this scheduler, we can't even safely assume the afterFunc will ever be run, let alone run within a 1ms margin of time error.
Hence the huge allowable delta. No code we write can rely on afterFunc being called within 1ms / 10ms / 50ms of the 100ms deadline because it can't rely on afterFunc being called full stop, and the smaller the allowable delta the more likely this becomes a test of how loaded the test server is.
With a 500ms variance, if the scheduler were to wait just 10ms, would the test still pass? As 10ms is within 500ms of 100ms
This is an interesting suggestion for a different sanity check. In this case we want the test to assert that end.Sub(start) >= 100*time.Millisecond, which seems worthwhile to add and I'll add that.
There was a problem hiding this comment.
When I wrote this (#4233), I wasn't able to find a way to advance manually the timer 100 ms in the future. Since clock.NewTimer returns a Go time.Timer, I could not do anything to it; setting the fake clock to 100 ms in the future would not have helped.
What I could have done is use a fake timer instead (by swapping out NewTimer during this unit test). But then, there would still be flakiness since each action (change the time vs. the timer) would have to be running in its own goroutine.
I think testing this way is OK and allows us to trust that our renewal mechanism will work with larger values (e.g., for a duration of 3 months, the user can expect the renewal to happen within 1% of the expected renewal time, which means with a delta of 21 hours around the renewal time). Since we use a duration of 100 ms in this test, a precision of 1% means that we expect the delta to be 1 ms.
That said, I missed the fact that f may be called arbitrarily at any moment, and that there is no guarantee that f would be called right after 100 ms. In fact, I ran the test 1000 times and it failed 2% of the time (I added t.Parallel() to speed up the process, although it has the tendency of worsening the situation since more goroutines = more chance of f being run later or earlier):
$ git checkout 2c254545d
$ go test ./pkg/scheduler/ -parallel=32 -count=100
--- FAIL: Test_afterFunc (0.12s)
--- FAIL: Test_afterFunc/f_is_called_after_100_milliseconds (0.10s)
scheduler_test.go:74:
Error Trace: scheduler_test.go:74
Error: Max difference between 100ms and 101.04394ms allowed is 1e+06, but difference was -1.04394e+06
Test: Test_afterFunc/f_is_called_after_100_milliseconds
--- FAIL: Test_afterFunc (0.12s)
--- FAIL: Test_afterFunc/f_is_called_after_100_milliseconds (0.10s)
scheduler_test.go:74:
Error Trace: scheduler_test.go:74
Error: Max difference between 100ms and 101.032942ms allowed is 1e+06, but difference was -1.032942e+06
Test: Test_afterFunc/f_is_called_after_100_milliseconds
FAIL
FAIL github.com/cert-manager/cert-manager/pkg/scheduler 472.271sAs a side note, Richard rightly mentioned that one important piece of comment above afterFunc was lost when I did the PR:
// For mocking purposes.
// This little bit of wrapping needs to be done because go doesn't do
// covariance, but it does coerce *time.Timer into stoppable implicitly if we
// write it out like so.
var afterFunc = func(c clock.Clock, d time.Duration, f func()) stoppable {I think I removed this comment because I did not understand it, can someone explain it to me?
To end my comment, I think we still want to make sure afterFunc does call f within a reasonable delta (I propose a delta of 1% around the expected duration), since cert-manager users expect the renewal to happen. Unfortunately, we can't run the test for a long time, such as 3 months (no one likes long unit tests). This means we have to work around the flakiness due to the short duration of 100 ms. With a duration of 3 months, the precision of 1% would mean a delta of 21 hours, which I am sure f would be called in time. I propose that we acknowledge the flakiness and that we wrap this test in a for loop, similarly to what the Go library does for testing afterFunc.
There was a problem hiding this comment.
I have created a follow-up issue #4856 with the above suggestion.
This test can easily fail on a heavily loaded machine, such as one running many tests in parallel. 1. The afterFunc could be delayed _massively_ on a heavily loaded machine, such as one running a lot of tests in parallel. 2. Requiring an accuracy of 1ms seems like a flake waiting to happen (as it was in this case) 3. When we write code which uses this scheduler, we can't even safely assume the afterFunc will _ever_ be run, let alone run within a 1% margin of time error. As such I don't think this test is providing any value beyond a general sanity check. By increasing the allowable delta massively, we keep this test as a sanity check but basically remove the chance of a flake. The test essentially becomes "does afterFunc work, generally?". Also adds a check that the elapsed time is greater than the expected time. Signed-off-by: Ashley Davis <[email protected]>
6346e25 to
89bb548
Compare
|
Thanks @SgtCoDFish! This PR is an improvement that reduces flakiness while keeping the purpose of the test (sanity check) intact. I agree that this test as a whole should probably be reworked and that there are many valid ways to do that, and if we think it is important we should create an issue to address that /lgtm |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: jahrlin, SgtCoDFish The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
I think we should fix/change the comment at the start of the file: // Note that re-implimenting AfterFunc is not a good idea, since testing it
// is tricky as seen in time_test.go in the standard library. We will just
// focus on two important cases: "f" should be run after the durationIt seems I did not end this sentence 😅 I think I meant: // Note that re-implimenting AfterFunc is not a good idea, since testing it
// is tricky as seen in time_test.go in the standard library. We will just
// focus on two important cases: "f" should be run shortly after the duration
// (since cert-manager needs to reliably renew certificates) and afterFunc
// should not leak goroutines because no one likes memory leaks. |
This test can easily fail on a heavily loaded machine, such as one running many tests in parallel or a weaker developer laptop.
Put simply, as implemented this test is more of a test of how loaded the test server is than a test of any functionality.
By increasing the allowable delta hugely, we keep this test as a sanity check but basically remove the chance of a flake. The test essentially becomes "does afterFunc work, generally?".
/kind cleanup