-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimequeue_example_test.go
More file actions
82 lines (70 loc) · 1.81 KB
/
timequeue_example_test.go
File metadata and controls
82 lines (70 loc) · 1.81 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package timequeue_test
import (
"fmt"
"time"
"github.com/gogolfing/timequeue"
)
func Example() {
tq := timequeue.New()
tq.Start()
//this would normally be a long-running process,
//and not stop at the return of a function call.
defer tq.Stop()
startTime := time.Now()
tq.Push(startTime, "this will be released immediately")
//adding Messages in chronological order.
for i := 1; i <= 4; i++ {
tq.Push(
startTime.Add(time.Duration(i)*time.Second),
fmt.Sprintf("message at second %v", i),
)
}
//adding Messages in reverse chronological order.
for i := 8; i >= 5; i-- {
tq.Push(
startTime.Add(time.Duration(i)*time.Second),
fmt.Sprintf("message at second %v", i),
)
}
//receive all 9 Messages that were pushed.
for i := 0; i < 9; i++ {
message := <-tq.Messages()
fmt.Println(message.Data)
}
fmt.Printf("there are %v messages left in the queue\n", tq.Size())
endTime := time.Now()
if endTime.Sub(startTime) > time.Duration(8)*time.Second {
fmt.Println("releasing all messages took more than 8 seconds")
} else {
fmt.Println("releasing all messages took less than 8 seconds")
}
//Output:
//this will be released immediately
//message at second 1
//message at second 2
//message at second 3
//message at second 4
//message at second 5
//message at second 6
//message at second 7
//message at second 8
//there are 0 messages left in the queue
//releasing all messages took more than 8 seconds
}
func ExampleTimeQueue_PopAllUntil() {
tq := timequeue.New()
now := time.Now()
for i := 0; i < 4; i++ {
tq.Push(now.Add(time.Duration(i)*time.Second), i)
}
tq.PopAllUntil(now.Add(time.Duration(2)*time.Second), true)
for i := 0; i < 2; i++ {
message := <-tq.Messages()
fmt.Println(message.Data)
}
fmt.Println("messages left:", tq.Size())
//Output:
//0
//1
//messages left: 2
}