Skip to content

Commit

Permalink
Fix incorrect QueueUrl (#8)
Browse files Browse the repository at this point in the history
Fix incorrect QueueUrl
  • Loading branch information
jwhitcraft authored Oct 18, 2019
2 parents 2e5e6ea + b5eb50b commit 2fc79bf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
6 changes: 3 additions & 3 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
50 changes: 41 additions & 9 deletions worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@ 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"
)

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
Expand All @@ -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: &region}
Expand All @@ -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)
})
}
Expand All @@ -83,11 +97,29 @@ 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},
}

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()
}

0 comments on commit 2fc79bf

Please sign in to comment.