Skip to content

Increase margin of error in an otherwise unsound test - #4843

Merged
jetstack-bot merged 1 commit into
cert-manager:masterfrom
SgtCoDFish:unsoundsched
Feb 11, 2022
Merged

jetstack-bot merged 1 commit into
cert-manager:masterfrom
SgtCoDFish:unsoundsched

Conversation

@SgtCoDFish

Copy link
Copy Markdown
Member

This test can easily fail on a heavily loaded machine, such as one running many tests in parallel or a weaker developer laptop.

  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 (it was such a flake which inspired this PR)
  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.

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

NONE

@jetstack-bot jetstack-bot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 10, 2022
@munnerz

munnerz commented Feb 11, 2022

Copy link
Copy Markdown
Member

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 👀

Comment thread pkg/scheduler/scheduler_test.go Outdated

<-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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 😅

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@maelvls maelvls Feb 11, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.271s

As 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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]>
@jetstack-bot jetstack-bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 11, 2022
@jahrlin

jahrlin commented Feb 11, 2022 •

Copy link
Copy Markdown
Contributor

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
/approve

@jetstack-bot jetstack-bot added the lgtm Indicates that a PR is ready to be merged. label Feb 11, 2022
@jetstack-bot

Copy link
Copy Markdown
Contributor

[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

Details 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

@maelvls

maelvls commented Feb 11, 2022

Copy link
Copy Markdown
Member

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 duration

It 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.

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. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants