worker_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package integration_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "mime/multipart"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/qor5/admin/worker"
  12. integration "github.com/qor5/admin/worker/integration_test"
  13. )
  14. func TestJobSelectList(t *testing.T) {
  15. r := httptest.NewRequest(http.MethodPost, "/workers?__execute_event__=presets_New", nil)
  16. w := httptest.NewRecorder()
  17. pb.ServeHTTP(w, r)
  18. body := w.Body.String()
  19. expectItems := []string{"noArgJob", "progressTextJob", "argJob", "longRunningJob", "scheduleJob", "errorJob", "panicJob"}
  20. for _, ei := range expectItems {
  21. if ok := strings.Contains(body, ei); !ok {
  22. t.Fatalf("want item %q, but not found\n", ei)
  23. }
  24. }
  25. }
  26. func TestJobForm(t *testing.T) {
  27. r := httptest.NewRequest(http.MethodPost, "/workers?__execute_event__=worker_selectJob&jobName=argJob", nil)
  28. w := httptest.NewRecorder()
  29. pb.ServeHTTP(w, r)
  30. body := w.Body.String()
  31. expectItems := []string{"F1", "F2", "F3"}
  32. for _, ei := range expectItems {
  33. if ok := strings.Contains(body, ei); !ok {
  34. t.Fatalf("want item %q, but not found\n", ei)
  35. }
  36. }
  37. }
  38. func TestJobLog(t *testing.T) {
  39. cleanData()
  40. mustCreateJob(map[string]string{
  41. "Job": "noArgJob",
  42. })
  43. integration.ConsumeQueItem()
  44. j := mustGetFirstJob()
  45. r := httptest.NewRequest(http.MethodPost, fmt.Sprintf(`/workers?__execute_event__=worker_updateJobProgressing&job=noArgJob&jobID=%d`, j.ID), nil)
  46. w := httptest.NewRecorder()
  47. pb.ServeHTTP(w, r)
  48. body := w.Body.String()
  49. expectItems := []string{"hoho1", "hoho2", "hoho3"}
  50. for _, ei := range expectItems {
  51. if ok := strings.Contains(body, ei); !ok {
  52. t.Fatalf("want item %q, but not found\n", ei)
  53. }
  54. }
  55. cleanData()
  56. mustCreateJob(map[string]string{
  57. "Job": "argJob",
  58. "F1": "f1val",
  59. "F2": "2",
  60. "F3": "true",
  61. })
  62. integration.ConsumeQueItem()
  63. j = mustGetFirstJob()
  64. r = httptest.NewRequest(http.MethodPost, fmt.Sprintf(`/workers?__execute_event__=worker_updateJobProgressing&job=argJob&jobID=%d`, j.ID), nil)
  65. w = httptest.NewRecorder()
  66. pb.ServeHTTP(w, r)
  67. body = w.Body.String()
  68. expectItems = []string{"F1", "f1val", "F2:2", "F3:true"}
  69. for _, ei := range expectItems {
  70. if ok := strings.Contains(body, ei); !ok {
  71. t.Fatalf("want item %q, but not found\n", ei)
  72. }
  73. }
  74. }
  75. func TestJobActions(t *testing.T) {
  76. // abort
  77. {
  78. cleanData()
  79. mustCreateJob(map[string]string{
  80. "Job": "longRunningJob",
  81. })
  82. j := mustGetFirstJob()
  83. go integration.ConsumeQueItem()
  84. time.Sleep(time.Second)
  85. r := httptest.NewRequest(http.MethodPost, fmt.Sprintf(`/workers/%d?__execute_event__=worker_abortJob&job=longRunningJob&jobID=%d`, j.ID, j.ID), nil)
  86. w := httptest.NewRecorder()
  87. pb.ServeHTTP(w, r)
  88. time.Sleep(2 * time.Second)
  89. r = httptest.NewRequest(http.MethodPost, fmt.Sprintf(`/workers?__execute_event__=worker_updateJobProgressing&job=longRunningJob&jobID=%d`, j.ID), nil)
  90. w = httptest.NewRecorder()
  91. pb.ServeHTTP(w, r)
  92. body := w.Body.String()
  93. expectItems := []string{"Killed", "job aborted"}
  94. for _, ei := range expectItems {
  95. if ok := strings.Contains(body, ei); !ok {
  96. t.Fatalf("want item %q, but not found\n", ei)
  97. }
  98. }
  99. }
  100. // rerun
  101. {
  102. cleanData()
  103. mustCreateJob(map[string]string{
  104. "Job": "noArgJob",
  105. })
  106. integration.ConsumeQueItem()
  107. j := mustGetFirstJob()
  108. if j.Status != worker.JobStatusDone {
  109. t.Fatalf("want status %q, got %q", worker.JobStatusDone, j.Status)
  110. }
  111. r := httptest.NewRequest(http.MethodPost, fmt.Sprintf(`/workers/%d?__execute_event__=worker_rerunJob&job=noArgJob&jobID=%d`, j.ID, j.ID), nil)
  112. w := httptest.NewRecorder()
  113. pb.ServeHTTP(w, r)
  114. j = mustGetFirstJob()
  115. if j.Status != worker.JobStatusNew {
  116. t.Fatalf("want status %q, got %q", worker.JobStatusNew, j.Status)
  117. }
  118. }
  119. // update
  120. {
  121. cleanData()
  122. mustCreateJob(map[string]string{
  123. "Job": "scheduleJob",
  124. "ScheduleTime": time.Now().Add(time.Hour).Local().Format(`2006-01-02 15:04`),
  125. })
  126. j := mustGetFirstJob()
  127. rBody := bytes.NewBuffer(nil)
  128. mw := multipart.NewWriter(rBody)
  129. st := time.Now().Add(2 * time.Hour).Local().Format(`2006-01-02 15:04`)
  130. mw.WriteField("ScheduleTime", st)
  131. mw.Close()
  132. r := httptest.NewRequest(http.MethodPost, fmt.Sprintf(`/workers/%d?__execute_event__=worker_updateJob&job=scheduleJob&jobID=%d`, j.ID, j.ID), rBody)
  133. r.Header.Add("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%s", mw.Boundary()))
  134. w := httptest.NewRecorder()
  135. pb.ServeHTTP(w, r)
  136. r = httptest.NewRequest(http.MethodGet, fmt.Sprintf(`/workers/%d`, j.ID), nil)
  137. w = httptest.NewRecorder()
  138. pb.ServeHTTP(w, r)
  139. if !strings.Contains(w.Body.String(), st) {
  140. t.Fatalf("want updated schedule time %q", st)
  141. }
  142. }
  143. }