-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_test.go
50 lines (46 loc) · 1.09 KB
/
consumer_test.go
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 rediqueue
import (
"bytes"
"sync"
"testing"
"time"
"github.com/go-redis/redis"
)
var messagePool = sync.Pool{
New: func() interface{} {
return &ConsumerMessage{}
},
}
func TestConsumer(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: ser,
Password: option.Password, // no password set
DB: option.DB, // use default DB
PoolSize: option.PoolSize,
IdleTimeout: option.IdleTimeout,
})
config := NewConfig()
config.Consumer.NewValueFunc = func() *ConsumerMessage {
buf := bufferPool.Get().(*bytes.Buffer)
msg := messagePool.Get().(*ConsumerMessage)
msg.Value = buf
return msg
}
consumer := NewConsumer(client, config)
partitions, _ := consumer.Partitions("rediqueue")
for _, partition := range partitions {
go func(partition int64) {
p, _ := consumer.ConsumerPartition("rediqueue", partition)
for {
msg := <-p.Messages()
msg.Value.Reset()
bufferPool.Put(msg.Value)
msg.Key = msg.Key[:0]
msg.Topic = ""
msg.Partition = -1
messagePool.Put(msg)
}
}(partition)
}
time.Sleep(1000 * time.Second)
}