From ce794f635502c6b0f90b455782073485f49244a2 Mon Sep 17 00:00:00 2001 From: Emil Shakirov Date: Mon, 14 Oct 2019 15:01:04 +0200 Subject: [PATCH 1/2] Add tests for verifying proper sqs configuration --- worker/worker_test.go | 50 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/worker/worker_test.go b/worker/worker_test.go index 065dff9..fcba502 100644 --- a/worker/worker_test.go +++ b/worker/worker_test.go @@ -10,6 +10,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/sqs" "github.com/aws/aws-sdk-go/service/sqs/sqsiface" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) @@ -17,19 +18,23 @@ type mockedSqsClient struct { Config *aws.Config Response sqs.ReceiveMessageOutput sqsiface.SQSAPI + mock.Mock } func (c *mockedSqsClient) GetQueueUrl(urlInput *sqs.GetQueueUrlInput) (*sqs.GetQueueUrlOutput, error) { - url := fmt.Sprintf("https://sqs.%v.amazonaws.com/123456789/%v", c.Config.Region, urlInput.QueueName) + url := fmt.Sprintf("https://sqs.%v.amazonaws.com/123456789/%v", *c.Config.Region, *urlInput.QueueName) return &sqs.GetQueueUrlOutput{QueueUrl: &url}, nil } -func (c *mockedSqsClient) ReceiveMessage(*sqs.ReceiveMessageInput) (*sqs.ReceiveMessageOutput, error) { +func (c *mockedSqsClient) ReceiveMessage(input *sqs.ReceiveMessageInput) (*sqs.ReceiveMessageOutput, error) { + c.Called(input) + return &c.Response, nil } -func (c *mockedSqsClient) DeleteMessage(*sqs.DeleteMessageInput) (*sqs.DeleteMessageOutput, error) { +func (c *mockedSqsClient) DeleteMessage(input *sqs.DeleteMessageInput) (*sqs.DeleteMessageOutput, error) { + c.Called(input) c.Response = sqs.ReceiveMessageOutput{} return &sqs.DeleteMessageOutput{}, nil @@ -39,15 +44,15 @@ type mockedHandler struct { mock.Mock } +func (mh *mockedHandler) HandleMessage(foo string, qux string) { + mh.Called(foo, qux) +} + type sqsEvent struct { Foo string `json:"foo"` Qux string `json:"qux"` } -func (mh *mockedHandler) HandleMessage(foo string, qux string) { - mh.Called(foo, qux) -} - func TestStart(t *testing.T) { region := "eu-west-1" awsConfig := &aws.Config{Region: ®ion} @@ -69,10 +74,19 @@ func TestStart(t *testing.T) { return }) - t.Run("when worker successfully receives a message", func(t *testing.T) { + t.Run("the worker has correct configuration", func(t *testing.T) { + assert.Equal(t, worker.Config.QueueName, "my-sqs-queue", "QueueName has been set properly") + assert.Equal(t, worker.Config.QueueURL, "https://sqs.eu-west-1.amazonaws.com/123456789/my-sqs-queue", "QueueURL has been set properly") + assert.Equal(t, worker.Config.MaxNumberOfMessage, int64(10), "MaxNumberOfMessage has been set properly") + assert.Equal(t, worker.Config.WaitTimeSecond, int64(20), "WaitTimeSecond has been set properly") + }) + + t.Run("the worker successfully processes a message", func(t *testing.T) { + setupClientSpies(client) handler.On("HandleMessage", "bar", "baz").Return().Once() worker.Start(ctx, handlerFunc) + client.AssertExpectations(t) handler.AssertExpectations(t) }) } @@ -83,7 +97,7 @@ func contextAndCancel() (context.Context, context.CancelFunc) { return context.WithDeadline(context.Background(), delay) } -func setupMockedSqsClient(awsConfig *aws.Config) sqsiface.SQSAPI { +func setupMockedSqsClient(awsConfig *aws.Config) *mockedSqsClient { sqsMessage := &sqs.Message{Body: aws.String(`{ "foo": "bar", "qux": "baz" }`)} sqsResponse := sqs.ReceiveMessageOutput{ Messages: []*sqs.Message{sqsMessage}, @@ -91,3 +105,21 @@ func setupMockedSqsClient(awsConfig *aws.Config) sqsiface.SQSAPI { return &mockedSqsClient{Response: sqsResponse, Config: awsConfig} } + +func setupClientSpies(client *mockedSqsClient) { + url := aws.String("https://sqs.eu-west-1.amazonaws.com/123456789/my-sqs-queue") + receiveInput := &sqs.ReceiveMessageInput{ + QueueUrl: url, + MaxNumberOfMessages: aws.Int64(10), + AttributeNames: []*string{ + aws.String("All"), + }, + WaitTimeSeconds: aws.Int64(10), + } + client.On("ReceiveMessage", receiveInput).Return() + + deleteInput := &sqs.DeleteMessageInput{ + QueueUrl: url, + } + client.On("DeleteMessage", deleteInput).Return() +} From b5eb50b0f0097239403ee8eb34391f3fb8990195 Mon Sep 17 00:00:00 2001 From: Emil Shakirov Date: Mon, 14 Oct 2019 15:01:45 +0200 Subject: [PATCH 2/2] Use QueueURL instead of QueueUrl when building aws params --- worker/worker.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/worker/worker.go b/worker/worker.go index 6c659ce..3c3520b 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -77,7 +77,7 @@ func (worker *Worker) Start(ctx context.Context, h Handler) { worker.Log.Debug("worker: Start Polling") params := &sqs.ReceiveMessageInput{ - QueueUrl: aws.String(worker.Config.QueueName), // Required + QueueUrl: aws.String(worker.Config.QueueURL), // Required MaxNumberOfMessages: aws.Int64(worker.Config.MaxNumberOfMessage), AttributeNames: []*string{ aws.String("All"), // Required @@ -127,8 +127,8 @@ func (worker *Worker) handleMessage(m *sqs.Message, h Handler) error { } params := &sqs.DeleteMessageInput{ - QueueUrl: aws.String(worker.Config.QueueName), // Required - ReceiptHandle: m.ReceiptHandle, // Required + QueueUrl: aws.String(worker.Config.QueueURL), // Required + ReceiptHandle: m.ReceiptHandle, // Required } _, err = worker.SqsClient.DeleteMessage(params) if err != nil {