-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic_test.go
126 lines (114 loc) · 2.81 KB
/
basic_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package redispubsub_test
import (
"bytes"
"context"
"testing"
"time"
_ "github.com/covrom/redispubsub"
"gocloud.dev/pubsub"
)
func TestBasicUsage(t *testing.T) {
ctx := context.Background()
orig1 := &pubsub.Message{
Body: []byte("Message #1"),
// Metadata is optional and can be nil.
Metadata: map[string]string{
// These are examples of metadata.
// There is nothing special about the key names.
"language": "en",
"importance": "high",
},
}
orig2 := &pubsub.Message{
Body: []byte("Message #2"),
// Metadata is optional and can be nil.
Metadata: map[string]string{
// These are examples of metadata.
// There is nothing special about the key names.
"language": "en",
},
}
// send before consumer attach
pubTest(ctx, orig1, t)
time.Sleep(100 * time.Millisecond)
pubTest(ctx, orig2, t)
time.Sleep(100 * time.Millisecond)
pubTest(ctx, orig1, t)
time.Sleep(100 * time.Millisecond)
// attach consumer and create group if needed
subTest(ctx, orig1, t)
time.Sleep(100 * time.Millisecond)
subTest(ctx, orig2, t)
time.Sleep(100 * time.Millisecond)
subTest(ctx, orig1, t)
time.Sleep(100 * time.Millisecond)
res, err := redisCli.XPending(ctx, "topics/1", "group1").Result()
if res.Count != 0 {
t.Error(res.Count, err)
}
}
func pubTest(ctx context.Context, orig *pubsub.Message, t *testing.T) {
topic, err := pubsub.OpenTopic(ctx, "redis://topics/1")
if err != nil {
t.Errorf("could not open topic: %v", err)
return
}
defer topic.Shutdown(ctx)
err = topic.Send(ctx, orig)
if err != nil {
t.Error(err)
return
}
time.Sleep(100 * time.Millisecond)
err = topic.Send(ctx, orig)
if err != nil {
t.Error(err)
return
}
time.Sleep(100 * time.Millisecond)
err = topic.Send(ctx, orig)
if err != nil {
t.Error(err)
return
}
}
func subTest(ctx context.Context, orig *pubsub.Message, t *testing.T) {
done := make(chan struct{})
go func() {
defer close(done)
subs, err := pubsub.OpenSubscription(ctx, "redis://group1?consumer=cons1&topic=topics/1")
if err != nil {
t.Error(err)
return
}
defer subs.Shutdown(ctx)
for i := 0; i < 4; i++ {
msg, err := subs.Receive(ctx)
if err != nil {
// Errors from Receive indicate that Receive will no longer succeed.
t.Errorf("Receiving message: %v", err)
return
}
// Do work based on the message, for example:
t.Logf("Got message %s: %q\n", msg.LoggableID, msg.Body)
// Emulate not ack message 0
if i > 0 {
// Messages must always be acknowledged with Ack.
msg.Ack()
// wait for Ack asynchronous (see docs for Ack)
time.Sleep(100 * time.Millisecond)
}
if !bytes.Equal(msg.Body, orig.Body) {
t.Error("body not equal")
return
}
for k, v := range msg.Metadata {
if orig.Metadata[k] != v {
t.Error("metadata not equal")
return
}
}
}
}()
<-done
}