-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
50 lines (40 loc) · 760 Bytes
/
example_test.go
File metadata and controls
50 lines (40 loc) · 760 Bytes
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
package syncbuffer_test
import (
"log"
"sync"
"time"
"github.com/ny0m/syncbuffer"
)
func Example() {
// New buffer with space for 2 items that adds messages every millisecond.
sb := syncbuffer.NewSyncBuffer(time.Second/2, 2)
var wg sync.WaitGroup
wg.Add(1)
// Start streaming somewhere.
go func() {
defer wg.Done()
s := syncbuffer.NewStreamer(sb)
for p := range s.Stream() {
log.Println(p)
}
}()
for i := 0; i < 10; i++ {
// Add will block until a millisecond has passed.
sb.Add([]byte{byte(i)})
}
// Close the buffer and stop its streamers.
sb.Close()
// Ensure that the program waits for the streamer to finish.
wg.Wait()
// Output:
// [0]
// [1]
// [2]
// [3]
// [4]
// [5]
// [6]
// [7]
// [8]
// [9]
}