forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub.go
43 lines (37 loc) · 1.21 KB
/
pub.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
package pubsubtest
import (
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
)
type (
// TestPublisher is a simple implementation of pubsub.Publisher meant to
// help mock out any implementations.
TestPublisher struct {
// Published will contain a list of all messages that have been published.
Published []TestPublishMsg
// GivenError will be returned by the TestPublisher on publish.
// Good for testing error scenarios.
GivenError error
// FoundError will contain any errors encountered while marshalling
// the protobuf struct.
FoundError error
}
// TestPublishMsg is a test publish message.
TestPublishMsg struct {
// Key represents the message key.
Key string
// Body represents the message body.
Body []byte
}
)
// Publish publishes the message, delegating to PublishRaw.
func (t *TestPublisher) Publish(ctx context.Context, key string, msg proto.Message) error {
data, err := proto.Marshal(msg)
t.FoundError = err
return t.PublishRaw(ctx, key, data)
}
// PublishRaw publishes the raw message byte slice.
func (t *TestPublisher) PublishRaw(_ context.Context, key string, msg []byte) error {
t.Published = append(t.Published, TestPublishMsg{key, msg})
return t.GivenError
}