mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-16 15:10:12 +01:00
add client timeout tests
This commit is contained in:
parent
6c1dfecea3
commit
a762ec39aa
3 changed files with 161 additions and 5 deletions
35
internal/testing/delayhttp/delayhttp.go
Normal file
35
internal/testing/delayhttp/delayhttp.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// Package delayhttp creates http.HandlerFunc's that delays the response.
|
||||
// Useful for testing timeout scenarios.
|
||||
package delayhttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// WithChannel returns a handler that delays until it recieves something on returnChan
|
||||
func WithChannel(returnChan chan struct{}) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Wait until channel sends return code
|
||||
<-returnChan
|
||||
}
|
||||
}
|
||||
|
||||
// WithCancel returns a handler that delays until the cancel func is called.
|
||||
// Useful together with defer to clean up tests.
|
||||
func WithCancel() (http.HandlerFunc, func()) {
|
||||
returnChan := make(chan struct{}, 1)
|
||||
return WithChannel(returnChan), func() {
|
||||
returnChan <- struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// WithTimeout returns a handler that delays until the passed duration has elapsed
|
||||
func WithTimeout(delay time.Duration) http.HandlerFunc {
|
||||
returnChan := make(chan struct{}, 1)
|
||||
go func() {
|
||||
time.Sleep(delay)
|
||||
returnChan <- struct{}{}
|
||||
}()
|
||||
return WithChannel(returnChan)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue