[test] Add tests for syncutil.TTLCache.GetOrCreate with maxSize <= 0#7491
Merged
Conversation
Cover the uncovered branch where maxSize is zero or negative causes GetOrCreate to bypass the cache entirely and call create() on every invocation. This brings syncutil package coverage from 97.7% to 100%. The passthrough-cache behaviour (maxSize <= 0) is a deliberate design choice that lets callers disable caching without changing call-sites. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves test coverage for internal/syncutil.TTLCache.GetOrCreate by adding a focused unit test for the maxSize <= 0 passthrough behavior, ensuring the previously uncovered early-return branch is exercised.
Changes:
- Added a table-driven test that verifies
maxSize = 0and negative values disable caching. - Asserts
create()is invoked on every call (even for the same key), returns fresh values, andLen()remains0.
Show a summary per file
| File | Description |
|---|---|
| internal/syncutil/ttl_cache_test.go | Adds coverage for the maxSize <= 0 GetOrCreate passthrough path (no storage, always calls create()). |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 0
This was referenced Jun 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Test Coverage Improvement:
TTLCache.GetOrCreateFunction Analyzed
internal/syncutil(*TTLCache[K, V]).GetOrCreatemaxSize <= 0) code pathWhy This Function?
Running
go test -coverprofile=coverage.out ./internal/syncutil/...revealed thatTTLCache.GetOrCreatehad one uncovered branch:When
maxSizeis zero or negative,GetOrCreateintentionally becomes a passthrough: it callscreate()on every invocation and never stores anything. This is a deliberate design choice that lets callers disable caching without modifying call-sites. The branch existed but had zero test coverage.Tests Added
maxSize = 0— boundary value; cache is fully disabledmaxSize = -1— negative value; cache is disabledmaxSize = -100— large negative; cache is disabledcreate()is called on every invocation (no caching)Len()always returns 0 (nothing is stored)Coverage Report
Test Execution
All tests pass:
Generated by Test Coverage Improver
Next run will target the next most complex under-tested function
Warning
Firewall blocked 9 domains
The following domains were blocked by the firewall during workflow execution:
go.googlesource.comgo.opentelemetry.iogo.yaml.ingolang.orggoogle.golang.orggopkg.ingoproxy.ioproxy.golang.orgreleaseassets.githubusercontent.comSee Network Configuration for more information.