-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkq_test.go
More file actions
53 lines (45 loc) · 1.29 KB
/
kq_test.go
File metadata and controls
53 lines (45 loc) · 1.29 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
package queue
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/IBM/sarama"
)
func Test(t *testing.T) {
// TODO
config := sarama.NewConfig()
config.Producer.Retry.Max = 3
config.Producer.Return.Successes = true
config.Producer.Return.Errors = true
// config.Producer.RequiredAcks = sarama.WaitForLocal // Only wait for the leader to ack
// config.Producer.Compression = sarama.CompressionSnappy // Compress messages
config.Producer.Flush.Frequency = 5 * time.Millisecond // Flush batches every 500ms
config.Consumer.Offsets.Initial = sarama.OffsetNewest
client, err := sarama.NewClient([]string{"127.0.0.1:9092"}, config)
if err != nil {
t.Error(err)
}
topicName := "belike"
kq := NewKfQueue(client, WithBatchSize(5))
kq.AddTopic(topicName, func(topicName string, batch batchEntry) error {
for _, e := range batch.entries {
var i int
if err := json.Unmarshal(e.Data.([]byte), &i); err != nil {
t.Error(err)
}
fmt.Printf("%s: %d ", e.Key, i)
}
fmt.Println("batch: ", len(batch.entries))
return nil
}, stdHandleErr)
go kq.Consume(topicName)
for i := 0; i < 4; i++ {
if err := kq.Produce(topicName, Entry{Key: "kmsg", Data: time.Now().Nanosecond()}); err != nil {
t.Error(err)
}
}
timer := time.NewTimer(time.Second * 3)
<-timer.C
kq.Wait()
}