example_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package timer_test
  2. import (
  3. "fmt"
  4. "leafstalk/module/timer"
  5. "time"
  6. )
  7. func ExampleTimer() {
  8. d := timer.NewDispatcher(10)
  9. // timer 1
  10. d.AfterFunc(1, func() {
  11. fmt.Println("My name is Leaf")
  12. })
  13. // timer 2
  14. t := d.AfterFunc(1, func() {
  15. fmt.Println("will not print")
  16. })
  17. t.Stop()
  18. // dispatch
  19. (<-d.ChanTimer).Cb()
  20. // Output:
  21. // My name is Leaf
  22. }
  23. func ExampleCronExpr() {
  24. cronExpr, err := timer.NewCronExpr("0 * * * *")
  25. if err != nil {
  26. return
  27. }
  28. fmt.Println(cronExpr.Next(time.Date(
  29. 2000, 1, 1,
  30. 20, 10, 5,
  31. 0, time.UTC,
  32. )))
  33. // Output:
  34. // 2000-01-01 21:00:00 +0000 UTC
  35. }
  36. func ExampleCron() {
  37. d := timer.NewDispatcher(10)
  38. // cron expr
  39. cronExpr, err := timer.NewCronExpr("* * * * * *")
  40. if err != nil {
  41. return
  42. }
  43. // cron
  44. var c *timer.Cron
  45. c = d.CronFunc(cronExpr, func() {
  46. fmt.Println("My name is Leaf")
  47. c.Stop()
  48. })
  49. // d.CronFunc(cronExpr, func() {
  50. // fmt.Println("My name is Leaf")
  51. // })
  52. // dispatch
  53. // for {
  54. // (<-d.ChanTimer).Cb()
  55. // }
  56. (<-d.ChanTimer).Cb()
  57. // Output:
  58. // My name is Leaf
  59. }