-
Notifications
You must be signed in to change notification settings - Fork 12
/
locator_test.go
112 lines (95 loc) · 3.22 KB
/
locator_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
package stream
import (
"context"
"errors"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rabbitmq/rabbitmq-stream-go-client/v2/pkg/raw"
"golang.org/x/exp/slog"
"time"
)
var _ = Describe("Locator", func() {
Describe("Operations", func() {
var (
logger *slog.Logger
backOffPolicy = func(_ int) time.Duration {
return time.Millisecond * 10
}
)
BeforeEach(func() {
logger = slog.New(slog.NewTextHandler(GinkgoWriter))
})
It("reconnects", func() {
Skip("this needs a real rabbit to test, or close enough to real rabbit")
})
When("there is an error", func() {
var (
loc *locator
)
BeforeEach(func() {
loc = &locator{
log: logger,
shutdownNotification: make(chan struct{}),
rawClientConf: raw.ClientConfiguration{},
client: nil,
isSet: true,
clientClose: nil,
backOffPolicy: backOffPolicy,
}
})
It("retries the operation on retryable errors", func() {
var runs int
_ = loc.locatorOperation(func(_ *locator, _ ...any) []any {
runs += 1
return []any{errors.New("oopsie")}
})
Expect(runs).To(BeNumerically("==", 3))
})
It("gives up on non-retryable errors", func() {
var runs int
_ = loc.locatorOperation(func(_ *locator, _ ...any) []any {
runs += 1
return []any{context.DeadlineExceeded}
})
Expect(runs).To(BeNumerically("==", 1))
})
It("bubbles up the error", func() {
r := loc.locatorOperation(func(_ *locator, _ ...any) []any {
return []any{errors.New("oopsie")}
})
Expect(r).To(HaveLen(1)) // not all result slices with error have only 1 element tho
Expect(r[0]).To(BeAssignableToTypeOf(errors.New("an error")))
Expect(r[0]).To(MatchError("oopsie"))
})
// TODO: add a test for logs
})
})
Describe("Utils", func() {
var (
discardLogger = slog.New(discardHandler{})
)
It("determines if server version is 3.11 or more", func() {
conf, err := raw.NewClientConfiguration("")
Expect(err).ToNot(HaveOccurred())
versionVal := "3.10.0"
conf.SetServerProperties("version", versionVal)
loc := newLocator(*conf, discardLogger)
Expect(loc.isServer311orMore()).To(BeFalse(), "expected %s to be lower than 3.11.0", versionVal)
versionVal = "3.11.0"
conf.SetServerProperties("version", versionVal)
Expect(loc.isServer311orMore()).To(BeTrue(), "expected %s to be greater or equal than 3.11.0", versionVal)
versionVal = "3.11.10"
conf.SetServerProperties("version", versionVal)
Expect(loc.isServer311orMore()).To(BeTrue(), "expected %s to be greater or equal than 3.11.0", versionVal)
versionVal = "3.12.0"
conf.SetServerProperties("version", versionVal)
Expect(loc.isServer311orMore()).To(BeTrue(), "expected %s to be greater or equal than 3.11.0", versionVal)
versionVal = "3.11.0-alpha.2"
conf.SetServerProperties("version", versionVal)
Expect(loc.isServer311orMore()).To(BeFalse(), "expected %s to be lower than 3.11.0", versionVal)
versionVal = "3.13.0-beta.1"
conf.SetServerProperties("version", versionVal)
Expect(loc.isServer311orMore()).To(BeTrue(), "expected %s to be greater or equal than 3.11.0", versionVal)
})
})
})