-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsubscribe_test.go
105 lines (83 loc) · 1.94 KB
/
subscribe_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
package pubsub_test
import (
"context"
"testing"
"github.com/lileio/pubsub/v2"
"github.com/lileio/pubsub/v2/providers/memory"
"github.com/lileio/pubsub/v2/test"
"github.com/stretchr/testify/assert"
)
type TestSubscriber struct {
JSON bool
T *testing.T
}
func (ts *TestSubscriber) DoSomething(ctx context.Context, t *test.Account, msg *pubsub.Msg) error {
if ts.T != nil {
assert.True(ts.T, len(msg.Data) > 0)
}
return nil
}
func (ts *TestSubscriber) Setup(c *pubsub.Client) {
c.On(pubsub.HandlerOptions{
Topic: "test_topic",
Name: "do_something",
Handler: ts.DoSomething,
JSON: ts.JSON,
})
}
func TestProtoSubscribers(t *testing.T) {
m := &memory.MemoryProvider{}
c := &pubsub.Client{
ServiceName: "test",
Provider: m,
}
ps := test.Account{Name: "test"}
for i := 0; i < 100; i++ {
err := c.Publish(context.Background(), "test_topic", &ps, false)
assert.Nil(t, err)
}
ts := TestSubscriber{T: t}
ts.Setup(c)
}
func TestJSONSubscribers(t *testing.T) {
m := &memory.MemoryProvider{}
c := &pubsub.Client{
ServiceName: "test",
Provider: m,
}
ps := test.Account{Name: "test"}
for i := 0; i < 100; i++ {
err := c.Publish(context.Background(), "test_topic", &ps, true)
assert.Nil(t, err)
}
ts := TestSubscriber{T: t, JSON: true}
ts.Setup(c)
}
func BenchmarkProtoSubscribers(b *testing.B) {
m := &memory.MemoryProvider{}
c := &pubsub.Client{
ServiceName: "test",
Provider: m,
}
ps := test.Account{Name: "test"}
for i := 0; i < b.N; i++ {
c.Publish(context.Background(), "test_topic", &ps, false)
}
b.ResetTimer()
ts := TestSubscriber{}
ts.Setup(c)
}
func BenchmarkJSONSubscribers(b *testing.B) {
m := &memory.MemoryProvider{}
c := &pubsub.Client{
ServiceName: "test",
Provider: m,
}
ps := test.Account{Name: "test"}
for i := 0; i < b.N; i++ {
c.Publish(context.Background(), "test_topic", &ps, true)
}
b.ResetTimer()
ts := TestSubscriber{JSON: true}
ts.Setup(c)
}