fix: parse configured dispatch delay strings
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
@@ -457,6 +459,9 @@ func jsonInt(value any) (int, bool) {
|
|||||||
case json.Number:
|
case json.Number:
|
||||||
parsed, err := typed.Int64()
|
parsed, err := typed.Int64()
|
||||||
return int(parsed), err == nil
|
return int(parsed), err == nil
|
||||||
|
case string:
|
||||||
|
parsed, err := strconv.Atoi(strings.TrimSpace(typed))
|
||||||
|
return parsed, err == nil
|
||||||
default:
|
default:
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,28 @@ package store
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
|
func TestJSONInt(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
value any
|
||||||
|
want int
|
||||||
|
ok bool
|
||||||
|
}{
|
||||||
|
{name: "number", value: float64(900), want: 900, ok: true},
|
||||||
|
{name: "numeric string", value: "1200", want: 1200, ok: true},
|
||||||
|
{name: "trimmed numeric string", value: " 900 ", want: 900, ok: true},
|
||||||
|
{name: "invalid string", value: "15 minutes", ok: false},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, ok := jsonInt(test.value)
|
||||||
|
if got != test.want || ok != test.ok {
|
||||||
|
t.Fatalf("jsonInt(%v) = (%d, %v), want (%d, %v)", test.value, got, ok, test.want, test.ok)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRandomDelaySeconds(t *testing.T) {
|
func TestRandomDelaySeconds(t *testing.T) {
|
||||||
for index := 0; index < 100; index++ {
|
for index := 0; index < 100; index++ {
|
||||||
value := randomDelaySeconds(900, 1200)
|
value := randomDelaySeconds(900, 1200)
|
||||||
|
|||||||
Reference in New Issue
Block a user