-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub_test.go
52 lines (40 loc) · 1.06 KB
/
pubsub_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
package gopubsub_test
import (
"context"
"testing"
_ "github.com/mshogin/gopubsub"
"github.com/stretchr/testify/require"
"gocloud.dev/pubsub"
)
type (
sender interface {
Send(ctx context.Context, msg *pubsub.Message) error
}
receiver interface {
Receive(context.Context) (*pubsub.Message, error)
}
)
func TestPubsub(t *testing.T) {
s := require.New(t)
topicName := "testtopic"
top, err := pubsub.OpenTopic(context.Background(), "local://"+topicName)
s.NoError(err)
s.NotNil(top)
s.NoError(top.Send(context.Background(), &pubsub.Message{Body: []byte("test message")}))
sub, err := pubsub.OpenSubscription(context.Background(), "local://"+topicName)
s.NoError(err)
s.NotNil(sub)
cnt := 3
for i := 0; i < cnt; i++ {
s.NoError(top.Send(context.Background(), &pubsub.Message{Body: []byte("test message")}))
}
for i := 0; i < cnt; i++ {
msg, err := sub.Receive(context.Background())
s.NoError(err)
s.NotNil(msg)
s.Equal([]byte("test message"), msg.Body)
}
// no messages and timeout
_, err = sub.Receive(context.Background())
s.Error(err)
}