Skip to content

Commit

Permalink
ASB: ScheduledEnqueueTimeUtc improve date parsing and return err (#2875)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Verst <[email protected]>
Co-authored-by: Alessandro (Ale) Segala <[email protected]>
  • Loading branch information
berndverst and ItalyPaleAle authored May 30, 2023
1 parent 8e714b1 commit 1dee97f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/component/azure/servicebus/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ func addMetadataToMessage(asbMsg *azservicebus.Message, metadata map[string]stri
timeVal, err := time.Parse(http.TimeFormat, v)
if err == nil {
asbMsg.ScheduledEnqueueTime = &timeVal
} else {
timeVal, err2 := time.Parse(time.RFC3339, v)
if err2 == nil {
asbMsg.ScheduledEnqueueTime = &timeVal
} else {
return fmt.Errorf("invalid time format for %s; expected HTTP time format or RFC3339", k)
}
}

// Fallback: set as application property
Expand Down
26 changes: 26 additions & 0 deletions internal/component/azure/servicebus/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package servicebus
import (
"testing"

azservicebus "github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -505,4 +506,29 @@ func TestParseServiceBusMetadata(t *testing.T) {
assert.Nil(t, m.LockDurationInSec)
assert.Nil(t, err)
})

t.Run("Test add system metadata: ScheduledEnqueueTimeUtc", func(t *testing.T) {
msg := azservicebus.Message{}
metadata := map[string]string{
MessageKeyScheduledEnqueueTimeUtc: "2024-06-15T13:45:30.00000000Z",
}
parseErr := addMetadataToMessage(&msg, metadata)
assert.NoError(t, parseErr)
assert.Equal(t, int64(1718459130000000), msg.ScheduledEnqueueTime.UnixMicro())

msg2 := azservicebus.Message{}
metadata2 := map[string]string{
MessageKeyScheduledEnqueueTimeUtc: "Sat, 15 Jun 2024 13:45:30 GMT",
}
parseErr2 := addMetadataToMessage(&msg2, metadata2)
assert.NoError(t, parseErr2)
assert.Equal(t, int64(1718459130000000), msg2.ScheduledEnqueueTime.UnixMicro())

msg3 := azservicebus.Message{}
metadata3 := map[string]string{
MessageKeyScheduledEnqueueTimeUtc: "Sat 2024-06-15 12:13:14 UTC+4",
}
parseErr3 := addMetadataToMessage(&msg3, metadata3)
assert.Error(t, parseErr3)
})
}

0 comments on commit 1dee97f

Please sign in to comment.