From 28a3d649a3775df0595bce0b9aa2e4d882a0478e Mon Sep 17 00:00:00 2001 From: Roberto Rojas Date: Tue, 16 Jan 2024 13:15:55 -0500 Subject: [PATCH] Metadata TryGetTTL: adds time.ParseDuration support (#3122) Signed-off-by: robertojrojas Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com> Co-authored-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com> --- .build-tools/go.mod | 8 +- .build-tools/go.sum | 42 ++++++- bindings/azure/storagequeues/metadata.yaml | 12 +- bindings/azure/storagequeues/storagequeues.go | 4 +- .../azure/storagequeues/storagequeues_test.go | 8 +- bindings/rabbitmq/metadata.yaml | 6 +- bindings/rabbitmq/rabbitmq.go | 2 +- metadata/utils.go | 28 +++-- metadata/utils_test.go | 106 ++++++++++++++++++ 9 files changed, 188 insertions(+), 28 deletions(-) diff --git a/.build-tools/go.mod b/.build-tools/go.mod index 2b7fb65bfd..7758891a8d 100644 --- a/.build-tools/go.mod +++ b/.build-tools/go.mod @@ -15,14 +15,16 @@ require ( require ( github.com/dapr/kit v0.12.2-0.20231031211530-0e1fd37fc4b3 // indirect + github.com/gogo/protobuf v1.3.2 // indirect github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect + github.com/spf13/cast v1.5.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect - gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + k8s.io/apimachinery v0.26.10 // indirect ) replace github.com/dapr/components-contrib => ../ diff --git a/.build-tools/go.sum b/.build-tools/go.sum index ef43cfd472..fd90cc2c70 100644 --- a/.build-tools/go.sum +++ b/.build-tools/go.sum @@ -1,22 +1,27 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/dapr/kit v0.12.2-0.20231031211530-0e1fd37fc4b3 h1:xsmVK3YOKRMOcaxqo50Ce0apQzq+LzAfWuFapQuu8Ro= github.com/dapr/kit v0.12.2-0.20231031211530-0e1fd37fc4b3/go.mod h1:c3Z78F+h7UYtb0LmpzJNC/ChT240ycDJFViRUztdpoo= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= +github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk= github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/invopop/jsonschema v0.6.0 h1:8e+xY8ZEn8gDHUYylSlLHy22P+SLeIRIHv3nM3hCbmY= github.com/invopop/jsonschema v0.6.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 h1:BpfhmLKZf+SjVanKKhCgf3bg+511DmU9eDQTen7LLbY= @@ -26,6 +31,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA= github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48= @@ -45,6 +52,35 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v1.2.1-0.20201027075954-b076d39a02e5 h1:ImnGIsrcG8vwbovhYvvSY8fagVV6QhCWSWXfzwGDLVs= github.com/xeipuuv/gojsonschema v1.2.1-0.20201027075954-b076d39a02e5/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/bindings/azure/storagequeues/metadata.yaml b/bindings/azure/storagequeues/metadata.yaml index 8b0170218a..3b8db46df4 100644 --- a/bindings/azure/storagequeues/metadata.yaml +++ b/bindings/azure/storagequeues/metadata.yaml @@ -50,14 +50,14 @@ metadata: Set the interval to poll Azure Storage Queues for new messages example: '"30s"' default: '"10s"' - - name: "ttlInSeconds" - type: number + - name: "ttl" + type: duration description: | - Set the default message Time To Live (TTL), in seconds. + Set the default message Time To Live (TTL). If empty, messages expire after 10 minutes. - It's also possible to specify a per-message TTL by setting the `ttlInSeconds` property in the invocation request's metadata. - example: '30' - default: '600' + It's also possible to specify a per-message TTL by setting the `ttl` property in the invocation request's metadata. + example: '30s' + default: '10m' binding: output: true input: false diff --git a/bindings/azure/storagequeues/storagequeues.go b/bindings/azure/storagequeues/storagequeues.go index f596bad98d..78e5540281 100644 --- a/bindings/azure/storagequeues/storagequeues.go +++ b/bindings/azure/storagequeues/storagequeues.go @@ -255,7 +255,7 @@ type storageQueuesMetadata struct { DecodeBase64 bool EncodeBase64 bool PollingInterval time.Duration `mapstructure:"pollingInterval"` - TTL *time.Duration `mapstructure:"ttlInSeconds"` + TTL *time.Duration `mapstructure:"ttl" mapstructurealiases:"ttlInSeconds"` VisibilityTimeout *time.Duration } @@ -328,6 +328,8 @@ func parseMetadata(meta bindings.Metadata) (*storageQueuesMetadata, error) { } if ok { m.TTL = &ttl + } else { + m.TTL = nil } return &m, nil diff --git a/bindings/azure/storagequeues/storagequeues_test.go b/bindings/azure/storagequeues/storagequeues_test.go index 27f72de692..98bef85991 100644 --- a/bindings/azure/storagequeues/storagequeues_test.go +++ b/bindings/azure/storagequeues/storagequeues_test.go @@ -352,7 +352,6 @@ func TestParseMetadata(t *testing.T) { // expectedAccountKey: "myKey", expectedQueueName: "queue1", expectedQueueEndpointURL: "", - expectedTTL: ptr.Of(time.Duration(0)), expectedPollingInterval: defaultPollingInterval, expectedVisibilityTimeout: ptr.Of(defaultVisibilityTimeout), }, @@ -394,7 +393,12 @@ func TestParseMetadata(t *testing.T) { require.NoError(t, err) // assert.Equal(t, tt.expectedAccountKey, meta.AccountKey) assert.Equal(t, tt.expectedQueueName, meta.QueueName) - assert.Equal(t, tt.expectedTTL, meta.TTL) + if tt.expectedTTL != nil { + _ = assert.NotNil(t, meta.TTL, "Expected TTL to be %v", *tt.expectedTTL) && + assert.Equal(t, *tt.expectedTTL, *meta.TTL) + } else if meta.TTL != nil { + assert.Failf(t, "Expected TTL to be nil", "Value was %v", *meta.TTL) + } assert.Equal(t, tt.expectedQueueEndpointURL, meta.QueueEndpoint) assert.Equal(t, tt.expectedVisibilityTimeout, meta.VisibilityTimeout) }) diff --git a/bindings/rabbitmq/metadata.yaml b/bindings/rabbitmq/metadata.yaml index 9b8a2c2d0f..ae1e2afac7 100644 --- a/bindings/rabbitmq/metadata.yaml +++ b/bindings/rabbitmq/metadata.yaml @@ -42,13 +42,13 @@ metadata: description: "Enables or disables auto-delete." default: 'false' example: '"true", "false"' - - name: ttlInSeconds - type: number + - name: ttl + type: duration description: | Set the default message time to live at RabbitMQ queue level. If this parameter is omitted, messages won't expire, continuing to exist on the queue until processed. - example: '60' + example: '60s' url: title: "RabbitMQ Time-To-Live and Expiration" url: "https://www.rabbitmq.com/ttl.html" diff --git a/bindings/rabbitmq/rabbitmq.go b/bindings/rabbitmq/rabbitmq.go index 96d04272dd..28ec7cbd0d 100644 --- a/bindings/rabbitmq/rabbitmq.go +++ b/bindings/rabbitmq/rabbitmq.go @@ -87,7 +87,7 @@ type rabbitMQMetadata struct { PrefetchCount int `mapstructure:"prefetchCount"` MaxPriority *uint8 `mapstructure:"maxPriority"` // Priority Queue deactivated if nil ReconnectWait time.Duration `mapstructure:"reconnectWaitInSeconds"` - DefaultQueueTTL *time.Duration `mapstructure:"ttlInSeconds"` + DefaultQueueTTL *time.Duration `mapstructure:"ttl" mapstructurealiases:"ttlInSeconds"` CaCert string `mapstructure:"caCert"` ClientCert string `mapstructure:"clientCert"` ClientKey string `mapstructure:"clientKey"` diff --git a/metadata/utils.go b/metadata/utils.go index 6ef1748457..dc82e8ff6f 100644 --- a/metadata/utils.go +++ b/metadata/utils.go @@ -21,12 +21,14 @@ import ( "strings" "time" + kitmd "github.com/dapr/kit/metadata" "github.com/dapr/kit/utils" ) const ( - // TTLMetadataKey defines the metadata key for setting a time to live (in seconds). - TTLMetadataKey = "ttlInSeconds" + // TTLMetadataKey defines the metadata key for setting a time to live (as a Go duration or number of seconds). + TTLMetadataKey = "ttl" + TTLInSecondsMetadataKey = "ttlInSeconds" // RawPayloadKey defines the metadata key for forcing raw payload in pubsub. RawPayloadKey = "rawPayload" @@ -46,26 +48,34 @@ const ( // TryGetTTL tries to get the ttl as a time.Duration value for pubsub, binding and any other building block. func TryGetTTL(props map[string]string) (time.Duration, bool, error) { - if val, ok := props[TTLMetadataKey]; ok && val != "" { + val, _ := kitmd.GetMetadataProperty(props, TTLMetadataKey, TTLInSecondsMetadataKey) + if val == "" { + return 0, false, nil + } + + // Try to parse as duration string first + duration, err := time.ParseDuration(val) + if err != nil { + // Failed to parse Duration string. + // Let's try Integer and assume the value is in seconds valInt64, err := strconv.ParseInt(val, 10, 64) if err != nil { return 0, false, fmt.Errorf("%s value must be a valid integer: actual is '%s'", TTLMetadataKey, val) } - if valInt64 <= 0 { - return 0, false, fmt.Errorf("%s value must be higher than zero: actual is %d", TTLMetadataKey, valInt64) + return 0, false, fmt.Errorf("%s value must be higher than zero: actual is '%d'", TTLMetadataKey, valInt64) } - duration := time.Duration(valInt64) * time.Second + duration = time.Duration(valInt64) * time.Second if duration < 0 { // Overflow duration = math.MaxInt64 } - - return duration, true, nil + } else if duration < 0 { + duration = 0 } - return 0, false, nil + return duration, true, nil } // TryGetPriority tries to get the priority for binding and any other building block. diff --git a/metadata/utils_test.go b/metadata/utils_test.go index 3c9cecb23d..44c09e2bb0 100644 --- a/metadata/utils_test.go +++ b/metadata/utils_test.go @@ -14,6 +14,8 @@ limitations under the License. package metadata import ( + "fmt" + "math" "reflect" "testing" "time" @@ -24,6 +26,110 @@ import ( "github.com/dapr/kit/metadata" ) +func TestTryGetTTL(t *testing.T) { + tests := []struct { + name string + md map[string]string + result any + wantOK bool + wantErr bool + errStr string + }{ + { + name: "ttl valid duration 20s", + md: map[string]string{ + TTLMetadataKey: "20s", + }, + result: time.Duration(20) * time.Second, + wantOK: true, + }, + { + name: "ttl valid integer 20", + md: map[string]string{ + TTLMetadataKey: "20", + }, + result: time.Duration(20) * time.Second, + wantOK: true, + }, + { + name: "ttlInSeconds valid duration 20s", + md: map[string]string{ + TTLInSecondsMetadataKey: "20s", + }, + result: time.Duration(20) * time.Second, + wantOK: true, + }, + { + name: "ttlInSeconds valid integer 20", + md: map[string]string{ + TTLInSecondsMetadataKey: "20", + }, + result: time.Duration(20) * time.Second, + wantOK: true, + }, + { + name: "invalid integer 20b", + md: map[string]string{ + TTLMetadataKey: "20b", + }, + wantOK: false, + wantErr: true, + errStr: "value must be a valid integer: actual is '20b'", + result: time.Duration(0) * time.Second, + }, + { + name: "negative ttl -1", + md: map[string]string{ + TTLMetadataKey: "-1", + }, + wantOK: false, + wantErr: true, + errStr: "value must be higher than zero: actual is '-1'", + result: time.Duration(0) * time.Second, + }, + { + name: "negative ttl -1s", + md: map[string]string{ + TTLMetadataKey: "-1s", + }, + wantOK: true, + wantErr: false, + result: time.Duration(0) * time.Second, + }, + { + name: "no ttl", + md: map[string]string{}, + wantOK: false, + wantErr: false, + result: time.Duration(0) * time.Second, + }, + { + name: "out of range", + md: map[string]string{ + TTLMetadataKey: fmt.Sprintf("%d1", math.MaxInt64), + }, + wantOK: false, + wantErr: true, + result: time.Duration(0) * time.Second, + errStr: "value must be a valid integer: actual is '92233720368547758071'", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + d, ok, err := TryGetTTL(tt.md) + + if tt.wantErr { + require.Error(t, err) + require.ErrorContains(t, err, tt.errStr) + } else { + require.NoError(t, err) + } + assert.Equal(t, tt.wantOK, ok, "wanted ok, but instead got not ok") + assert.Equal(t, tt.result, d, "expected result %v, but instead got = %v", tt.result, d) + }) + } +} + func TestIsRawPayload(t *testing.T) { t.Run("Metadata not found", func(t *testing.T) { val, err := IsRawPayload(map[string]string{