-
Notifications
You must be signed in to change notification settings - Fork 69
/
amqp_test.go
166 lines (157 loc) · 4.13 KB
/
amqp_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package amqp
import (
"context"
"testing"
"github.com/beatlabs/patron/correlation"
"github.com/beatlabs/patron/encoding/json"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/streadway/amqp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var validExch, _ = NewExchange("e", amqp.ExchangeDirect)
func Test_message(t *testing.T) {
b, err := json.Encode("test")
assert.NoError(t, err)
del := &amqp.Delivery{
Body: b,
}
mtr := mocktracer.New()
opentracing.SetGlobalTracer(mtr)
sp := opentracing.StartSpan("test")
ctx := context.Background()
m := message{
ctx: ctx,
del: del,
dec: json.DecodeRaw,
span: sp,
source: "thequeue",
}
assert.Equal(t, ctx, m.Context())
var data string
assert.NoError(t, m.Decode(&data))
assert.Equal(t, "test", data)
assert.Error(t, m.Ack())
assert.Error(t, m.Nack())
assert.Equal(t, "thequeue", m.Source())
assert.Equal(t, []byte(`"test"`), m.Payload())
}
func TestNewExchange(t *testing.T) {
type args struct {
name string
kind string
}
tests := []struct {
name string
args args
wantErr bool
}{
{"success, kind fanout", args{name: "abc", kind: amqp.ExchangeFanout}, false},
{"success, kind headers", args{name: "abc", kind: amqp.ExchangeHeaders}, false},
{"success, kind topic", args{name: "abc", kind: amqp.ExchangeTopic}, false},
{"success, kind direct", args{name: "abc", kind: amqp.ExchangeDirect}, false},
{"fail, empty name", args{name: "", kind: amqp.ExchangeTopic}, true},
{"fail, empty kind", args{name: "abc", kind: ""}, true},
{"fail, invalid kind", args{name: "abc", kind: "def"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exc, err := NewExchange(tt.args.name, tt.args.kind)
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, exc)
} else {
assert.NoError(t, err)
assert.NotNil(t, exc)
}
})
}
}
func TestNew(t *testing.T) {
type args struct {
url string
queue string
exchange Exchange
opt OptionFunc
}
tests := []struct {
name string
args args
wantErr bool
}{
{"success", args{url: "amqp://guest:guest@localhost:5672/", queue: "q", exchange: *validExch, opt: Buffer(100)}, false},
{"fail, invalid url", args{url: "", queue: "q", exchange: *validExch, opt: Buffer(100)}, true},
{"fail, invalid queue name", args{url: "url", queue: "", exchange: *validExch, opt: Buffer(100)}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.url, tt.args.queue, tt.args.exchange, tt.args.opt)
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}
func TestFactory_Create(t *testing.T) {
type fields struct {
oo []OptionFunc
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{name: "success", wantErr: false},
{name: "invalid option", fields: fields{oo: []OptionFunc{Buffer(-10)}}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &Factory{
url: "url",
queue: "queue",
exchange: *validExch,
bindings: []string{},
oo: tt.fields.oo,
}
got, err := f.Create()
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
require.NotNil(t, got)
assert.True(t, got.OutOfOrder())
}
})
}
}
func Test_mapHeader(t *testing.T) {
hh := amqp.Table{"test1": 10, "test2": 0.11}
mm := map[string]string{"test1": "10", "test2": "0.11"}
assert.Equal(t, mm, mapHeader(hh))
}
func Test_getCorrelationID(t *testing.T) {
withID := amqp.Table{correlation.HeaderID: "123"}
withoutID := amqp.Table{correlation.HeaderID: ""}
missingHeader := amqp.Table{}
type args struct {
hh amqp.Table
}
tests := map[string]struct {
args args
}{
"with id": {args: args{hh: withID}},
"without id": {args: args{hh: withoutID}},
"missing header": {args: args{hh: missingHeader}},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
assert.NotEmpty(t, getCorrelationID(tt.args.hh))
})
}
}