@@ -135,6 +135,63 @@ func TestAlertsSubscribePutStarvation(t *testing.T) {
135
135
}
136
136
}
137
137
138
+ func TestDeadLock (t * testing.T ) {
139
+ t0 := time .Now ()
140
+ t1 := t0 .Add (5 * time .Second )
141
+
142
+ marker := types .NewMarker (prometheus .NewRegistry ())
143
+ // Run gc every 5 milliseconds to increase the possibility of a deadlock with Subscribe()
144
+ alerts , err := NewAlerts (context .Background (), marker , 5 * time .Millisecond , noopCallback {}, log .NewNopLogger (), nil )
145
+ if err != nil {
146
+ t .Fatal (err )
147
+ }
148
+ alertsToInsert := []* types.Alert {}
149
+ for i := 0 ; i < 200 + 1 ; i ++ {
150
+ alertsToInsert = append (alertsToInsert , & types.Alert {
151
+ Alert : model.Alert {
152
+ // Make sure the fingerprints differ
153
+ Labels : model.LabelSet {"iteration" : model .LabelValue (strconv .Itoa (i ))},
154
+ Annotations : model.LabelSet {"foo" : "bar" },
155
+ StartsAt : t0 ,
156
+ EndsAt : t1 ,
157
+ GeneratorURL : "http://example.com/prometheus" ,
158
+ },
159
+ UpdatedAt : t0 ,
160
+ Timeout : false ,
161
+ })
162
+ }
163
+
164
+ if err := alerts .Put (alertsToInsert ... ); err != nil {
165
+ t .Fatal ("Unable to add alerts" )
166
+ }
167
+ done := make (chan bool )
168
+
169
+ // call subscribe repeatedly in a goroutine to increase
170
+ // the possibility of a deadlock occurring
171
+ go func () {
172
+ tick := time .NewTicker (10 * time .Millisecond )
173
+ defer tick .Stop ()
174
+ stopAfter := time .After (1 * time .Second )
175
+ for {
176
+ select {
177
+ case <- tick .C :
178
+ alerts .Subscribe ()
179
+ case <- stopAfter :
180
+ done <- true
181
+ break
182
+ }
183
+ }
184
+ }()
185
+
186
+ select {
187
+ case <- done :
188
+ // no deadlock
189
+ alerts .Close ()
190
+ case <- time .After (10 * time .Second ):
191
+ t .Error ("Deadlock detected" )
192
+ }
193
+ }
194
+
138
195
func TestAlertsPut (t * testing.T ) {
139
196
marker := types .NewMarker (prometheus .NewRegistry ())
140
197
alerts , err := NewAlerts (context .Background (), marker , 30 * time .Minute , noopCallback {}, log .NewNopLogger (), nil )
0 commit comments