Skip to content

Commit

Permalink
♻️ use an interface for the ManagedSettlingHandler (#126)
Browse files Browse the repository at this point in the history
* use an interface for the ManagedSettlingHandler
* fix tests
  • Loading branch information
serbrech authored Apr 17, 2023
1 parent a83569e commit e31062f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
19 changes: 15 additions & 4 deletions v2/managedsettling.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,30 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
)

// ManagedSettlingFunc is the signature of the message handler to implement when using the ManagedSettling middleware
// ManagedSettlingFunc allows to convert a function with the signature
// func(context.Context, *azservicebus.ReceivedMessage) error
// to the ManagedSettlingHandler interface.
type ManagedSettlingFunc func(ctx context.Context, message *azservicebus.ReceivedMessage) error

func (f ManagedSettlingFunc) Handle(ctx context.Context, message *azservicebus.ReceivedMessage) error {
return f(ctx, message)
}

// ManagedSettlingHandler is the message Handler interface for the ManagedSettler.
type ManagedSettlingHandler interface {
Handle(context.Context, *azservicebus.ReceivedMessage) error
}

var _ Handler = (*ManagedSettler)(nil)

// ManagedSettler is a middleware that allows to reduce the message handler signature to ManagedSettlingFunc
type ManagedSettler struct {
next ManagedSettlingFunc
next ManagedSettlingHandler
options *ManagedSettlingOptions
}

func (m *ManagedSettler) Handle(ctx context.Context, settler MessageSettler, message *azservicebus.ReceivedMessage) {
if err := m.next(ctx, message); err != nil {
if err := m.next.Handle(ctx, message); err != nil {
log(ctx, "error returned from the handler. Calling ManagedSettler error handler")
m.options.OnError(ctx, m.options, settler, message, err)
return
Expand Down Expand Up @@ -99,7 +110,7 @@ type ManagedSettlingOptions struct {
// nil -> complete
// the RetryDecision can be overridden and can inspect the error returned to decide to retry the message or not.
// this allows to define error types that shouldn't be retried (and moved directly to the deadletter queue)
func NewManagedSettlingHandler(opts *ManagedSettlingOptions, handler ManagedSettlingFunc) *ManagedSettler {
func NewManagedSettlingHandler(opts *ManagedSettlingOptions, handler ManagedSettlingHandler) *ManagedSettler {
options := defaultManagedSettlingOptions()
if opts != nil {
if opts.OnError != nil {
Expand Down
7 changes: 4 additions & 3 deletions v2/managedsettling_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"

"github.com/Azure/go-shuttle/v2"
)

Expand Down Expand Up @@ -46,14 +47,14 @@ func ExampleNewManagedSettlingHandler() {
cancel()
}

func myManagedSettlementHandler() shuttle.ManagedSettlingFunc {
func myManagedSettlementHandler() shuttle.ManagedSettlingHandler {
count := 0
return func(ctx context.Context, message *azservicebus.ReceivedMessage) error {
return shuttle.ManagedSettlingFunc(func(ctx context.Context, message *azservicebus.ReceivedMessage) error {
count++
if count == 0 {
// this will abandon the message
return fmt.Errorf("this will abandon the message, and eventually move it to DLQ")
}
return nil // this will complete the message
}
})
}
12 changes: 6 additions & 6 deletions v2/managedsettling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ func TestManagedSettler_Handle(t *testing.T) {
},
}
h := NewManagedSettlingHandler(options,
func(ctx context.Context, message *azservicebus.ReceivedMessage) error {
ManagedSettlingFunc(func(ctx context.Context, message *azservicebus.ReceivedMessage) error {
return tc.handlerResponse
})
}))
h.Handle(context.TODO(), &tc.settler, tc.msg)
tc.expectation(tt, tc.hooks, &tc.settler)
})
Expand All @@ -162,9 +162,9 @@ func Test_NilErr_WrappedInDeadLetter(t *testing.T) {
func TestDefaultOptions_CallDefaultHooks(t *testing.T) {
h := NewManagedSettlingHandler(&ManagedSettlingOptions{
RetryDelayStrategy: &ConstantDelayStrategy{Delay: 0},
}, func(_ context.Context, _ *azservicebus.ReceivedMessage) error {
}, ManagedSettlingFunc(func(_ context.Context, _ *azservicebus.ReceivedMessage) error {
return nil
})
}))

settler := &fakeSettler{}
h.Handle(context.TODO(), settler, &azservicebus.ReceivedMessage{})
Expand All @@ -190,9 +190,9 @@ func TestOnErrorOverride(t *testing.T) {
opts.OnError = func(ctx context.Context, opts *ManagedSettlingOptions, settler MessageSettler, message *azservicebus.ReceivedMessage, handleErr error) {
onErrorCalled = true
}
h := NewManagedSettlingHandler(opts, func(_ context.Context, _ *azservicebus.ReceivedMessage) error {
h := NewManagedSettlingHandler(opts, ManagedSettlingFunc(func(_ context.Context, _ *azservicebus.ReceivedMessage) error {
return fmt.Errorf("failed")
})
}))
h.Handle(context.Background(), settler, &azservicebus.ReceivedMessage{})
g.Expect(onErrorCalled).To(BeTrue())
}
Expand Down

0 comments on commit e31062f

Please sign in to comment.