You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
955 B

3 years ago
  1. package timer
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. var job = mockJob{}
  9. type mockJob struct{}
  10. func (job mockJob) Run() {
  11. mockFunc()
  12. }
  13. func mockFunc() {
  14. time.Sleep(time.Second)
  15. fmt.Println("1s...")
  16. }
  17. func TestNewTimerTask(t *testing.T) {
  18. tm := NewTimerTask()
  19. _tm := tm.(*timer)
  20. {
  21. _, err := tm.AddTaskByFunc("func", "@every 1s", mockFunc)
  22. assert.Nil(t, err)
  23. _, ok := _tm.taskList["func"]
  24. if !ok {
  25. t.Error("no find func")
  26. }
  27. }
  28. {
  29. _, err := tm.AddTaskByJob("job", "@every 1s", job)
  30. assert.Nil(t, err)
  31. _, ok := _tm.taskList["job"]
  32. if !ok {
  33. t.Error("no find job")
  34. }
  35. }
  36. {
  37. _, ok := tm.FindCron("func")
  38. if !ok {
  39. t.Error("no find func")
  40. }
  41. _, ok = tm.FindCron("job")
  42. if !ok {
  43. t.Error("no find job")
  44. }
  45. _, ok = tm.FindCron("none")
  46. if ok {
  47. t.Error("find none")
  48. }
  49. }
  50. {
  51. tm.Clear("func")
  52. _, ok := tm.FindCron("func")
  53. if ok {
  54. t.Error("find func")
  55. }
  56. }
  57. }