-
Notifications
You must be signed in to change notification settings - Fork 8
/
connection_test.go
82 lines (66 loc) · 1.7 KB
/
connection_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
package amqprpc
import (
"encoding/json"
"fmt"
"net/http"
"slices"
"testing"
"time"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/require"
)
func TestCreateConnections(t *testing.T) {
hasConnections := func(t *testing.T, names ...string) bool {
connectionsURL := fmt.Sprintf("%s/connections", serverAPITestURL)
resp, err := http.Get(connectionsURL)
require.NoError(t, err)
result := []struct {
UserProvidedName string `json:"user_provided_name"`
}{}
err = json.NewDecoder(resp.Body).Decode(&result)
require.NoError(t, err)
_ = resp.Body.Close()
foundNames := []string{}
for _, conn := range result {
foundNames = append(foundNames, conn.UserProvidedName)
}
for _, wantName := range names {
if !slices.Contains(foundNames, wantName) {
return false
}
}
return true
}
tests := []struct {
name string
config amqp.Config
wantConsumerConnName string
wantPublisherConnName string
}{
{
name: "nil Properties sets connection names",
config: amqp.Config{
Properties: nil,
},
wantConsumerConnName: "testingName-consumer",
wantPublisherConnName: "testingName-publisher",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := amqp.Config{
Properties: amqp.Table{},
Dial: amqp.DefaultDial(time.Second),
}
consumerConn, publisherConn, err := createConnections(testURL, "testingName", config)
require.NoError(t, err)
require.Eventually(t,
func() bool { return hasConnections(t, tt.wantConsumerConnName, tt.wantPublisherConnName) },
10*time.Second,
100*time.Millisecond,
)
consumerConn.Close()
publisherConn.Close()
})
}
}