-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers_test.go
179 lines (149 loc) · 4.92 KB
/
helpers_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
167
168
169
170
171
172
173
174
175
176
177
178
179
package amqpx_test
import (
"context"
"errors"
"fmt"
"github.com/jxsl13/amqpx"
"github.com/jxsl13/amqpx/internal/testutils"
"github.com/jxsl13/amqpx/logging"
"github.com/jxsl13/amqpx/pool"
)
func declareExchangeQueue(ctx context.Context, eq testutils.ExchangeQueue, t *pool.Topologer, log logging.Logger) (err error) {
err = createExchange(ctx, eq.Exchange, t, log)
if err != nil {
return err
}
err = createQueue(ctx, eq.Queue, t, log)
if err != nil {
return err
}
err = bindQueue(ctx, eq.Queue, eq.Exchange, eq.RoutingKey, t, log)
if err != nil {
return err
}
return nil
}
func deleteExchangeQueue(ctx context.Context, eq testutils.ExchangeQueue, t *pool.Topologer, log logging.Logger) (err error) {
err = unbindQueue(ctx, eq.Queue, eq.Exchange, eq.RoutingKey, t, log)
if err != nil {
return err
}
err = deleteQueue(ctx, eq.Queue, t, log)
if err != nil {
return err
}
err = deleteExchange(ctx, eq.Exchange, t, log)
if err != nil {
return err
}
return nil
}
func createTopology(log logging.Logger, eqs ...testutils.ExchangeQueue) amqpx.TopologyFunc {
return func(ctx context.Context, t *pool.Topologer) (err error) {
// documentation: https://www.cloudamqp.com/blog/part4-rabbitmq-for-beginners-exchanges-routing-keys-bindings.html#:~:text=The%20routing%20key%20is%20a%20message%20attribute%20added%20to%20the,routing%20key%20of%20the%20message.
for _, eq := range eqs {
err = declareExchangeQueue(ctx, eq, t, log)
if err != nil {
return err
}
}
return nil
}
}
func deleteTopology(log logging.Logger, eqs ...testutils.ExchangeQueue) amqpx.TopologyFunc {
return func(ctx context.Context, t *pool.Topologer) (err error) {
for _, eq := range eqs {
err = deleteExchangeQueue(ctx, eq, t, log)
if err != nil {
return err
}
}
return nil
}
}
func createQueue(ctx context.Context, name string, t *pool.Topologer, log logging.Logger) (err error) {
log.Infof("topology: creating queue: %s", name)
_, err = t.QueueDeclarePassive(ctx, name)
if !errors.Is(err, pool.ErrNotFound) {
if err != nil {
return fmt.Errorf("queue %s was found even tho it should not exist: %w", name, err)
}
return fmt.Errorf("queue %s was found even tho it should not exist", name)
}
_, err = t.QueueDeclare(ctx, name)
if err != nil {
return err
}
_, err = t.QueueDeclarePassive(ctx, name)
if err != nil {
return fmt.Errorf("queue %s was not found even tho it should exist: %w", name, err)
}
return nil
}
func deleteQueue(ctx context.Context, name string, t *pool.Topologer, log logging.Logger) (err error) {
log.Infof("topology: deleting queue: %s", name)
_, err = t.QueueDeclarePassive(ctx, name)
if err != nil {
return fmt.Errorf("%q does not exist but is supposed to be deleted: %w", name, err)
}
_, err = t.QueueDelete(ctx, name)
if err != nil {
return err
}
_, err = t.QueueDeclarePassive(ctx, name)
if err == nil {
return fmt.Errorf("%q still exists after deletion", name)
}
return nil
}
func createExchange(ctx context.Context, name string, t *pool.Topologer, log logging.Logger) (err error) {
log.Infof("topology: creating exchange: %s", name)
err = t.ExchangeDeclarePassive(ctx, name, pool.ExchangeKindTopic)
if !errors.Is(err, pool.ErrNotFound) {
if err != nil {
return fmt.Errorf("exchange %s was found even tho it should not exist: %w", name, err)
}
return fmt.Errorf("exchange %s was found even tho it should not exist", name)
}
err = t.ExchangeDeclare(ctx, name, pool.ExchangeKindTopic)
if err != nil {
return err
}
err = t.ExchangeDeclarePassive(ctx, name, pool.ExchangeKindTopic)
if err != nil {
return fmt.Errorf("exchange %s was not found even tho it should exist: %w", name, err)
}
return nil
}
func deleteExchange(ctx context.Context, name string, t *pool.Topologer, log logging.Logger) (err error) {
log.Infof("topology: deleting exchange: %s", name)
err = t.ExchangeDeclarePassive(ctx, name, pool.ExchangeKindTopic)
if err != nil {
return fmt.Errorf("exchange %s was not found even tho it should exist: %w", name, err)
}
err = t.ExchangeDelete(ctx, name)
if err != nil {
return err
}
err = t.ExchangeDeclarePassive(ctx, name, pool.ExchangeKindTopic)
if !errors.Is(err, pool.ErrNotFound) {
return fmt.Errorf("exchange %s was found even tho it should not exist: %w", name, err)
}
return nil
}
func bindQueue(ctx context.Context, queue, exchange, routingKey string, t *pool.Topologer, log logging.Logger) (err error) {
log.Infof("topology: binding queue %s to exchange %s with routing key: %s", queue, exchange, routingKey)
err = t.QueueBind(ctx, queue, routingKey, exchange)
if err != nil {
return err
}
return nil
}
func unbindQueue(ctx context.Context, queue, exchange, routingKey string, t *pool.Topologer, log logging.Logger) (err error) {
log.Infof("topology: unbinding queue %s from exchange %s with routing key: %s", queue, exchange, routingKey)
err = t.QueueUnbind(ctx, queue, routingKey, exchange)
if err != nil {
return err
}
return nil
}