Skip to content

Commit

Permalink
Allow no delay publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 committed Oct 22, 2024
1 parent 0973bb4 commit b7fb98b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 15 additions & 1 deletion components/delay/publisher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package delay

import "github.com/ThreeDotsLabs/watermill/message"
import (
"errors"

"github.com/ThreeDotsLabs/watermill/message"
)

type DefaultDelayGeneratorParams struct {
Topic string
Expand All @@ -12,6 +16,10 @@ type PublisherConfig struct {
// DefaultDelayGenerator is a function that generates the default delay for a message.
// If the message doesn't have the delay metadata set, the default delay will be applied.
DefaultDelayGenerator func(params DefaultDelayGeneratorParams) (Delay, error)

// AllowNoDelay allows publishing messages without a delay set.
// By default, the publisher returns an error when a message is published without a delay and no default delay generator is provided.
AllowNoDelay bool
}

// NewPublisher wraps a publisher with a delay mechanism.
Expand Down Expand Up @@ -63,6 +71,12 @@ func (p *publisher) applyDelay(topic string, msg *message.Message) error {
return err
}

Check warning on line 72 in components/delay/publisher.go

View check run for this annotation

Codecov / codecov/patch

components/delay/publisher.go#L71-L72

Added lines #L71 - L72 were not covered by tests
Message(msg, delay)

return nil
}

if !p.config.AllowNoDelay {
return errors.New("message doesn't have a delay set")
}

return nil
Expand Down
22 changes: 21 additions & 1 deletion components/delay/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func TestPublisher(t *testing.T) {
pub, err := delay.NewPublisher(pubSub, delay.PublisherConfig{})
require.NoError(t, err)

pubAllowNoDelay, err := delay.NewPublisher(pubSub, delay.PublisherConfig{
AllowNoDelay: true,
})
require.NoError(t, err)

defaultDelayPub, err := delay.NewPublisher(pubSub, delay.PublisherConfig{
DefaultDelayGenerator: func(params delay.DefaultDelayGeneratorParams) (delay.Delay, error) {
return delay.For(1 * time.Second), nil
Expand All @@ -34,6 +39,7 @@ func TestPublisher(t *testing.T) {
name string
publisher message.Publisher
messageConstructor func(id string) *message.Message
expectedError bool
expectedDelay time.Duration
}{
{
Expand All @@ -42,6 +48,15 @@ func TestPublisher(t *testing.T) {
messageConstructor: func(id string) *message.Message {
return message.NewMessage(id, nil)
},
expectedError: true,
expectedDelay: 0,
},
{
name: "no delay but allowed",
publisher: pubAllowNoDelay,
messageConstructor: func(id string) *message.Message {
return message.NewMessage(id, nil)
},
expectedDelay: 0,
},
{
Expand Down Expand Up @@ -124,8 +139,13 @@ func TestPublisher(t *testing.T) {

msg := testCase.messageConstructor(id)
err = testCase.publisher.Publish("test", msg)
require.NoError(t, err)

if testCase.expectedError {
require.Error(t, err)
return
}

require.NoError(t, err)
assertMessage(t, messages, id, testCase.expectedDelay)
})
}
Expand Down

0 comments on commit b7fb98b

Please sign in to comment.