Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 18, 2025

Closes: #issue

Description

Tests calling stubRepoAccessCache() created separate singleton instances due to sync.Once semantics—first initialization won, subsequent calls with different mock clients were ignored. This caused test failures when subtests needed specific GraphQL mock behavior.

Changes

pkg/lockdown/lockdown.go

  • Added ResetInstance() to clear singleton state between tests

Test files (pkg/github/issues_test.go, pkg/github/pullrequests_test.go, pkg/lockdown/lockdown_test.go)

  • Initialize cache once at test function start via ResetInstance() + GetInstance()
  • Subtests conditionally reset when requiring custom mock clients
  • 11 test functions updated across 3 files

Example

func Test_GetIssue(t *testing.T) {
    // Initialize once for all subtests
    gqlClient := githubv4.NewClient(nil)
    lockdown.ResetInstance()
    lockdown.GetInstance(gqlClient, lockdown.WithTTL(5*time.Minute))
    
    // Subtests share the same cache instance
    for _, tc := range tests {
        t.Run(tc.name, func(t *testing.T) {
            if tc.gqlHTTPClient != nil {
                // Reset only when custom mock needed
                lockdown.ResetInstance()
                gqlClient = githubv4.NewClient(tc.gqlHTTPClient)
            }
            // ...
        })
    }
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/graphql
    • Triggering command: /tmp/go-build1162513046/b001/github.test -test.testlogfile=/tmp/go-build1162513046/b001/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -test.run=Test_GetIssue (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Update all tests using lockdown cache and make them initialize cache only once instead of separately for each test.

The user has attached the following file paths as relevant context:

  • .github/copilot-instructions.md

Created from VS Code via the GitHub Pull Request extension.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

JoannaaKL and others added 12 commits November 17, 2025 14:26
* Initial plan

* Replace custom cache with cache2go library

- Added github.com/muesli/cache2go dependency
- Replaced custom map-based cache with cache2go.CacheTable
- Removed manual timer management (scheduleExpiry, ensureEntry methods)
- Removed timer field from repoAccessCacheEntry struct
- Updated GetRepoAccessInfo to use cache2go's Value() and Add() methods
- Updated SetTTL to flush and re-add entries with new TTL
- Used unique cache names per instance to avoid test interference
- All existing tests pass with the new implementation

Co-authored-by: JoannaaKL <[email protected]>

* Final verification complete

Co-authored-by: JoannaaKL <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: JoannaaKL <[email protected]>
* Initial plan

* Implement RepoAccessCache as a singleton pattern

Co-authored-by: JoannaaKL <[email protected]>

* Complete singleton implementation and verification

Co-authored-by: JoannaaKL <[email protected]>

* Remove cacheIDCounter as requested

Co-authored-by: JoannaaKL <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: JoannaaKL <[email protected]>
- Added ResetInstance() function to lockdown package for test cleanup
- Updated lockdown_test.go to reset singleton before and after each test
- Updated issues_test.go to initialize cache once per test function (4 functions)
- Updated pullrequests_test.go to initialize cache once per test function (7 functions)
- All tests now explicitly reset and initialize cache at the beginning of each test function
- This prevents race conditions and ensures each test has proper cache state

Co-authored-by: JoannaaKL <[email protected]>
Copilot AI changed the title [WIP] Update tests to use lockdown cache initialization Initialize lockdown cache singleton once per test function Nov 18, 2025
Copilot AI requested a review from JoannaaKL November 18, 2025 12:50
Copilot finished work on behalf of JoannaaKL November 18, 2025 12:50
Base automatically changed from lockdown-mode-more-tools to main November 21, 2025 09:34
@stasyu2009-ux
Copy link

Код потрібно вставити готовий
і все

@stasyu2009-ux
Copy link

func Test_GetIssue(t testing.T) {
// Initialize once for all subtests
gqlClient := githubv4.NewClient(nil)
lockdown.ResetInstance()
lockdown.GetInstance(gqlClient, lockdown.WithTTL(5
time.Minute))

// Subtests share the same cache instance
for _, tc := range tests {
    t.Run(tc.name, func(t *testing.T) {
        if tc.gqlHTTPClient != nil {
            // Reset only when custom mock needed
            lockdown.ResetInstance()
            gqlClient = githubv4.NewClient(tc.gqlHTTPClient)
        }
        // ...
    })
}

}

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.

3 participants