4b8c930ff1
Split Queue Process * queue
154 lines
3.2 KiB
Go
154 lines
3.2 KiB
Go
package runner
|
|
|
|
import (
|
|
"context"
|
|
queuemock "queryorchestration/mocks/queue"
|
|
runnermock "queryorchestration/mocks/runner"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
func TestPollMessages(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
cfg := &BaseConfig{}
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
cfg.QueueClient = mockSQS
|
|
mockController := runnermock.NewMockController(t)
|
|
cfg.Controller = mockController
|
|
cfg.QueueURL = "/i/am/here"
|
|
|
|
res := sqs.ReceiveMessageOutput{Messages: []types.Message{}}
|
|
|
|
mockSQS.EXPECT().
|
|
ReceiveMessage(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *sqs.ReceiveMessageInput) bool {
|
|
return *in.QueueUrl == cfg.QueueURL
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&res, nil)
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
|
|
scfg := &Server{
|
|
cfg: cfg,
|
|
cleanup: func() error { return nil },
|
|
}
|
|
scfg.Listen(ctx)
|
|
}
|
|
|
|
func TestPollMessage(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
cfg := &BaseConfig{}
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
cfg.QueueClient = mockSQS
|
|
mockController := runnermock.NewMockController(t)
|
|
cfg.Controller = mockController
|
|
cfg.QueueURL = "/i/am/here"
|
|
|
|
ser := &Server{
|
|
cfg: cfg,
|
|
cleanup: func() error { return nil },
|
|
}
|
|
|
|
id := "example_id"
|
|
body := "example_body"
|
|
receipt := "receipts"
|
|
|
|
mockSQS.EXPECT().
|
|
ReceiveMessage(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *sqs.ReceiveMessageInput) bool {
|
|
return *in.QueueUrl == cfg.QueueURL
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&sqs.ReceiveMessageOutput{
|
|
Messages: []types.Message{
|
|
{
|
|
MessageId: &id,
|
|
Body: &body,
|
|
ReceiptHandle: &receipt,
|
|
},
|
|
},
|
|
}, nil)
|
|
|
|
mockController.EXPECT().
|
|
Process(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *types.Message) bool {
|
|
return *in.MessageId == id && *in.Body == body
|
|
}),
|
|
).
|
|
Return(nil)
|
|
|
|
mockSQS.EXPECT().
|
|
DeleteMessage(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *sqs.DeleteMessageInput) bool {
|
|
return *in.QueueUrl == cfg.QueueURL && *in.ReceiptHandle == receipt
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&sqs.DeleteMessageOutput{}, nil)
|
|
|
|
err := ser.pollMessage(ctx)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestProcessMessage(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
cfg := &BaseConfig{}
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
cfg.QueueClient = mockSQS
|
|
mockController := runnermock.NewMockController(t)
|
|
cfg.Controller = mockController
|
|
cfg.QueueURL = "/i/am/here"
|
|
|
|
ser := &Server{
|
|
cfg: cfg,
|
|
cleanup: func() error { return nil },
|
|
}
|
|
|
|
id := "example_id"
|
|
body := "example_body"
|
|
receipt := "receipts"
|
|
msg := &types.Message{
|
|
MessageId: &id,
|
|
Body: &body,
|
|
ReceiptHandle: &receipt,
|
|
}
|
|
|
|
mockController.EXPECT().
|
|
Process(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *types.Message) bool {
|
|
return *in.MessageId == id && *in.Body == body
|
|
}),
|
|
).
|
|
Return(nil)
|
|
|
|
mockSQS.EXPECT().
|
|
DeleteMessage(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *sqs.DeleteMessageInput) bool {
|
|
return *in.QueueUrl == cfg.QueueURL && *in.ReceiptHandle == receipt
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&sqs.DeleteMessageOutput{}, nil)
|
|
|
|
err := ser.processMessage(ctx, msg)
|
|
assert.NoError(t, err)
|
|
}
|