2025-01-31 13:43:55 +00:00
|
|
|
package runner
|
2025-01-10 11:12:03 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-02-03 17:30:50 +00:00
|
|
|
queuemock "queryorchestration/mocks/queue"
|
|
|
|
|
runnermock "queryorchestration/mocks/runner"
|
2025-01-10 11:12:03 +00:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
2025-01-10 11:12:03 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-02-03 17:30:50 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2025-01-10 11:12:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestPollMessages(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
2025-02-03 18:51:44 +00:00
|
|
|
cfg := &BaseConfig{}
|
|
|
|
|
cfg.QueueClient = mockSQS
|
|
|
|
|
cfg.ControllerFunc = func() Controller {
|
|
|
|
|
return runnermock.NewMockController(t)
|
2025-02-03 17:30:50 +00:00
|
|
|
}
|
2025-02-03 18:51:44 +00:00
|
|
|
cfg.QueueURL = "/i/am/here"
|
2025-02-03 17:30:50 +00:00
|
|
|
|
|
|
|
|
res := sqs.ReceiveMessageOutput{Messages: []types.Message{}}
|
|
|
|
|
|
|
|
|
|
mockSQS.EXPECT().
|
|
|
|
|
ReceiveMessage(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *sqs.ReceiveMessageInput) bool {
|
2025-02-03 18:51:44 +00:00
|
|
|
return *in.QueueUrl == cfg.QueueURL
|
2025-02-03 17:30:50 +00:00
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&res, nil)
|
2025-01-10 11:12:03 +00:00
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
scfg := &Server{
|
2025-02-03 18:51:44 +00:00
|
|
|
cfg: cfg,
|
|
|
|
|
cleanup: func() error { return nil },
|
2025-01-10 19:17:20 +00:00
|
|
|
}
|
2025-02-03 17:30:50 +00:00
|
|
|
scfg.Listen(ctx)
|
2025-01-10 11:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPollMessage(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
2025-02-03 18:51:44 +00:00
|
|
|
cfg := &BaseConfig{}
|
|
|
|
|
cfg.QueueClient = mockSQS
|
|
|
|
|
cfg.ControllerFunc = func() Controller {
|
|
|
|
|
return runnermock.NewMockController(t)
|
|
|
|
|
}
|
|
|
|
|
cfg.QueueURL = "/i/am/here"
|
2025-01-10 11:12:03 +00:00
|
|
|
|
2025-02-03 18:51:44 +00:00
|
|
|
ser := &Server{
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
cleanup: func() error { return nil },
|
2025-01-10 19:17:20 +00:00
|
|
|
}
|
2025-02-03 17:30:50 +00:00
|
|
|
|
|
|
|
|
mockSQS.EXPECT().
|
|
|
|
|
ReceiveMessage(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *sqs.ReceiveMessageInput) bool {
|
2025-02-03 18:51:44 +00:00
|
|
|
return *in.QueueUrl == cfg.QueueURL
|
2025-02-03 17:30:50 +00:00
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&sqs.ReceiveMessageOutput{}, nil)
|
|
|
|
|
|
2025-02-03 18:51:44 +00:00
|
|
|
err := ser.pollMessage(ctx)
|
2025-02-03 17:30:50 +00:00
|
|
|
assert.NoError(t, err)
|
2025-01-10 11:12:03 +00:00
|
|
|
}
|