engine_integration_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // +build integration
  2. package alerting
  3. import (
  4. "context"
  5. "errors"
  6. "net"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "time"
  11. "github.com/grafana/grafana/pkg/setting"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. func TestEngineTimeouts(t *testing.T) {
  15. Convey("Alerting engine timeout tests", t, func() {
  16. engine := &AlertEngine{}
  17. engine.Init()
  18. setting.AlertingNotificationTimeout = 30 * time.Second
  19. setting.AlertingMaxAttempts = 3
  20. engine.resultHandler = &FakeResultHandler{}
  21. job := &Job{running: true, Rule: &Rule{}}
  22. Convey("Should trigger as many retries as needed", func() {
  23. Convey("pended alert for datasource -> result handler should be worked", func() {
  24. // reduce alert timeout to test quickly
  25. setting.AlertingEvaluationTimeout = 30 * time.Second
  26. transportTimeoutInterval := 2 * time.Second
  27. serverBusySleepDuration := 1 * time.Second
  28. evalHandler := NewFakeCommonTimeoutHandler(transportTimeoutInterval, serverBusySleepDuration)
  29. resultHandler := NewFakeCommonTimeoutHandler(transportTimeoutInterval, serverBusySleepDuration)
  30. engine.evalHandler = evalHandler
  31. engine.resultHandler = resultHandler
  32. engine.processJobWithRetry(context.TODO(), job)
  33. So(evalHandler.EvalSucceed, ShouldEqual, true)
  34. So(resultHandler.ResultHandleSucceed, ShouldEqual, true)
  35. // initialize for other tests.
  36. setting.AlertingEvaluationTimeout = 2 * time.Second
  37. engine.resultHandler = &FakeResultHandler{}
  38. })
  39. })
  40. })
  41. }
  42. type FakeCommonTimeoutHandler struct {
  43. TransportTimeoutDuration time.Duration
  44. ServerBusySleepDuration time.Duration
  45. EvalSucceed bool
  46. ResultHandleSucceed bool
  47. }
  48. func NewFakeCommonTimeoutHandler(transportTimeoutDuration time.Duration, serverBusySleepDuration time.Duration) *FakeCommonTimeoutHandler {
  49. return &FakeCommonTimeoutHandler{
  50. TransportTimeoutDuration: transportTimeoutDuration,
  51. ServerBusySleepDuration: serverBusySleepDuration,
  52. EvalSucceed: false,
  53. ResultHandleSucceed: false,
  54. }
  55. }
  56. func (handler *FakeCommonTimeoutHandler) Eval(evalContext *EvalContext) {
  57. // 1. prepare mock server
  58. path := "/evaltimeout"
  59. srv := runBusyServer(path, handler.ServerBusySleepDuration)
  60. defer srv.Close()
  61. // 2. send requests
  62. url := srv.URL + path
  63. res, err := sendRequest(evalContext.Ctx, url, handler.TransportTimeoutDuration)
  64. if res != nil {
  65. defer res.Body.Close()
  66. }
  67. if err != nil {
  68. evalContext.Error = errors.New("Fake evaluation timeout test failure")
  69. return
  70. }
  71. if res.StatusCode == 200 {
  72. handler.EvalSucceed = true
  73. }
  74. evalContext.Error = errors.New("Fake evaluation timeout test failure; wrong response")
  75. }
  76. func (handler *FakeCommonTimeoutHandler) handle(evalContext *EvalContext) error {
  77. // 1. prepare mock server
  78. path := "/resulthandle"
  79. srv := runBusyServer(path, handler.ServerBusySleepDuration)
  80. defer srv.Close()
  81. // 2. send requests
  82. url := srv.URL + path
  83. res, err := sendRequest(evalContext.Ctx, url, handler.TransportTimeoutDuration)
  84. if res != nil {
  85. defer res.Body.Close()
  86. }
  87. if err != nil {
  88. evalContext.Error = errors.New("Fake result handle timeout test failure")
  89. return evalContext.Error
  90. }
  91. if res.StatusCode == 200 {
  92. handler.ResultHandleSucceed = true
  93. return nil
  94. }
  95. evalContext.Error = errors.New("Fake result handle timeout test failure; wrong response")
  96. return evalContext.Error
  97. }
  98. func runBusyServer(path string, serverBusySleepDuration time.Duration) *httptest.Server {
  99. mux := http.NewServeMux()
  100. server := httptest.NewServer(mux)
  101. mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
  102. time.Sleep(serverBusySleepDuration)
  103. })
  104. return server
  105. }
  106. func sendRequest(context context.Context, url string, transportTimeoutInterval time.Duration) (resp *http.Response, err error) {
  107. req, err := http.NewRequest("GET", url, nil)
  108. if err != nil {
  109. return nil, err
  110. }
  111. req = req.WithContext(context)
  112. transport := http.Transport{
  113. Dial: (&net.Dialer{
  114. Timeout: transportTimeoutInterval,
  115. KeepAlive: transportTimeoutInterval,
  116. }).Dial,
  117. }
  118. client := http.Client{
  119. Transport: &transport,
  120. }
  121. return client.Do(req)
  122. }