-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add dependency * feat: add interface and mock * feat: add mq service * feat: set up mq * feat: add example env * fix: test issue --------- Co-authored-by: Eyo Chen <eyo.chen@amazingtalker.com>
- Loading branch information
Showing
11 changed files
with
339 additions
and
3 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
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
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,24 @@ | ||
package mq | ||
|
||
import ( | ||
"github.com/eyo-chen/expense-tracker-go/internal/adapter/interfaces" | ||
"github.com/eyo-chen/expense-tracker-go/pkg/logger" | ||
amqp "github.com/rabbitmq/amqp091-go" | ||
) | ||
|
||
// NewMQClient initializes a new MQ client. | ||
func NewMQClient(url string) (interfaces.MQClient, error) { | ||
conn, err := amqp.Dial(url) | ||
if err != nil { | ||
logger.Error("Failed to connect to message queue", "error", err) | ||
return nil, err | ||
} | ||
|
||
ch, err := conn.Channel() | ||
if err != nil { | ||
logger.Error("Failed to create channel", "error", err) | ||
return nil, err | ||
} | ||
|
||
return ch, nil | ||
} |
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,54 @@ | ||
package mq | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/eyo-chen/expense-tracker-go/internal/adapter/interfaces" | ||
"github.com/eyo-chen/expense-tracker-go/pkg/logger" | ||
amqp "github.com/rabbitmq/amqp091-go" | ||
) | ||
|
||
const ( | ||
packageName = "adapter/service/mq" | ||
) | ||
|
||
type Service struct { | ||
QueueName string | ||
MQClient interfaces.MQClient | ||
} | ||
|
||
// NewMQService initializes a new MQ service. | ||
func New(queueName string, mqClient interfaces.MQClient) *Service { | ||
queue, err := mqClient.QueueDeclare(queueName, true, false, false, false, nil) | ||
if err != nil { | ||
logger.Fatal("Failed to declare queue", "error", err, "package", packageName) | ||
return nil | ||
} | ||
|
||
return &Service{QueueName: queue.Name, MQClient: mqClient} | ||
} | ||
|
||
func (s *Service) Publish(ctx context.Context, msg interface{}) error { | ||
body, err := json.Marshal(msg) | ||
if err != nil { | ||
logger.Error("Failed to marshal message", "error", err, "package", packageName) | ||
return err | ||
} | ||
|
||
return s.MQClient.PublishWithContext( | ||
ctx, | ||
"", // default exchange | ||
s.QueueName, | ||
false, | ||
false, | ||
amqp.Publishing{ | ||
ContentType: "application/json", | ||
Body: body, | ||
}, | ||
) | ||
} | ||
|
||
func (s *Service) Close() { | ||
s.MQClient.Close() | ||
} |
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,100 @@ | ||
package mq | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/eyo-chen/expense-tracker-go/mocks" | ||
"github.com/eyo-chen/expense-tracker-go/pkg/logger" | ||
"github.com/eyo-chen/expense-tracker-go/pkg/testutil" | ||
amqp "github.com/rabbitmq/amqp091-go" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
var ( | ||
mockCTX = context.Background() | ||
mockQueueName = "test-queue" | ||
) | ||
|
||
type mqServiceSuite struct { | ||
suite.Suite | ||
service *Service | ||
mockMQClient *mocks.MQClient | ||
} | ||
|
||
func TestMQServiceSuite(t *testing.T) { | ||
suite.Run(t, new(mqServiceSuite)) | ||
} | ||
|
||
func (s *mqServiceSuite) SetupSuite() { | ||
logger.Register() | ||
} | ||
|
||
func (s *mqServiceSuite) SetupTest() { | ||
s.mockMQClient = new(mocks.MQClient) | ||
|
||
var args amqp.Table | ||
s.mockMQClient.On("QueueDeclare", mockQueueName, true, false, false, false, args).Return(amqp.Queue{Name: mockQueueName}, nil) | ||
s.service = New(mockQueueName, s.mockMQClient) | ||
} | ||
|
||
func (s *mqServiceSuite) TearDownTest() { | ||
s.mockMQClient.AssertExpectations(s.T()) | ||
} | ||
|
||
func (s *mqServiceSuite) TestPublish() { | ||
for scenario, fn := range map[string]func(s *mqServiceSuite, desc string){ | ||
"when no error, publish successfully": publish_NoError_ReturnSuccessfully, | ||
"when publish failed, return error": publish_Error_ReturnError, | ||
} { | ||
s.Run(testutil.GetFunName(fn), func() { | ||
s.SetupTest() | ||
fn(s, scenario) | ||
s.TearDownTest() | ||
}) | ||
} | ||
} | ||
|
||
func publish_NoError_ReturnSuccessfully(s *mqServiceSuite, desc string) { | ||
// prepare mock data | ||
mockMessage := "test-message" | ||
mockBody, err := json.Marshal(mockMessage) | ||
s.Require().NoError(err, desc) | ||
mockPublishing := amqp.Publishing{ | ||
ContentType: "application/json", | ||
Body: mockBody, | ||
} | ||
|
||
s.mockMQClient.On("PublishWithContext", mockCTX, "", mockQueueName, false, false, mockPublishing).Return(nil) | ||
|
||
err = s.service.Publish(mockCTX, mockMessage) | ||
s.Require().NoError(err, desc) | ||
} | ||
|
||
func publish_Error_ReturnError(s *mqServiceSuite, desc string) { | ||
// prepare mock data | ||
mockMessage := "test-message" | ||
mockBody, err := json.Marshal(mockMessage) | ||
s.Require().NoError(err, desc) | ||
mockPublishing := amqp.Publishing{ | ||
ContentType: "application/json", | ||
Body: mockBody, | ||
} | ||
mockError := errors.New("test-error") | ||
|
||
s.mockMQClient.On("PublishWithContext", mockCTX, "", mockQueueName, false, false, mockPublishing).Return(mockError) | ||
|
||
err = s.service.Publish(mockCTX, mockMessage) | ||
|
||
s.Require().ErrorIs(err, mockError, desc) | ||
} | ||
|
||
func (s *mqServiceSuite) TestClose() { | ||
// mock service | ||
s.mockMQClient.On("Close").Return(nil) | ||
|
||
// action | ||
s.service.Close() | ||
} |
Oops, something went wrong.