Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging unit tests to locator #236

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 64 additions & 18 deletions pkg/stream/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,47 @@ package stream
import (
"context"
"errors"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/rabbitmq/rabbitmq-stream-go-client/v2/pkg/raw"
"golang.org/x/exp/slog"
"time"
)

var _ = Describe("Locator", func() {
Describe("Operations", func() {
var (
loc *locator
logger *slog.Logger
backOffPolicy = func(_ int) time.Duration {
return time.Millisecond * 10
}
)

BeforeEach(func() {
logger = slog.New(slog.NewTextHandler(GinkgoWriter))
h := slog.HandlerOptions{
Level: slog.LevelDebug,
}.NewTextHandler(GinkgoWriter)
logger = slog.New(h)
Zerpet marked this conversation as resolved.
Show resolved Hide resolved

loc = &locator{
log: logger,
shutdownNotification: make(chan struct{}),
rawClientConf: raw.ClientConfiguration{},
client: nil,
isSet: true,
clientClose: nil,
backOffPolicy: backOffPolicy,
}
})

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
Expand Down Expand Up @@ -71,7 +72,52 @@ var _ = Describe("Locator", func() {
Expect(r[0]).To(MatchError("oopsie"))
})

// TODO: add a test for logs
})

Context("logging", func() {
var logBuffer *gbytes.Buffer

BeforeEach(func() {
logBuffer = gbytes.NewBuffer()
GinkgoWriter.TeeTo(logBuffer)
})

It("emits a log when locator operation starts", func() {
_ = loc.locatorOperation(func(_ *locator, _ ...any) []any {
return []any{errors.New("oopsie")}
})

Eventually(logBuffer).Within(time.Second).Should(gbytes.Say("starting locator operation"))
})

It("emits a log when locator operation succeeds", func() {
_ = loc.locatorOperation(func(_ *locator, _ ...any) []any {
return []any{nil}
})

Eventually(logBuffer).Within(time.Second).Should(gbytes.Say("locator operation succeed"))
})

It("emits a log on error", func() {
_ = loc.locatorOperation(func(_ *locator, _ ...any) []any {
return []any{errors.New("oopsie")}
})
Eventually(logBuffer).Within(time.Second).Should(gbytes.Say("error in locator operation"))
Eventually(logBuffer).Within(time.Second).Should(gbytes.Say("error"))
Eventually(logBuffer).Within(time.Second).Should(gbytes.Say("oopsie"))
})

It("emits a log on non-retryable error", func() {
var runs int
_ = loc.locatorOperation(func(_ *locator, _ ...any) []any {
runs += 1
return []any{context.DeadlineExceeded}
})
Expect(runs).To(BeNumerically("==", 1))
Eventually(logBuffer).Within(time.Second).Should(gbytes.Say("locator operation failed with non-retryable error"))
Eventually(logBuffer).Within(time.Second).Should(gbytes.Say("error"))
Eventually(logBuffer).Within(time.Second).Should(gbytes.Say("context deadline exceeded"))
})
})
})

Expand Down