Skip to content

Commit

Permalink
Add logging unit tests to locator
Browse files Browse the repository at this point in the history
  • Loading branch information
ablease committed Sep 26, 2023
1 parent a6cced8 commit de0e74b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
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)

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,8 +72,53 @@ 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"))
})
})
})

Describe("Utils", func() {
Expand Down
10 changes: 10 additions & 0 deletions pkg/stream/stream_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package stream_test

import (
"testing"
"golang.org/x/exp/slog"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var logger *slog.Logger

func TestStream(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Stream Suite")
}

var _ = BeforeSuite(func() {
h := slog.HandlerOptions{
Level: slog.LevelDebug,
}.NewTextHandler(GinkgoWriter)
logger = slog.New(h)
})

0 comments on commit de0e74b

Please sign in to comment.