Skip to content

Commit a5fb860

Browse files
committed
chore: Add exchange type constant variable (direct, fanout, topic, headers)
1 parent 015c577 commit a5fb860

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

options.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ import (
77
"github.com/golang-queue/queue/core"
88
)
99

10+
// defined in rabbitmq client package.
11+
const (
12+
ExchangeDirect = "direct"
13+
ExchangeFanout = "fanout"
14+
ExchangeTopic = "topic"
15+
ExchangeHeaders = "headers"
16+
)
17+
18+
func isVaildExchange(name string) bool {
19+
switch name {
20+
case ExchangeDirect, ExchangeFanout, ExchangeTopic, ExchangeHeaders:
21+
return true
22+
default:
23+
return false
24+
}
25+
}
26+
1027
// Option for queue system
1128
type Option func(*options)
1229

@@ -104,7 +121,7 @@ func newOptions(opts ...Option) options {
104121
subj: "golang-queue",
105122
tag: "golang-queue",
106123
exchangeName: "test-exchange",
107-
exchangeType: "direct",
124+
exchangeType: ExchangeDirect,
108125
routingKey: "test-key",
109126
logger: queue.NewLogger(),
110127
autoAck: false,
@@ -119,5 +136,9 @@ func newOptions(opts ...Option) options {
119136
opt(&defaultOpts)
120137
}
121138

139+
if !isVaildExchange(defaultOpts.exchangeType) {
140+
defaultOpts.logger.Fatal("invaild exchange type: ", defaultOpts.exchangeType)
141+
}
142+
122143
return defaultOpts
123144
}

options_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package rabbitmq
2+
3+
import "testing"
4+
5+
func Test_isVaildExchange(t *testing.T) {
6+
type args struct {
7+
name string
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want bool
13+
}{
14+
{
15+
name: "fanout",
16+
args: args{
17+
name: ExchangeFanout,
18+
},
19+
want: true,
20+
},
21+
{
22+
name: "invaild exchange type",
23+
args: args{
24+
name: "test",
25+
},
26+
want: false,
27+
},
28+
}
29+
for _, tt := range tests {
30+
t.Run(tt.name, func(t *testing.T) {
31+
if got := isVaildExchange(tt.args.name); got != tt.want {
32+
t.Errorf("isVaildExchange() = %v, want %v", got, tt.want)
33+
}
34+
})
35+
}
36+
}

0 commit comments

Comments
 (0)