-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterceptor_test.go
119 lines (101 loc) · 3.07 KB
/
interceptor_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
package subee
import (
"bytes"
"context"
"testing"
"github.com/pkg/errors"
)
type chainContextTestKey struct{}
func writeBuffer(ctx context.Context, tag string) {
ctx.Value(chainContextTestKey{}).(*bytes.Buffer).Write([]byte(tag))
}
func tagBatchConsumerInterceptor(tag string) BatchConsumerInterceptor {
return func(consumer BatchConsumer) BatchConsumer {
return BatchConsumerFunc(func(ctx context.Context, msgs []Message) error {
writeBuffer(ctx, tag)
return errors.WithStack(consumer.BatchConsume(ctx, msgs))
})
}
}
func tagBatchConsumer(tag string) BatchConsumer {
return BatchConsumerFunc(func(ctx context.Context, msgs []Message) error {
writeBuffer(ctx, tag)
return nil
})
}
func tagConsumerInterceptor(tag string) ConsumerInterceptor {
return func(consumer Consumer) Consumer {
return ConsumerFunc(func(ctx context.Context, msg Message) error {
writeBuffer(ctx, tag)
return errors.WithStack(consumer.Consume(ctx, msg))
})
}
}
func tagConsumer(tag string) Consumer {
return ConsumerFunc(func(ctx context.Context, msg Message) error {
writeBuffer(ctx, tag)
return nil
})
}
func TestChainConsumerInterceptors(t *testing.T) {
tests := []struct {
consumer Consumer
interceptors []ConsumerInterceptor
want string
}{
{
consumer: tagConsumer("consumer-A\n"),
interceptors: []ConsumerInterceptor{
tagConsumerInterceptor("intr-A\n"),
tagConsumerInterceptor("intr-B\n"),
tagConsumerInterceptor("intr-C\n"),
},
want: "intr-A\nintr-B\nintr-C\nconsumer-A\n",
},
{
consumer: tagConsumer("consumer-A\n"),
interceptors: []ConsumerInterceptor{},
want: "consumer-A\n",
},
}
for _, test := range tests {
chain := chainConsumerInterceptors(test.consumer, test.interceptors...)
ctx := context.WithValue(context.Background(), chainContextTestKey{}, new(bytes.Buffer))
chain.Consume(ctx, nil)
got := ctx.Value(chainContextTestKey{}).(*bytes.Buffer).String()
if test.want != got {
t.Errorf("chainConsumerInterceptors output is wrong. want: %v, got: %v", test.want, got)
}
}
}
func TestChainBatchConsumerInterceptors(t *testing.T) {
tests := []struct {
consumer BatchConsumer
interceptors []BatchConsumerInterceptor
want string
}{
{
consumer: tagBatchConsumer("consumer-A\n"),
interceptors: []BatchConsumerInterceptor{
tagBatchConsumerInterceptor("intr-A\n"),
tagBatchConsumerInterceptor("intr-B\n"),
tagBatchConsumerInterceptor("intr-C\n"),
},
want: "intr-A\nintr-B\nintr-C\nconsumer-A\n",
},
{
consumer: tagBatchConsumer("consumer-A\n"),
interceptors: []BatchConsumerInterceptor{},
want: "consumer-A\n",
},
}
for _, test := range tests {
chain := chainBatchConsumerInterceptors(test.consumer, test.interceptors...)
ctx := context.WithValue(context.Background(), chainContextTestKey{}, new(bytes.Buffer))
chain.BatchConsume(ctx, nil)
got := ctx.Value(chainContextTestKey{}).(*bytes.Buffer).String()
if test.want != got {
t.Errorf("chainBatchConsumerInterceptors output is wrong. want: %v, got: %v", test.want, got)
}
}
}