-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher_test.go
112 lines (95 loc) · 3.31 KB
/
publisher_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
package pubsub_test
import (
"context"
"errors"
"reflect"
"testing"
"github.com/hmoragrega/pubsub"
"github.com/hmoragrega/pubsub/marshaller"
"github.com/hmoragrega/pubsub/pubsubtest/stubs"
)
func TestPublisher_PublishMarshallFailure(t *testing.T) {
fakeErr := errors.New("foo")
m := &stubs.MarshallerStub{
MarshalFunc: func(data interface{}) ([]byte, string, error) {
if data.(string) != "data" {
t.Fatalf("unexpected data to marshall; got %+v", data)
}
return nil, "", fakeErr
},
}
p := pubsub.NewPublisher(nil, m)
err := p.Publish(context.Background(), "foo", &pubsub.Message{Data: "data"})
if !errors.Is(err, fakeErr) {
t.Fatalf("unexpected error result; got %v", err)
}
}
func TestPublisher_PublishHandlerSendMessages(t *testing.T) {
var (
publishedMessages []*pubsub.Envelope
publishedTopic string
dummyErr = errors.New("some error")
)
p := pubsub.NewPublisher(pubsub.EnvelopePublisherFunc(func(ctx context.Context, topic string, envelopes ...*pubsub.Envelope) error {
publishedTopic = topic
publishedMessages = envelopes
return dummyErr
}), &marshaller.ByteMarshaller{})
topicToSend := "foo"
messagesToSend := []*pubsub.Message{
{Data: "foo"},
{Data: "bar"},
}
err := p.Handler(topicToSend, pubsub.PublisherHandlerFunc(func(ctx context.Context, message *pubsub.Message) ([]*pubsub.Message, error) {
return messagesToSend, nil
})).HandleMessage(context.Background(), &pubsub.Message{})
if !errors.Is(err, dummyErr) {
t.Fatalf("expected error; got %v, want %v", err, dummyErr)
}
if publishedTopic != topicToSend {
t.Fatalf("unpected pubslihing name; got %v, want %v", publishedTopic, topicToSend)
}
if reflect.DeepEqual(messagesToSend, publishedMessages) {
t.Fatalf("expected error; got %v, want %v", err, dummyErr)
}
}
func TestPublisher_PublishHandlerDoNotSend(t *testing.T) {
p := pubsub.NewPublisher(pubsub.EnvelopePublisherFunc(func(ctx context.Context, topic string, envelopes ...*pubsub.Envelope) error {
t.Error("publisher call was not expected")
return nil
}), &marshaller.ByteMarshaller{})
dummyErr := errors.New("some error")
err := p.Handler("foo", pubsub.PublisherHandlerFunc(func(ctx context.Context, message *pubsub.Message) ([]*pubsub.Message, error) {
return nil, dummyErr
})).HandleMessage(context.Background(), &pubsub.Message{})
if !errors.Is(err, dummyErr) {
t.Fatalf("expected error; got %v, want %v", err, dummyErr)
}
}
func TestPublisher_Wrapper(t *testing.T) {
injectedValue := "some-value"
attributeKey := "some-key"
ctxKey := "some-ctx-key"
innerPublisher := pubsub.PublisherFunc(func(ctx context.Context, topic string, envelopes ...*pubsub.Message) error {
for _, e := range envelopes {
if e.Attributes[attributeKey] != injectedValue {
t.Fatalf("message does not have the injected attribute; got %+v", e.Attributes)
}
if e.Name != topic {
t.Fatalf("name should be used as event name; got %+v", e.Name)
}
}
return nil
})
p := pubsub.WrapPublisher(innerPublisher,
pubsub.TopicAsEventName(),
pubsub.CtxAttributeInjector(attributeKey, func(ctx context.Context) string {
return ctx.Value(ctxKey).(string)
}),
)
ctx := context.WithValue(context.Background(), ctxKey, injectedValue)
err := p.Publish(ctx, "foo-name", &pubsub.Message{})
if err != nil {
t.Fatal("unexpected error publishing", err)
}
}