-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_test.go
More file actions
169 lines (134 loc) · 3.7 KB
/
group_test.go
File metadata and controls
169 lines (134 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package lifecycle
import (
"context"
"errors"
"strings"
"testing"
"time"
"github.com/aretw0/lifecycle/pkg/core/metrics"
"github.com/aretw0/lifecycle/pkg/core/metrics/mock"
)
func TestGroup_PanicRecovery(t *testing.T) {
// Setup mock provider
mockProvider := mock.New()
metrics.SetProvider(mockProvider)
ctx := context.Background()
g, _ := NewGroup(ctx)
// Launch a goroutine that panics
g.Go(func(ctx context.Context) error {
panic("oops")
})
err := g.Wait()
if err == nil {
t.Fatal("Expected error from panic, got nil")
}
if !strings.Contains(err.Error(), "panic in lifecycle.Group") {
t.Errorf("Expected panic error message, got: %v", err)
}
// Verify metrics
if mockProvider.GoroutinesPanicked != 1 {
t.Errorf("Expected 1 panicked goroutine, got %d", mockProvider.GoroutinesPanicked)
}
}
func TestGroup_Metrics(t *testing.T) {
mockProvider := mock.New()
metrics.SetProvider(mockProvider)
ctx := context.Background()
g, _ := NewGroup(ctx)
g.Go(func(ctx context.Context) error {
return nil
})
g.Wait()
if mockProvider.GoroutinesStarted != 1 {
t.Errorf("Expected 1 started goroutine, got %d", mockProvider.GoroutinesStarted)
}
if mockProvider.GoroutinesFinished != 1 {
t.Errorf("Expected 1 finished goroutine, got %d", mockProvider.GoroutinesFinished)
}
}
func TestGroup_ErrorPropagation(t *testing.T) {
ctx := context.Background()
g, _ := NewGroup(ctx)
expectedErr := errors.New("worker error")
g.Go(func(ctx context.Context) error {
return expectedErr
})
err := g.Wait()
if err != expectedErr {
t.Errorf("Expected %v, got %v", expectedErr, err)
}
}
func TestGroup_Limit(t *testing.T) {
ctx := context.Background()
g, _ := NewGroup(ctx)
g.SetLimit(1)
// Channel to signal that the first goroutine is running
firstRunning := make(chan struct{})
// Channel to block the first goroutine
blockFirst := make(chan struct{})
g.Go(func(ctx context.Context) error {
close(firstRunning)
<-blockFirst
return nil
})
<-firstRunning
// Try to launch a second goroutine. It should block.
// We can't easily assert "it blocks" without a timeout or race condition check,
// but we can assert that it *eventually* runs after we unblock the first.
secondDone := make(chan struct{})
go func() {
g.Go(func(ctx context.Context) error {
close(secondDone)
return nil
})
}()
select {
case <-secondDone:
t.Fatal("Second goroutine started despite limit=1")
case <-time.After(10 * time.Millisecond):
// Good, it didn't start immediately
}
// Unblock first
close(blockFirst)
select {
case <-secondDone:
// Good, it started after capacity freed
case <-time.After(1 * time.Second):
t.Fatal("Second goroutine did not start after unblocking")
}
g.Wait()
}
func TestGroup_BackpressureMetrics(t *testing.T) {
mockProvider := mock.New()
metrics.SetProvider(mockProvider)
ctx := context.Background()
g, _ := NewGroup(ctx)
g.SetLimit(1)
// Block the slot
releaseSlot := make(chan struct{})
g.Go(func(ctx context.Context) error {
<-releaseSlot
return nil
})
// Wait for first routine to start
// (Best effort wait, or we need a way to know it started.
// The mock provider is naive, but we can check GoroutinesStarted)
time.Sleep(10 * time.Millisecond)
// Launch second routine (should wait)
done := make(chan struct{})
go func() {
g.Go(func(ctx context.Context) error {
close(done)
return nil
})
}()
// Give it time to enter the queue
time.Sleep(50 * time.Millisecond)
// It should verify that we recorded a Waiting goroutine (eventually).
// Since our mock relies on Inc/Dec, we can't easily check "Current Waiting" without a counter.
// But we can assume IncGoroutineWaiting was called.
// Unblock
close(releaseSlot)
<-done
g.Wait()
}