Files
gosec/regex_cache_test.go
T
oittaa 89685023f9 feat: implement global cache usage in rules (#1480)
* feat: implement global cache usage in rules

* refactor: make global cache generic with local key types

- Remove GlobalKey struct from gosec_cache.go
- Each use case now defines its own key type (type safety via Go's type system)
- Move RegexMatchWithCache to separate regex_cache.go file
- Move cache kind constants to rules/hardcoded_credentials.go as local types
- Add documentation for cache key requirements
2026-01-26 13:29:03 +01:00

38 lines
791 B
Go

package gosec
import (
"fmt"
"regexp"
"sync"
"testing"
)
func TestGlobalCache_Stress(t *testing.T) {
// Simple stress test to ensure thread safety (running with -race is ideal)
// We can't easily assert on race conditions without the race detector,
// but this ensures no obvious panics or deadlocks.
const routines = 10
const iterations = 100
// Use a test regex for the cache key
testRe := regexp.MustCompile(`test`)
var wg sync.WaitGroup
wg.Add(routines)
for i := range routines {
go func(id int) {
defer wg.Done()
key := regexCacheKey{Re: testRe, Str: fmt.Sprintf("str-%d", id)}
for j := range iterations {
GlobalCache.Add(key, j)
if _, ok := GlobalCache.Get(key); !ok {
t.Errorf("failed to get key %v", key)
}
}
}(i)
}
wg.Wait()
}