MCPcopy Index your code
hub / github.com/flant/shell-operator / TestConcurrentOperations

Function TestConcurrentOperations

pkg/task/queue/queue_test.go:13–141  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

11)
12
13func TestConcurrentOperations(t *testing.T) {
14 queue := NewTasksQueue(
15 "test",
16 metricsstorage.NewMetricStorage(metricsstorage.WithNewRegistry()),
17 WithContext(context.Background()),
18 )
19
20 // Use a smaller number of goroutines and operations to avoid overwhelming the system
21 numGoroutines := 10
22 opsPerGoroutine := 10
23
24 var wg sync.WaitGroup
25
26 // 10 goroutines adding tasks
27 for i := 0; i < numGoroutines; i++ {
28 wg.Add(1)
29 go func() {
30 defer wg.Done()
31 for j := 0; j < opsPerGoroutine; j++ {
32 newTask := task.NewTask("test")
33 queue.AddLast(newTask)
34 }
35 }()
36 }
37
38 // 10 goroutines adding tasks before a random existing task
39 for i := 0; i < numGoroutines; i++ {
40 wg.Add(1)
41 go func() {
42 defer wg.Done()
43 for j := 0; j < opsPerGoroutine; j++ {
44 newTask := task.NewTask("test")
45 // Try to add before a task that might exist
46 existingTask := queue.GetFirst()
47 if existingTask != nil {
48 queue.AddBefore(existingTask.GetId(), newTask)
49 } else {
50 queue.AddLast(newTask)
51 }
52 }
53 }()
54 }
55
56 // 10 goroutines adding tasks after a random existing task
57 for i := 0; i < numGoroutines; i++ {
58 wg.Add(1)
59 go func() {
60 defer wg.Done()
61 for j := 0; j < opsPerGoroutine; j++ {
62 newTask := task.NewTask("test")
63 // Try to add after a task that might exist
64 existingTask := queue.GetFirst()
65 if existingTask != nil {
66 queue.AddAfter(existingTask.GetId(), newTask)
67 } else {
68 queue.AddLast(newTask)
69 }
70 }

Callers

nothing calls this directly

Calls 14

NewTaskFunction · 0.92
NewTasksQueueFunction · 0.85
WithContextFunction · 0.85
AddMethod · 0.65
AddLastMethod · 0.65
GetFirstMethod · 0.65
GetIdMethod · 0.65
WaitMethod · 0.65
AddBeforeMethod · 0.45
AddAfterMethod · 0.45
DeleteFuncMethod · 0.45
LengthMethod · 0.45

Tested by

no test coverage detected