Skip to content

Commit a10e8bb

Browse files
committed
Delete parsing error when cleanup_timeout is less than or equal to 0.
1 parent 7e697af commit a10e8bb

2 files changed

Lines changed: 31 additions & 25 deletions

File tree

‎fbconn/config.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ type Config struct {
4040
ConnectTimeout time.Duration
4141
// CleanupTimeout used to clean up server resources in the event of an error.
4242
// The value must be greater than 0; otherwise, the default value of
43-
// 5 seconds will be used. When parsing from a configuration string,
44-
// a parsing error will occur if the value is less than or equal to 0.
43+
// 5 seconds will be used.
4544
CleanupTimeout time.Duration
4645
DialFunc DialFunc // e.g. net.Dialer.DialContext
4746
LookupFunc LookupFunc // e.g. net.Resolver.LookupHost
@@ -458,9 +457,10 @@ func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Con
458457
return nil, &ParseConfigError{ConnString: connString, msg: "invalid cleanup_timeout", err: err}
459458
}
460459
if cleanupTimeout <= 0 {
461-
return nil, &ParseConfigError{ConnString: connString, msg: "invalid cleanup_timeout", err: errors.New("cleanup_timeout must be positive")}
460+
config.CleanupTimeout = defaultGracePeriod
461+
} else {
462+
config.CleanupTimeout = cleanupTimeout
462463
}
463-
config.CleanupTimeout = cleanupTimeout
464464
}
465465

466466
config.LookupFunc = makeDefaultResolver().LookupHost

‎fbconn/config_test.go‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -560,32 +560,38 @@ func TestParseConfigPageSize(t *testing.T) {
560560
func TestParseConfigCleanupTimeout(t *testing.T) {
561561
t.Parallel()
562562

563-
for _, connString := range []string{
564-
"cleanup_timeout=250ms",
565-
"firebird://localhost/testdb?cleanup_timeout=250ms",
566-
} {
567-
t.Run(connString, func(t *testing.T) {
568-
config, err := fbconn.ParseConfig(connString)
569-
require.NoError(t, err)
570-
require.Equal(t, 250*time.Millisecond, config.CleanupTimeout)
571-
require.NotContains(t, config.RuntimeParams, "cleanup_timeout")
572-
})
563+
tests := []struct {
564+
value string
565+
want time.Duration
566+
wantErr bool
567+
}{
568+
{value: "10s", want: 10 * time.Second},
569+
{value: "150ns", want: 150 * time.Nanosecond},
570+
{value: "250ms", want: 250 * time.Millisecond},
571+
{value: "0s", want: 5 * time.Second},
572+
{value: "-100s", want: 5 * time.Second},
573+
{value: "-250ms", want: 5 * time.Second},
574+
{value: "250", wantErr: true},
575+
{value: "invalid", wantErr: true},
573576
}
574-
}
575-
576-
func TestParseConfigInvalidCleanupTimeout(t *testing.T) {
577-
t.Parallel()
578577

579-
for _, value := range []string{"invalid", "250", "0s", "-250ms"} {
578+
for _, tt := range tests {
580579
for _, connString := range []string{
581-
"cleanup_timeout=" + value,
582-
"firebird://localhost/testdb?cleanup_timeout=" + value,
580+
"cleanup_timeout=" + tt.value,
581+
"firebird://localhost/testdb?cleanup_timeout=" + tt.value,
583582
} {
584583
t.Run(connString, func(t *testing.T) {
585-
_, err := fbconn.ParseConfig(connString)
586-
var parseErr *fbconn.ParseConfigError
587-
require.ErrorAs(t, err, &parseErr)
588-
require.ErrorContains(t, err, "invalid cleanup_timeout")
584+
config, err := fbconn.ParseConfig(connString)
585+
if tt.wantErr {
586+
var parseErr *fbconn.ParseConfigError
587+
require.ErrorAs(t, err, &parseErr)
588+
require.ErrorContains(t, err, "invalid cleanup_timeout")
589+
return
590+
}
591+
592+
require.NoError(t, err)
593+
assert.Equal(t, tt.want, config.CleanupTimeout)
594+
require.NotContains(t, config.RuntimeParams, "cleanup_timeout")
589595
})
590596
}
591597
}

0 commit comments

Comments
 (0)