example_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package timerqueue_test
  2. import (
  3. "fmt"
  4. "time"
  5. "go-common/app/admin/main/up/util/timerqueue"
  6. )
  7. type event int
  8. func (e event) OnTimer(t time.Time) {
  9. fmt.Printf(" Event %d executed at %v\n", int(e), t)
  10. }
  11. // Schedule several events with a timerqueue, and dispatch
  12. // them by calling Advance.
  13. func ExampleQueue() {
  14. queue := timerqueue.New()
  15. // Schedule an event each day from Jan 1 to Jan 7, 2015.
  16. tm := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)
  17. for i := 1; i <= 7; i++ {
  18. queue.Schedule(event(i), tm)
  19. tm = tm.Add(24 * time.Hour)
  20. }
  21. fmt.Println("Advancing to Jan 4...")
  22. queue.Advance(time.Date(2015, 1, 4, 0, 0, 0, 0, time.UTC))
  23. fmt.Println("Advancing to Jan 10...")
  24. queue.Advance(time.Date(2015, 1, 10, 0, 0, 0, 0, time.UTC))
  25. // Output:
  26. // Advancing to Jan 4...
  27. // Event 1 executed at 2015-01-01 00:00:00 +0000 UTC
  28. // Event 2 executed at 2015-01-02 00:00:00 +0000 UTC
  29. // Event 3 executed at 2015-01-03 00:00:00 +0000 UTC
  30. // Event 4 executed at 2015-01-04 00:00:00 +0000 UTC
  31. // Advancing to Jan 10...
  32. // Event 5 executed at 2015-01-05 00:00:00 +0000 UTC
  33. // Event 6 executed at 2015-01-06 00:00:00 +0000 UTC
  34. // Event 7 executed at 2015-01-07 00:00:00 +0000 UTC
  35. }