-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_test.go
44 lines (35 loc) · 1.17 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package timerqueue_test
import (
"fmt"
"time"
"github.com/beevik/timerqueue"
)
type event int
func (e event) OnTimer(t time.Time) {
fmt.Printf(" Event %d executed at %v\n", int(e), t)
}
// Schedule several events with a timerqueue, and dispatch
// them by calling Advance.
func ExampleQueue() {
queue := timerqueue.New()
// Schedule an event each day from Jan 1 to Jan 7, 2015.
tm := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)
for i := 1; i <= 7; i++ {
queue.Schedule(event(i), tm)
tm = tm.Add(24 * time.Hour)
}
fmt.Println("Advancing to Jan 4...")
queue.Advance(time.Date(2015, 1, 4, 0, 0, 0, 0, time.UTC))
fmt.Println("Advancing to Jan 10...")
queue.Advance(time.Date(2015, 1, 10, 0, 0, 0, 0, time.UTC))
// Output:
// Advancing to Jan 4...
// Event 1 executed at 2015-01-01 00:00:00 +0000 UTC
// Event 2 executed at 2015-01-02 00:00:00 +0000 UTC
// Event 3 executed at 2015-01-03 00:00:00 +0000 UTC
// Event 4 executed at 2015-01-04 00:00:00 +0000 UTC
// Advancing to Jan 10...
// Event 5 executed at 2015-01-05 00:00:00 +0000 UTC
// Event 6 executed at 2015-01-06 00:00:00 +0000 UTC
// Event 7 executed at 2015-01-07 00:00:00 +0000 UTC
}