-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer_test.go
97 lines (94 loc) · 2.64 KB
/
producer_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
package dynamomq_test
import (
"context"
"errors"
"reflect"
"testing"
"github.com/vvatanabe/dynamomq"
"github.com/vvatanabe/dynamomq/internal/mock"
"github.com/vvatanabe/dynamomq/internal/test"
)
func TestProducerProduce(t *testing.T) {
type args[T any] struct {
params *dynamomq.ProduceInput[T]
}
type testCase[T any] struct {
name string
c *dynamomq.Producer[T]
args args[T]
want *dynamomq.ProduceOutput[T]
wantErr bool
}
defaultMockClient := &mock.Client[test.MessageData]{
SendMessageFunc: func(ctx context.Context,
params *dynamomq.SendMessageInput[test.MessageData]) (*dynamomq.SendMessageOutput[test.MessageData], error) {
return &dynamomq.SendMessageOutput[test.MessageData]{
SentMessage: &dynamomq.Message[test.MessageData]{
ID: params.ID,
Data: params.Data,
},
}, nil
},
}
defaultTestProducer := dynamomq.NewProducer[test.MessageData](defaultMockClient, dynamomq.WithIDGenerator(func() string {
return "A-101"
}))
tests := []testCase[test.MessageData]{
{
name: "should success to produce a message",
c: defaultTestProducer,
args: args[test.MessageData]{
params: &dynamomq.ProduceInput[test.MessageData]{
Data: test.NewMessageData("A-101"),
DelaySeconds: 10,
},
},
want: &dynamomq.ProduceOutput[test.MessageData]{
Message: &dynamomq.Message[test.MessageData]{
ID: "A-101",
Data: test.NewMessageData("A-101"),
},
},
wantErr: false,
},
{
name: "should success to produce a message when params is nil",
c: defaultTestProducer,
args: args[test.MessageData]{
params: nil,
},
want: &dynamomq.ProduceOutput[test.MessageData]{
Message: &dynamomq.Message[test.MessageData]{
ID: "A-101",
},
},
wantErr: false,
},
{
name: "should fail to produce a message when client.SendMessage returns error",
c: dynamomq.NewProducer[test.MessageData](&mock.Client[test.MessageData]{
SendMessageFunc: func(ctx context.Context,
params *dynamomq.SendMessageInput[test.MessageData]) (*dynamomq.SendMessageOutput[test.MessageData], error) {
return nil, errors.New("for error case")
},
}),
args: args[test.MessageData]{
params: nil,
},
want: &dynamomq.ProduceOutput[test.MessageData]{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.c.Produce(context.Background(), tt.args.params)
if (err != nil) != tt.wantErr {
t.Errorf("Produce() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Produce() got = %v, want %v", got, tt.want)
}
})
}
}