Fix cancelling timers in FakeClock package #37
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This existing timer implementation used for testing does not properly support cancellation.
This is because of how pointers and copies are handled in the FakeClock structure.
Here, we determine whether to still retain a timer by comparing the registered waiters and the current timer/waiter, and if they do not match, we retain the timer (as it is not the timer being cancelled): https://github.com/kubernetes/utils/blob/master/clock/testing/fake_clock.go#L225-L240
However, we instantiate the
fakeClockWaiter
as a non-pointer within the Timer structure, and record this structure in a slice stored on the FakeClock. You can see that happening here: https://github.com/kubernetes/utils/blob/master/clock/testing/fake_clock.go#L89-L94The issue is that on L94, we actually create a copy of the
fakeClockWaiter
, which means that the comparison on L232 will never match, and so all timers will be retained.This PR changes the slice in the FakeClock to be a slice of pointers, which resolves the issue.
Unit tests still pass with this new adapted version, and my own tests that rely on this package are now passing too.