MCPcopy
hub / github.com/grafana/tempo / TestConcurrency

Function TestConcurrency

modules/backendscheduler/work/work_sharded_test.go:439–552  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

437}
438
439func TestConcurrency(t *testing.T) {
440 work := New(Config{})
441 ctx := context.Background()
442
443 // Test concurrent operations
444 t.Run("concurrent adds", func(t *testing.T) {
445 const numGoroutines = 10
446 const jobsPerGoroutine = 10
447
448 // Add jobs concurrently
449 done := make(chan struct{})
450 for i := range numGoroutines {
451 go func(goroutineID int) {
452 defer func() { done <- struct{}{} }()
453
454 for j := range jobsPerGoroutine {
455 job := createTestJob(fmt.Sprintf("concurrent-%d-%d", goroutineID, j), tempopb.JobType_JOB_TYPE_COMPACTION)
456 err := work.AddJob(job)
457 require.NoError(t, err)
458 }
459 }(i)
460 }
461
462 // Wait for all goroutines
463 for range numGoroutines {
464 <-done
465 }
466
467 // Verify all jobs were added (should be numGoroutines * jobsPerGoroutine)
468 require.Equal(t, numGoroutines*jobsPerGoroutine, len(work.ListJobs()))
469 })
470
471 t.Run("concurrent operations", func(t *testing.T) {
472 var err error
473
474 // Add initial jobs
475 for i := range 50 {
476 job := createTestJob(fmt.Sprintf("ops-job-%d", i), tempopb.JobType_JOB_TYPE_COMPACTION)
477 err = work.AddJob(job)
478 require.NoError(t, err)
479 }
480
481 // Perform concurrent operations
482 const numWorkers = 5
483 done := make(chan struct{})
484
485 for i := range numWorkers {
486 go func(workerID int) {
487 defer func() { done <- struct{}{} }()
488
489 // Perform various operations
490 jobs := work.ListJobs()
491 if len(jobs) > 0 {
492 job := jobs[workerID%len(jobs)]
493 work.StartJob(job.ID)
494 work.GetJob(job.ID)
495 if workerID%2 == 0 {
496 work.CompleteJob(job.ID)

Callers

nothing calls this directly

Calls 13

createTestJobFunction · 0.85
NewFunction · 0.70
AddJobMethod · 0.65
ListJobsMethod · 0.65
StartJobMethod · 0.65
GetJobMethod · 0.65
CompleteJobMethod · 0.65
FailJobMethod · 0.65
AddMethod · 0.65
NowMethod · 0.65
PruneMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected