-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_test.go
More file actions
40 lines (32 loc) · 797 Bytes
/
example_test.go
File metadata and controls
40 lines (32 loc) · 797 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
package buffer_test
import (
"fmt"
"time"
buffer "github.com/woorui/async-buffer"
)
// pp implements Flusher interface
type pp struct{}
func (p *pp) Flush(strs []string) error {
return print(strs)
}
func print(strs []string) error {
for _, s := range strs {
fmt.Printf("%s ", s)
}
return nil
}
func Example() {
// can also call buffer.FlushFunc` to adapt a function to Flusher
buf := buffer.New[string](&pp{}, buffer.Option[string]{
Threshold: 1,
FlushInterval: 3 * time.Second,
WriteTimeout: time.Second,
FlushTimeout: time.Second,
ErrHandler: func(err error, t []string) { fmt.Printf("err: %v, ele: %v", err, t) },
})
// data maybe loss if Close() is not be called
defer buf.Close()
buf.Write("a", "b", "c", "d", "e", "f")
// Output:
// a b c d e f
}