Skip to content

[test] Add tests for syncutil.TTLCache.GetOrCreate with maxSize <= 0#7491

Merged
lpcox merged 1 commit into
mainfrom
test/ttlcache-maxsize-coverage-5bb0b4e1151a1fe4
Jun 13, 2026
Merged

[test] Add tests for syncutil.TTLCache.GetOrCreate with maxSize <= 0#7491
lpcox merged 1 commit into
mainfrom
test/ttlcache-maxsize-coverage-5bb0b4e1151a1fe4

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Test Coverage Improvement: TTLCache.GetOrCreate

Function Analyzed

  • Package: internal/syncutil
  • Function: (*TTLCache[K, V]).GetOrCreate
  • Previous Coverage: 97.7% (package), 96.2% (function)
  • New Coverage: 100.0% (package), 100.0% (function)
  • Complexity: Medium — TTL eviction, LRU eviction, concurrent locking, and now the passthrough (maxSize <= 0) code path

Why This Function?

Running go test -coverprofile=coverage.out ./internal/syncutil/... revealed that TTLCache.GetOrCreate had one uncovered branch:

func (c *TTLCache[K, V]) GetOrCreate(key K, create func() V) V {
    if c.maxSize <= 0 {
        return create()  // ← NOT COVERED
    }
    // ... cache lookup, TTL eviction, LRU eviction ...
}

When maxSize is zero or negative, GetOrCreate intentionally becomes a passthrough: it calls create() 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 disabled
  • maxSize = -1 — negative value; cache is disabled
  • maxSize = -100 — large negative; cache is disabled
  • ✅ Verifies create() is called on every invocation (no caching)
  • ✅ Verifies successive calls return fresh values (not a cached result)
  • ✅ Verifies Len() always returns 0 (nothing is stored)

Coverage Report

Before:
github.com/github/gh-aw-mcpg/internal/syncutil/ttl_cache.go:56:  GetOrCreate  96.2%
total: (statements) 97.7%

After:
github.com/github/gh-aw-mcpg/internal/syncutil/ttl_cache.go:56:  GetOrCreate  100.0%
total: (statements) 100.0%

Improvement: +2.3pp (package), +3.8pp (function)

Test Execution

All tests pass:

=== RUN   TestTTLCache_GetOrCreate_MaxSizeZeroOrNegativeBypassesCache
=== RUN   TestTTLCache_GetOrCreate_MaxSizeZeroOrNegativeBypassesCache/zero_disables_cache
=== RUN   TestTTLCache_GetOrCreate_MaxSizeZeroOrNegativeBypassesCache/negative_one_disables_cache
=== RUN   TestTTLCache_GetOrCreate_MaxSizeZeroOrNegativeBypassesCache/large_negative_disables_cache
--- PASS: TestTTLCache_GetOrCreate_MaxSizeZeroOrNegativeBypassesCache (0.00s)
ok  github.com/github/gh-aw-mcpg/internal/syncutil  0.205s  coverage: 100.0% of statements

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.com
  • go.opentelemetry.io
  • go.yaml.in
  • golang.org
  • google.golang.org
  • gopkg.in
  • goproxy.io
  • proxy.golang.org
  • releaseassets.githubusercontent.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "go.googlesource.com"
    - "go.opentelemetry.io"
    - "go.yaml.in"
    - "golang.org"
    - "google.golang.org"
    - "gopkg.in"
    - "goproxy.io"
    - "proxy.golang.org"
    - "releaseassets.githubusercontent.com"

See Network Configuration for more information.

Generated by Test Coverage Improver · 1.8K AIC · ⊞ 28.7K ·

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>
@lpcox lpcox marked this pull request as ready for review June 13, 2026 20:37
Copilot AI review requested due to automatic review settings June 13, 2026 20:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 = 0 and negative values disable caching.
  • Asserts create() is invoked on every call (even for the same key), returns fresh values, and Len() remains 0.
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

@lpcox lpcox merged commit 19d5bc2 into main Jun 13, 2026
24 checks passed
@lpcox lpcox deleted the test/ttlcache-maxsize-coverage-5bb0b4e1151a1fe4 branch June 13, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants