Skip to content

Commit

Permalink
test: add example for godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
woorui committed Oct 21, 2022
1 parent e5357a0 commit 7cbff3f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,22 @@ func main() {
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()

// 1. flush at threshold
buf.Write("a", "b", "c", "d", "e", "f")
// Output
// print: [a b c d e f]

// 2. time to flush automatically
buf.Write("aaaaa")
buf.Write("bbbbb")
buf.Write("ccccc", "ddddd")
time.Sleep(5 * time.Second)
// Output
// print: [aaaaa bbbbb ccccc ddddd]

// 3. flush manually and write call `WriteWithContext`
buf.WriteWithContext(context.Background(), "eeeee", "fffff")
buf.Flush()
// Output
// print: [eeeee fffff]
}

```
Expand Down
12 changes: 0 additions & 12 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,3 @@ func TestFlushFunc(t *testing.T) {
func TestDefaultErrHandler(t *testing.T) {
DefaultErrHandler(errors.New("mock_error"), []string{"A", "B", "C"})
}

// stringInclude return if arr includes v
func stringInclude(arr []string, v string) bool {
b := false
for _, item := range arr {
if item == v {
b = true
break
}
}
return b
}
38 changes: 38 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 {
fmt.Printf("print: %v \n", strs)
return nil
}

func Example() {
// can also call buffer.FlushFunc` to adapt a function to Flusher
buf := buffer.New[string](&pp{}, buffer.Option[string]{
Threshold: 5,
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:
// print: [a b c d e f]
}

0 comments on commit 7cbff3f

Please sign in to comment.