Skip to content

Commit

Permalink
Merge pull request #3 from asecurityteam/update-interface
Browse files Browse the repository at this point in the history
upgrade interface version
  • Loading branch information
mikerott authored Oct 25, 2022
2 parents e58b9ff + 2eb8f2d commit 5d263df
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
22 changes: 21 additions & 1 deletion compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// runsqs.SQSProducer to produce the compressed message.
// We must base64 encode the compressed message because SQS
// supports only character data -- valid utf8 -- not raw binary data
type CompressionSQSProducer struct {
type CompressionSQSProducer struct { // nolint
Wrapped runsqs.SQSProducer
}

Expand All @@ -41,3 +41,23 @@ func (producer *CompressionSQSProducer) ProduceMessage(ctx context.Context, mess
messageInput.MessageBody = aws.String(string(encodedBytes))
return producer.Wrapped.ProduceMessage(ctx, messageInput)
}

// BatchProduceMessage produces compressed, base64 encoded messages to the configured sqs queue.
func (producer *CompressionSQSProducer) BatchProduceMessage(ctx context.Context, messageBatchInput *sqs.SendMessageBatchInput) (*sqs.SendMessageBatchOutput, error) {
for i := range messageBatchInput.Entries {
var bytes bytes.Buffer
gz := gzip.NewWriter(&bytes)
if _, err := gz.Write([]byte(*messageBatchInput.Entries[i].MessageBody)); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
encodedBytes := make([]byte, base64.StdEncoding.EncodedLen(len(bytes.Bytes())))
base64.StdEncoding.Encode(encodedBytes, bytes.Bytes())

messageBatchInput.QueueUrl = aws.String(producer.Wrapped.QueueURL())
messageBatchInput.Entries[i].MessageBody = aws.String(string(encodedBytes))
}
return producer.Wrapped.BatchProduceMessage(ctx, messageBatchInput)
}
46 changes: 42 additions & 4 deletions compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import (
"github.com/golang/mock/gomock"
)

func TestCompression(t *testing.T) {
const (
helloWorld = `hello world!`
encodedCompressedMessage = `H4sIAAAAAAAA/8pIzcnJVyjPL8pJUQQEAAD//23CtAMMAAAA`
exampleURL = `www.example.com`
)

func TestCompressionProduceMessage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand All @@ -20,9 +26,9 @@ func TestCompression(t *testing.T) {
compressionProducer := CompressionSQSProducer{
Wrapped: mockMessageProducer,
}
queueURL := "www.example.com"
originalMessage := "hello world!"
encodedCompressedMessage := "H4sIAAAAAAAA/8pIzcnJVyjPL8pJUQQEAAD//23CtAMMAAAA"
queueURL := exampleURL
originalMessage := helloWorld
encodedCompressedMessage := encodedCompressedMessage

mockMessageProducer.EXPECT().QueueURL().Return(queueURL)
mockMessageProducer.EXPECT().ProduceMessage(mockContext, &sqs.SendMessageInput{
Expand All @@ -36,3 +42,35 @@ func TestCompression(t *testing.T) {
assert.Nil(t, e)

}

func TestCompressionBatchProduceMessage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockContext := context.Background()
mockMessageProducer := NewMockSQSProducer(ctrl)

compressionProducer := CompressionSQSProducer{
Wrapped: mockMessageProducer,
}
queueURL := exampleURL
originalMessage := helloWorld
encodedCompressedMessage := encodedCompressedMessage

mockMessageProducer.EXPECT().QueueURL().Return(queueURL)
mockMessageProducer.EXPECT().BatchProduceMessage(mockContext, &sqs.SendMessageBatchInput{
QueueUrl: &queueURL,
Entries: []*sqs.SendMessageBatchRequestEntry{{
MessageBody: &encodedCompressedMessage,
}},
}).Return(nil, nil)

_, e := compressionProducer.BatchProduceMessage(mockContext, &sqs.SendMessageBatchInput{
QueueUrl: &queueURL,
Entries: []*sqs.SendMessageBatchRequestEntry{{
MessageBody: &originalMessage,
}},
})
assert.Nil(t, e)

}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/asecurityteam/runsqs-compressor
go 1.13

require (
github.com/asecurityteam/runsqs/v2 v2.0.0
github.com/asecurityteam/runsqs/v2 v2.5.0
github.com/aws/aws-sdk-go v1.43.43
github.com/golang/mock v1.6.0
github.com/rs/zerolog v1.26.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/asecurityteam/logevent v1.6.1 h1:D/V11UxgZMBrktTgL3ugRO4hKdBWkz/xLBlpqDq4PoM=
github.com/asecurityteam/logevent v1.6.1/go.mod h1:wtopE0Pe+072poxy+lGBMah604Uu6/BVwFWRwKwF3TA=
github.com/asecurityteam/runsqs/v2 v2.0.0 h1:HhIGlmYS5KyjthZrFnntJsln3RM7uhvwXv04suQRCP8=
github.com/asecurityteam/runsqs/v2 v2.0.0/go.mod h1:G11mzsloYaO7IJLy/1BiqFF4T+2v1jLZhijZSyPu4t8=
github.com/asecurityteam/runsqs/v2 v2.5.0 h1:p/cG4nrTiIsH6cjLtKuqaCCs0Vh7ZCh6v35MaO1+XUs=
github.com/asecurityteam/runsqs/v2 v2.5.0/go.mod h1:G11mzsloYaO7IJLy/1BiqFF4T+2v1jLZhijZSyPu4t8=
github.com/aws/aws-sdk-go v1.35.0/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
github.com/aws/aws-sdk-go v1.43.43 h1:1L06qzQvl4aC3Skfh5rV7xVhGHjIZoHcqy16NoyQ1o4=
github.com/aws/aws-sdk-go v1.43.43/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
Expand Down
15 changes: 15 additions & 0 deletions mock_runsqs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d263df

Please sign in to comment.