-
Notifications
You must be signed in to change notification settings - Fork 5
/
pub_sub_test.go
66 lines (63 loc) · 1.4 KB
/
pub_sub_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package rmq
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"strconv"
"sync"
"testing"
"time"
)
func TestNewPubSubMQ(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
q := NewPubSubMQ(client)
topic := "test"
count := 10
var wg sync.WaitGroup
wg.Add(count)
go q.Consume(context.Background(), topic, 0, func(msg *Msg) error {
fmt.Printf("consume partiton0: %+v\n", string(msg.Body))
wg.Done()
return nil
})
go q.Consume(context.Background(), topic, 1, func(msg *Msg) error {
fmt.Printf("consume partiton1: %+v\n", string(msg.Body))
wg.Done()
return nil
})
time.Sleep(time.Second)
for i := 0; i < count; i++ {
q.SendMsg(context.Background(), &Msg{
Topic: topic,
Body: []byte(topic + strconv.Itoa(i)),
Partition: i % 2,
})
}
wg.Wait()
}
func TestPubSubMQConsumeMultiPartitions(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
q := NewPubSubMQ(client)
topic := "test"
count := 10
var wg sync.WaitGroup
wg.Add(count)
go q.ConsumeMultiPartitions(context.Background(), topic, []int{0, 1}, func(msg *Msg) error {
fmt.Printf("consume partiton: %+v\n", msg)
wg.Done()
return nil
})
time.Sleep(time.Second)
for i := 0; i < count; i++ {
q.SendMsg(context.Background(), &Msg{
Topic: topic,
Body: []byte(topic + strconv.Itoa(i)),
Partition: i % 2,
})
}
wg.Wait()
}