Documentation ¶
Overview ¶
Package cstesting provides testing helpers
Index ¶
- Constants
- func ChangeDir(t fataler, dir string) func()
- func ChangeEnv(t fataler, key, value string) func()
- func Close(t errorFormatter, c io.Closer)
- func ContainsCount(t errorFormatter, str, contains string, maxOccurrences int)
- func GCPause() time.Duration
- func MustFreePort() int
- type HTTPParallelUsers
- type HTTPTrip
Constants ¶
const ( HeaderUserID = "X-Test-User-ID" HeaderLoopID = "X-Test-Loop-ID" HeaderSleep = "X-Test-Sleep" )
Header* got set within an user iteration to allow you to identify a request.
Variables ¶
This section is empty.
Functions ¶
func ChangeDir ¶
func ChangeDir(t fataler, dir string) func()
ChangeDir temporarily changes the working directory for non parallel tests.
defer cstesting.ChangeDir(t, os.TempDir())()
func ChangeEnv ¶
func ChangeEnv(t fataler, key, value string) func()
ChangeEnv temporarily changes the environment for non parallel tests.
defer cstesting.ChangeEnv(t, "PATH", ".")()
func ContainsCount ¶
ContainsCount checks if str contains the substring contains maxOccurrences times.
func MustFreePort ¶
func MustFreePort() int
MustFreePort returns a free TCP port or panics on error.
Types ¶
type HTTPParallelUsers ¶
type HTTPParallelUsers struct { // Users or also known as number of threads Users int // Loops each user runs these loops Loops int // RampUpPeriod time to take to generate to the full request force. The // duration calculates: RampUpPeriod * Interval RampUpPeriod int // Interval an enum set of time.Nanosecond, time.Microsecond, time.Millisecond, // time.Second, time.Minute, time.Hour. Interval time.Duration // AssertResponse provides the possibility to check the written data after each // request. AssertResponse func(*httptest.ResponseRecorder) }
HTTPParallelUsers allows to run parallel and concurrent calls to a given http.Handler.
func NewHTTPParallelUsers ¶
func NewHTTPParallelUsers(users, loopsPerUser, rampUpPeriod int, interval time.Duration) HTTPParallelUsers
NewHTTPParallelUsers initializes a new request producer. Users means the total amount of parallel users. Each user can execute a specific loopsPerUser count. The rampUpPeriod defines the total runtime of the test and the period it takes to produce the finally total amount of parallel requests. The interval applies to the exported constants of the time package: time.Nanosecond, time.Microsecond, time.Millisecond, time.Second, time.Minute, time.Hour. The total runtime calculates rampUpPeriod * interval. Every (rampUpPeriod / users) a new user starts with its requests. Each user request sleeps a specific equal time until the test ends. With the last started user the maximum amount of parallel requests will be reached.
func (HTTPParallelUsers) ServeHTTP ¶
func (hpu HTTPParallelUsers) ServeHTTP(r *http.Request, h http.Handler)
ServeHTTP starts the testing and the request gets called with http.Handler. You might run into a race condition when trying to add a request body (an io.ReadCloser), because multiple reads and writes into the buffer. Use the function ServeHTTPNewRequest() if you need for each call to http.Handler a new request object.
func (HTTPParallelUsers) ServeHTTPNewRequest ¶
func (hpu HTTPParallelUsers) ServeHTTPNewRequest(rf func() *http.Request, h http.Handler)
ServeHTTPNewRequest same as ServeHTTP() but creates for each iteration a new fresh request which will be passed to http.Handler. Does not trigger a race condition.
type HTTPTrip ¶
type HTTPTrip struct { GenerateResponse func(*http.Request) *http.Response Err error sync.Mutex RequestCache map[*http.Request]struct{} }
HTTPTrip used for mocking the Transport field in http.Client.
func NewHTTPTrip ¶
NewHTTPTrip creates a new http.RoundTripper
func NewHTTPTripFromFile ¶
NewHTTPTripFromFile creates a new trip with content found in the provided file. You must close the body and hence close the file.
func (*HTTPTrip) RequestsCount ¶
func (tp *HTTPTrip) RequestsCount(t interface { Errorf(format string, args ...interface{}) }, expected int)
RequestsCount counts the requests in the cache and compares it with your expected value.