cstesting

package
v0.0.0-...-202847b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package cstesting provides testing helpers

Index

Constants

View Source
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 Close

func Close(t errorFormatter, c io.Closer)

Close for usage in conjunction with defer.

defer cstesting.Close(t, con)

func ContainsCount

func ContainsCount(t errorFormatter, str, contains string, maxOccurrences int)

ContainsCount checks if str contains the substring contains maxOccurrences times.

func GCPause

func GCPause() time.Duration

GCPause runs the garbage collector and measures the time taken.

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

func NewHTTPTrip(code int, body string, err error) *HTTPTrip

NewHTTPTrip creates a new http.RoundTripper

func NewHTTPTripFromFile

func NewHTTPTripFromFile(code int, filename string) *HTTPTrip

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.

func (*HTTPTrip) RequestsMatchAll

func (tp *HTTPTrip) RequestsMatchAll(t interface {
	Errorf(format string, args ...interface{})
}, f func(*http.Request) bool)

RequestsMatchAll checks if all requests in the cache matches the predicate function f.

func (*HTTPTrip) RoundTrip

func (tp *HTTPTrip) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper and adds the Request to the field Req for later inspection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL