-
Notifications
You must be signed in to change notification settings - Fork 5
/
amqp_transport_test.go
91 lines (78 loc) · 1.77 KB
/
amqp_transport_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
package gosumer
import (
"context"
"fmt"
"testing"
"time"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/assert"
)
func TestAmqpConnect(t *testing.T) {
transport := RabbitMQ{
Host: "localhost",
Port: nil,
User: "guest",
Password: "guest",
Queue: "queue_name",
}
err := transport.connect()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestAmqpListen(t *testing.T) {
transport := RabbitMQ{
Host: "localhost",
Port: nil,
User: "guest",
Password: "guest",
Queue: "queue_name",
}
go func() {
err := transport.listen(processMessage, Message{}, 0)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}()
connection, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s/%s", transport.User, transport.Password, transport.Host, "%2f"))
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
defer connection.Close()
channel, err := connection.Channel()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
defer channel.Close()
q, err := channel.QueueDeclare(
transport.Queue,
true,
false,
false,
false,
nil,
)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
body := `{"id": 1, "name": "John Doe"}`
err = channel.PublishWithContext(ctx,
"",
q.Name,
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
},
)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// TODO: maybe found a better way to wait for the message to be processed
time.Sleep(1 * time.Second)
assert.True(t, processMessageCalled, "Expected processMessage to be called")
processMessageCalled = false
}