-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDP-1317] Add MessageDispatcher to SDP (#391)
- Loading branch information
1 parent
99cbb5d
commit 3f16719
Showing
19 changed files
with
597 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package dependencyinjection | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/stellar/stellar-disbursement-platform-backend/internal/message" | ||
) | ||
|
||
const MessageDispatcherInstanceName = "message_dispatcher_instance" | ||
|
||
type MessageDispatcherOpts struct { | ||
EmailOpts *EmailClientOptions | ||
SMSOpts *SMSClientOptions | ||
} | ||
|
||
func NewMessageDispatcher(ctx context.Context, opts MessageDispatcherOpts) (*message.MessageDispatcher, error) { | ||
if instance, ok := GetInstance(MessageDispatcherInstanceName); ok { | ||
if dispatcherInstance, ok := instance.(*message.MessageDispatcher); ok { | ||
return dispatcherInstance, nil | ||
} | ||
return nil, fmt.Errorf("trying to cast pre-existing MessageDispatcher for dependency injection") | ||
} | ||
|
||
dispatcher := message.NewMessageDispatcher() | ||
|
||
if opts.EmailOpts != nil { | ||
emailClient, err := NewEmailClient(*opts.EmailOpts) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating email client: %w", err) | ||
} | ||
dispatcher.RegisterClient(ctx, message.MessageChannelEmail, emailClient) | ||
} | ||
|
||
if opts.SMSOpts != nil { | ||
smsClient, err := NewSMSClient(*opts.SMSOpts) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating SMS client: %w", err) | ||
} | ||
dispatcher.RegisterClient(ctx, message.MessageChannelSMS, smsClient) | ||
} | ||
|
||
SetInstance(MessageDispatcherInstanceName, dispatcher) | ||
return dispatcher, nil | ||
} |
116 changes: 116 additions & 0 deletions
116
internal/dependencyinjection/message_dispatcher_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package dependencyinjection | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/stellar/stellar-disbursement-platform-backend/internal/message" | ||
) | ||
|
||
func Test_NewMessageDispatcher(t *testing.T) { | ||
ctx := context.Background() | ||
t.Run("should return the same instance when called twice", func(t *testing.T) { | ||
defer ClearInstancesTestHelper(t) | ||
|
||
opts := MessageDispatcherOpts{ | ||
EmailOpts: &EmailClientOptions{ | ||
EmailType: message.MessengerTypeDryRun, | ||
}, | ||
SMSOpts: &SMSClientOptions{ | ||
SMSType: message.MessengerTypeDryRun, | ||
}, | ||
} | ||
|
||
dispatcher1, err := NewMessageDispatcher(ctx, opts) | ||
require.NoError(t, err) | ||
dispatcher2, err := NewMessageDispatcher(ctx, opts) | ||
require.NoError(t, err) | ||
assert.Equal(t, dispatcher1, dispatcher2) | ||
}) | ||
|
||
t.Run("should create dispatcher with email client only", func(t *testing.T) { | ||
defer ClearInstancesTestHelper(t) | ||
|
||
opts := MessageDispatcherOpts{ | ||
EmailOpts: &EmailClientOptions{ | ||
EmailType: message.MessengerTypeDryRun, | ||
}, | ||
} | ||
|
||
dispatcher, err := NewMessageDispatcher(ctx, opts) | ||
require.NoError(t, err) | ||
|
||
emailClient, err := dispatcher.GetClient(message.MessageChannelEmail) | ||
require.NoError(t, err) | ||
assert.NotNil(t, emailClient) | ||
|
||
smsClient, err := dispatcher.GetClient(message.MessageChannelSMS) | ||
assert.EqualError(t, err, "no client registered for channel \"SMS\"") | ||
assert.Nil(t, smsClient) | ||
}) | ||
|
||
t.Run("should create dispatcher with SMS client only", func(t *testing.T) { | ||
defer ClearInstancesTestHelper(t) | ||
|
||
opts := MessageDispatcherOpts{ | ||
SMSOpts: &SMSClientOptions{ | ||
SMSType: message.MessengerTypeDryRun, | ||
}, | ||
} | ||
|
||
dispatcher, err := NewMessageDispatcher(ctx, opts) | ||
require.NoError(t, err) | ||
|
||
smsClient, err := dispatcher.GetClient(message.MessageChannelSMS) | ||
require.NoError(t, err) | ||
assert.NotNil(t, smsClient) | ||
|
||
emailClient, err := dispatcher.GetClient(message.MessageChannelEmail) | ||
assert.EqualError(t, err, "no client registered for channel \"EMAIL\"") | ||
assert.Nil(t, emailClient) | ||
}) | ||
|
||
t.Run("should return an error on invalid email client creation", func(t *testing.T) { | ||
defer ClearInstancesTestHelper(t) | ||
|
||
opts := MessageDispatcherOpts{ | ||
EmailOpts: &EmailClientOptions{ | ||
EmailType: "invalid-type", | ||
}, | ||
} | ||
|
||
dispatcher, err := NewMessageDispatcher(ctx, opts) | ||
assert.ErrorContains(t, err, `trying to create a Email client with a non-supported Email type: "invalid-type"`) | ||
assert.Nil(t, dispatcher) | ||
}) | ||
|
||
t.Run("should return an error on invalid SMS client creation", func(t *testing.T) { | ||
defer ClearInstancesTestHelper(t) | ||
|
||
opts := MessageDispatcherOpts{ | ||
SMSOpts: &SMSClientOptions{ | ||
SMSType: "invalid-type", | ||
}, | ||
} | ||
|
||
dispatcher, err := NewMessageDispatcher(ctx, opts) | ||
assert.ErrorContains(t, err, `trying to create a SMS client with a non-supported SMS type: "invalid-type"`) | ||
assert.Nil(t, dispatcher) | ||
}) | ||
|
||
t.Run("should return an error on invalid pre-existing instance", func(t *testing.T) { | ||
defer ClearInstancesTestHelper(t) | ||
|
||
preExistingDispatcherWithInvalidType := struct{}{} | ||
SetInstance(MessageDispatcherInstanceName, preExistingDispatcherWithInvalidType) | ||
|
||
opts := MessageDispatcherOpts{} | ||
|
||
gotDispatcher, err := NewMessageDispatcher(ctx, opts) | ||
assert.Nil(t, gotDispatcher) | ||
assert.EqualError(t, err, "trying to cast pre-existing MessageDispatcher for dependency injection") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package message | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/stellar/go/support/log" | ||
) | ||
|
||
type MessageChannel string | ||
|
||
const ( | ||
MessageChannelEmail MessageChannel = "EMAIL" | ||
MessageChannelSMS MessageChannel = "SMS" | ||
) | ||
|
||
//go:generate mockery --name MessageDispatcherInterface --case=underscore --structname=MockMessageDispatcher --inpackage | ||
type MessageDispatcherInterface interface { | ||
RegisterClient(ctx context.Context, channel MessageChannel, client MessengerClient) | ||
SendMessage(message Message, channel MessageChannel) error | ||
GetClient(channel MessageChannel) (MessengerClient, error) | ||
} | ||
|
||
type MessageDispatcher struct { | ||
clients map[MessageChannel]MessengerClient | ||
} | ||
|
||
func NewMessageDispatcher() *MessageDispatcher { | ||
return &MessageDispatcher{ | ||
clients: make(map[MessageChannel]MessengerClient), | ||
} | ||
} | ||
|
||
func (d *MessageDispatcher) RegisterClient(ctx context.Context, channel MessageChannel, client MessengerClient) { | ||
log.Ctx(ctx).Infof("📡 [MessageDispatcher] Registering client %s for channel %s", client.MessengerType(), channel) | ||
d.clients[channel] = client | ||
} | ||
|
||
func (d *MessageDispatcher) SendMessage(message Message, channel MessageChannel) error { | ||
client, err := d.GetClient(channel) | ||
if err != nil { | ||
return fmt.Errorf("getting client for channel: %w", err) | ||
} | ||
|
||
return client.SendMessage(message) | ||
} | ||
|
||
func (d *MessageDispatcher) GetClient(channel MessageChannel) (MessengerClient, error) { | ||
client, ok := d.clients[channel] | ||
if !ok { | ||
return nil, fmt.Errorf("no client registered for channel %q", channel) | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
var _ MessageDispatcherInterface = &MessageDispatcher{} |
Oops, something went wrong.