@@ -213,3 +213,65 @@ func TestTelegramNotify(t *testing.T) {
213213 })
214214 }
215215}
216+
217+ func TestTelegramTimeout (t * testing.T ) {
218+ token := "secret"
219+
220+ tests := []struct {
221+ name string
222+ latency time.Duration
223+ timeout time.Duration
224+ wantErr bool
225+ }{
226+ {
227+ name : "success" ,
228+ latency : 100 * time .Millisecond ,
229+ timeout : 120 * time .Millisecond ,
230+ wantErr : false ,
231+ },
232+ {
233+ name : "timeout" ,
234+ latency : 100 * time .Millisecond ,
235+ timeout : 80 * time .Millisecond ,
236+ wantErr : true ,
237+ },
238+ }
239+
240+ for _ , tc := range tests {
241+ t .Run (tc .name , func (t * testing.T ) {
242+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
243+ require .Equal (t , "/bot" + token + "/sendMessage" , r .URL .Path )
244+ _ , err := io .ReadAll (r .Body )
245+ require .NoError (t , err )
246+ time .Sleep (tc .latency )
247+ w .Write ([]byte (`{"ok":true,"result":{"chat":{}}}` ))
248+ }))
249+ defer srv .Close ()
250+ u , _ := url .Parse (srv .URL )
251+
252+ cfg := & config.TelegramConfig {
253+ Message : "test" ,
254+ HTTPConfig : & commoncfg.HTTPClientConfig {},
255+ BotToken : commoncfg .Secret (token ),
256+ Timeout : tc .timeout ,
257+ APIUrl : & amcommoncfg.URL {URL : u },
258+ }
259+
260+ notifier , err := New (cfg , test .CreateTmpl (t ), promslog .NewNopLogger ())
261+ require .NoError (t , err )
262+
263+ ctx := context .Background ()
264+ ctx = notify .WithGroupKey (ctx , "1" )
265+
266+ alert := & types.Alert {
267+ Alert : model.Alert {
268+ StartsAt : time .Now (),
269+ EndsAt : time .Now ().Add (time .Hour ),
270+ },
271+ }
272+
273+ _ , err = notifier .Notify (ctx , alert )
274+ require .Equal (t , tc .wantErr , err != nil )
275+ })
276+ }
277+ }
0 commit comments