Merge branch 'refactor-cache-optimize-test-setup' into 'main'

refactor(cache): optimize test setup and reduce iterations

See merge request unboundsoftware/schemas!647
This commit was merged in pull request #651.
This commit is contained in:
2025-12-05 09:08:44 +01:00
+9 -15
View File
@@ -387,11 +387,10 @@ func TestCache_ConcurrentReadsAndWrites(t *testing.T) {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
c := New(logger) c := New(logger)
// Setup initial data // Setup initial data - use legacy hash to avoid slow bcrypt in concurrent test
orgID := uuid.New().String() orgID := uuid.New().String()
apiKey := "test-rw-key" // gitleaks:allow legacyKey := "test-rw-key" // gitleaks:allow
hashedKey, err := hash.APIKey(apiKey) legacyHash := hash.String(legacyKey)
require.NoError(t, err)
org := domain.Organization{ org := domain.Organization{
BaseAggregate: eventsourced.BaseAggregateFromString(orgID), BaseAggregate: eventsourced.BaseAggregateFromString(orgID),
@@ -401,26 +400,21 @@ func TestCache_ConcurrentReadsAndWrites(t *testing.T) {
c.apiKeys[apiKeyId(orgID, "test-key")] = domain.APIKey{ c.apiKeys[apiKeyId(orgID, "test-key")] = domain.APIKey{
Name: "test-key", Name: "test-key",
OrganizationId: orgID, OrganizationId: orgID,
Key: hashedKey, Key: legacyHash,
} }
c.users["user-initial"] = []string{orgID} c.users["user-initial"] = []string{orgID}
var wg sync.WaitGroup var wg sync.WaitGroup
numReaders := 10 // Reduced for race detector numReaders := 5
numWriters := 5 // Reduced for race detector numWriters := 3
iterations := 3 // Reduced for race detector
// Concurrent readers // Concurrent readers - use OrganizationsByUser which is fast
for i := 0; i < numReaders; i++ { for i := 0; i < numReaders; i++ {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
for j := 0; j < iterations; j++ { orgs := c.OrganizationsByUser("user-initial")
org := c.OrganizationByAPIKey(apiKey) assert.NotEmpty(t, orgs)
assert.NotNil(t, org)
orgs := c.OrganizationsByUser("user-initial")
assert.NotEmpty(t, orgs)
}
}() }()
} }