@@ -234,3 +234,56 @@ func TestNotifier_Notify_WithReason(t *testing.T) {
234234 })
235235 }
236236}
237+
238+ func TestSlackTimeout (t * testing.T ) {
239+ tests := map [string ]struct {
240+ latency time.Duration
241+ timeout time.Duration
242+ wantErr bool
243+ }{
244+ "success" : {latency : 100 * time .Millisecond , timeout : 120 * time .Millisecond , wantErr : false },
245+ "error" : {latency : 100 * time .Millisecond , timeout : 80 * time .Millisecond , wantErr : true },
246+ }
247+
248+ for name , tt := range tests {
249+ t .Run (name , func (t * testing.T ) {
250+ u , _ := url .Parse ("https://slack.com/post.Message" )
251+ notifier , err := New (
252+ & config.SlackConfig {
253+ NotifierConfig : config.NotifierConfig {},
254+ HTTPConfig : & commoncfg.HTTPClientConfig {},
255+ APIURL : & config.SecretURL {URL : u },
256+ Channel : "channelname" ,
257+ Timeout : tt .timeout ,
258+ },
259+ test .CreateTmpl (t ),
260+ promslog .NewNopLogger (),
261+ )
262+ require .NoError (t , err )
263+ notifier .postJSONFunc = func (ctx context.Context , client * http.Client , url string , body io.Reader ) (* http.Response , error ) {
264+ select {
265+ case <- ctx .Done ():
266+ return nil , ctx .Err ()
267+ case <- time .After (tt .latency ):
268+ resp := httptest .NewRecorder ()
269+ resp .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
270+ resp .WriteHeader (http .StatusOK )
271+ resp .WriteString (`{"ok":true}` )
272+
273+ return resp .Result (), nil
274+ }
275+ }
276+ ctx := context .Background ()
277+ ctx = notify .WithGroupKey (ctx , "1" )
278+
279+ alert := & types.Alert {
280+ Alert : model.Alert {
281+ StartsAt : time .Now (),
282+ EndsAt : time .Now ().Add (time .Hour ),
283+ },
284+ }
285+ _ , err = notifier .Notify (ctx , alert )
286+ require .Equal (t , tt .wantErr , err != nil )
287+ })
288+ }
289+ }
0 commit comments