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

Use pluggable clock for auto refresh cache and make unit tests fast #5767

Merged
merged 2 commits into from
Sep 24, 2024

Conversation

Sovietaced
Copy link
Contributor

@Sovietaced Sovietaced commented Sep 22, 2024

Tracking issue

Closes #5711

Why are the changes needed?

Previously this unit test was relying on a real clock which makes unit tests slow and flaky due to nondeterministic execution duration. If code takes a dependency on time it should use a pluggable clock.

Before

ok      github.com/flyteorg/flyte/flytestdlib/cache     8.828s

After

ok      github.com/flyteorg/flyte/flytestdlib/cache     0.277s

What changes were proposed in this pull request?

autoRefresh and the work queue it relies were updated to use a pluggable clock. A fake clock is used for unit tests while a real clock is used under all other circumstances.

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Related PRs

Docs link

// If the cache tries to enqueue the item, a panic will be thrown.
time.Sleep(500 * time.Millisecond)
fakeClock.Step(testResyncPeriod)
Copy link
Contributor Author

@Sovietaced Sovietaced Sep 22, 2024

Choose a reason for hiding this comment

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

These tests aren't perfect with how things are asynchronous... It's hard to verify that a sync actually happened. I wanted to add a sync counter to the auto refresh struct to use in unit tests but the constructor IMO incorrectly returns the interface instead of a pointer to the struct (which should adhere to the interface). As such, the unit tests can't access anything package level which is a bit unfortunate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to fix that but I'm trying to reduce the blast radius of this change.

Copy link

codecov bot commented Sep 22, 2024

Codecov Report

Attention: Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 36.31%. Comparing base (59bf191) to head (38f9700).
Report is 15 commits behind head on master.

Files with missing lines Patch % Lines
flytestdlib/cache/auto_refresh.go 88.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5767      +/-   ##
==========================================
+ Coverage   36.21%   36.31%   +0.09%     
==========================================
  Files        1303     1305       +2     
  Lines      109644   110017     +373     
==========================================
+ Hits        39710    39949     +239     
- Misses      65810    65912     +102     
- Partials     4124     4156      +32     
Flag Coverage Δ
unittests-datacatalog 51.37% <ø> (ø)
unittests-flyteadmin 55.62% <ø> (+0.01%) ⬆️
unittests-flytecopilot 12.17% <ø> (ø)
unittests-flytectl 62.21% <ø> (ø)
unittests-flyteidl 7.12% <ø> (ø)
unittests-flyteplugins 53.35% <ø> (ø)
unittests-flytepropeller 41.89% <ø> (+0.13%) ⬆️
unittests-flytestdlib 55.37% <88.00%> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -406,3 +428,8 @@ func NewAutoRefreshCache(name string, syncCb SyncFunc, syncRateLimiter workqueue

return NewAutoRefreshBatchedCache(name, SingleItemBatches, syncCb, syncRateLimiter, resyncPeriod, parallelizm, size, scope)
}

func newAutoRefreshCacheWithClock(name string, syncCb SyncFunc, syncRateLimiter workqueue.RateLimiter, resyncPeriod time.Duration,
Copy link
Contributor Author

@Sovietaced Sovietaced Sep 22, 2024

Choose a reason for hiding this comment

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

I've added these package level constructors for testing but honestly with all the arguments that can be configured on the auto refresh cache it would be nicer to have a constructor with Options. I can follow up with that in another pull request if you'd like.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can do that in a separate PR.

@Sovietaced Sovietaced changed the title Use pluggable clock and make unit tests fast Use pluggable clock for auto refresh cache and make unit tests fast Sep 22, 2024
Signed-off-by: Jason Parraga <[email protected]>
@Sovietaced Sovietaced marked this pull request as ready for review September 22, 2024 06:17
Copy link
Contributor

@eapolinario eapolinario left a comment

Choose a reason for hiding this comment

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

Thank you for working on this.

@@ -406,3 +428,8 @@ func NewAutoRefreshCache(name string, syncCb SyncFunc, syncRateLimiter workqueue

return NewAutoRefreshBatchedCache(name, SingleItemBatches, syncCb, syncRateLimiter, resyncPeriod, parallelizm, size, scope)
}

func newAutoRefreshCacheWithClock(name string, syncCb SyncFunc, syncRateLimiter workqueue.RateLimiter, resyncPeriod time.Duration,
Copy link
Contributor

Choose a reason for hiding this comment

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

We can do that in a separate PR.

@eapolinario eapolinario merged commit c01a059 into flyteorg:master Sep 24, 2024
50 checks passed
@Sovietaced Sovietaced mentioned this pull request Oct 5, 2024
3 tasks
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

Successfully merging this pull request may close these issues.

[BUG] flytestdlib/cache unit tests are flaky
2 participants