MCPcopy Create free account
hub / github.com/conforma/cli / TestGet_ConcurrentAccess

Function TestGet_ConcurrentAccess

internal/policy/cache/cache_test.go:330–391  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

328}
329
330func TestGet_ConcurrentAccess(t *testing.T) {
331 ctx := context.Background()
332
333 tests := []struct {
334 name string
335 numGoroutines int
336 numOperations int
337 expectedTotal int
338 }{
339 {
340 name: "concurrent access with 10 goroutines",
341 numGoroutines: 10,
342 numOperations: 100,
343 expectedTotal: 1000,
344 },
345 {
346 name: "concurrent access with 5 goroutines",
347 numGoroutines: 5,
348 numOperations: 50,
349 expectedTotal: 250,
350 },
351 }
352
353 for _, tt := range tests {
354 t.Run(tt.name, func(t *testing.T) {
355 // Create a fresh cache for each test
356 testCache := NewPolicyCache(ctx)
357
358 var wg sync.WaitGroup
359 wg.Add(tt.numGoroutines)
360
361 // Start concurrent goroutines
362 for i := 0; i < tt.numGoroutines; i++ {
363 go func(id int) {
364 defer wg.Done()
365 for j := 0; j < tt.numOperations; j++ {
366 key := fmt.Sprintf("key_%d_%d", id, j)
367 value := fmt.Sprintf("value_%d_%d", id, j)
368 testCache.Set(key, value, nil)
369 }
370 }(i)
371 }
372
373 wg.Wait()
374
375 // Verify all values were set correctly
376 count := 0
377 for i := 0; i < tt.numGoroutines; i++ {
378 for j := 0; j < tt.numOperations; j++ {
379 key := fmt.Sprintf("key_%d_%d", i, j)
380 expectedValue := fmt.Sprintf("value_%d_%d", i, j)
381 value, ok := testCache.Get(key)
382 assert.True(t, ok, "Key %s should exist", key)
383 assert.Equal(t, expectedValue, value, "Value for key %s should match", key)
384 count++
385 }
386 }
387

Callers

nothing calls this directly

Calls 4

SetMethod · 0.95
GetMethod · 0.95
NewPolicyCacheFunction · 0.85
RunMethod · 0.65

Tested by

no test coverage detected