-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
75 lines (64 loc) · 1.63 KB
/
main_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
67
68
69
70
71
72
73
74
75
package watermill_redislist
import (
"context"
"log"
"testing"
"time"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/redis/go-redis/v9"
"github.com/githubzhaoqian/watermill-redislist/pkg/redislist"
)
func TestRun(t *testing.T) {
subClient := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
DB: 0,
})
subscriber, err := redislist.NewSubscriber(
redislist.SubscriberConfig{
Client: subClient,
Unmarshaller: redislist.DefaultMarshallerUnmarshaller{},
},
watermill.NewStdLogger(false, false),
)
if err != nil {
panic(err)
}
messages, err := subscriber.Subscribe(context.Background(), "example.list.topic")
if err != nil {
panic(err)
}
go process(messages)
pubClient := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
DB: 0,
})
publisher, err := redislist.NewPublisher(
redislist.PublisherConfig{
Client: pubClient,
Marshaller: redislist.DefaultMarshallerUnmarshaller{},
},
watermill.NewStdLogger(false, false),
)
if err != nil {
panic(err)
}
publishMessages(publisher)
}
func publishMessages(publisher message.Publisher) {
for {
msg := message.NewMessage(watermill.NewUUID(), []byte("Hello, world!"))
if err := publisher.Publish("example.list.topic", msg); err != nil {
panic(err)
}
time.Sleep(time.Second)
}
}
func process(messages <-chan *message.Message) {
for msg := range messages {
log.Printf("received message: %s, payload: %s", msg.UUID, string(msg.Payload))
// we need to Acknowledge that we received and processed the message,
// otherwise, it will be resent over and over again.
msg.Ack()
}
}