-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
61 lines (54 loc) · 1.32 KB
/
example_test.go
File metadata and controls
61 lines (54 loc) · 1.32 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
package otfranz_test
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/DoNewsCode/core"
"github.com/DoNewsCode/core-otfranz"
"github.com/knadh/koanf/providers/confmap"
"github.com/twmb/franz-go/pkg/kgo"
)
func Example() {
if os.Getenv("KAFKA_ADDR") == "" {
fmt.Println("set KAFKA_ADDR to run this example")
return
}
brokers := strings.Split(os.Getenv("KAFKA_ADDR"), ",")
conf := map[string]interface{}{
"log": map[string]interface{}{
"level": "none",
},
"kafka": map[string]interface{}{
"default": map[string]interface{}{
"seed_brokers": brokers,
"default_produce_topic": "example",
"topics": []string{"example"},
"group": "test",
},
},
}
c := core.Default(core.WithConfigStack(confmap.Provider(conf, "."), nil))
c.Provide(otfranz.Providers())
c.Invoke(func(cli *kgo.Client) {
record := &kgo.Record{Value: []byte("bar")}
cli.Produce(context.Background(), record, nil)
})
c.Invoke(func(cli *kgo.Client) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
fetches := cli.PollFetches(ctx)
if errs := fetches.Errors(); len(errs) > 0 {
panic(errs)
}
iter := fetches.RecordIter()
if iter.Done() {
return
}
rec := iter.Next()
fmt.Println(string(rec.Value))
})
// Output:
// bar
}