-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(httpserver): option to set shutdown timeout for unit tests (#2066)
* chore(httpserver): optional shutdown timeout parameter * fix(pprof-tests): bigger shutdown timeout * Increase timeout for `Test_Server_Run_success` * Add test depth to `Test_New` in `httpserver` * Add licenses
- Loading branch information
Showing
8 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2021 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package httpserver | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_newOptionalSettings(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
options []Option | ||
settings optionalSettings | ||
}{ | ||
"no option": { | ||
settings: optionalSettings{ | ||
shutdownTimeout: 3 * time.Second, | ||
}, | ||
}, | ||
"shutdown option": { | ||
options: []Option{ | ||
ShutdownTimeout(time.Second), | ||
}, | ||
settings: optionalSettings{ | ||
shutdownTimeout: time.Second, | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
settings := newOptionalSettings(testCase.options) | ||
|
||
assert.Equal(t, testCase.settings, settings) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2021 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package httpserver | ||
|
||
import "time" | ||
|
||
// Option is a functional option for the HTTP server. | ||
type Option func(s *optionalSettings) | ||
|
||
type optionalSettings struct { | ||
shutdownTimeout time.Duration | ||
} | ||
|
||
func newOptionalSettings(options []Option) (settings optionalSettings) { | ||
for _, option := range options { | ||
option(&settings) | ||
} | ||
|
||
if settings.shutdownTimeout == 0 { | ||
const defaultShutdownTimeout = 3 * time.Second | ||
settings.shutdownTimeout = defaultShutdownTimeout | ||
} | ||
|
||
return settings | ||
} | ||
|
||
// ShutdownTimeout sets an optional timeout for the HTTP server | ||
// to shutdown. The default shutdown is 3 seconds. | ||
func ShutdownTimeout(timeout time.Duration) Option { | ||
return func(s *optionalSettings) { | ||
s.shutdownTimeout = timeout | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters