esitesting

package
v0.0.0-...-004e0d7 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package esitesting dedicated package for testing caddyesi

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 KillZombieProcess

func KillZombieProcess(processName16 string)

KillZombieProcess searches a running process by its name and kills it. Writes the success to Stderr. Panics on errors.

func MockRequestContent

func MockRequestContent(content string) esitag.ResourceHandler

MockRequestContent for testing purposes only.

func MockRequestContentCB

func MockRequestContentCB(content string, callback func() error) esitag.ResourceHandler

MockRequestContentCB for testing purposes only. Call back gets executed before the function returns.

func MockRequestError

func MockRequestError(err error) esitag.ResourceHandler

MockRequestError for testing purposes only.

func MockRequestPanic

func MockRequestPanic(msg interface{}) esitag.ResourceHandler

MockRequestPanic just panics

func StartProcess

func StartProcess(name string, arg ...string) *exec.Cmd

StartProcess starts a process and returns a cleanup function which kills the process. Panics on error.

func Tempfile

func Tempfile(t interface {
	Fatal(args ...interface{})
}) (fileName string, clean func())

Tempfile returns a temporary file path.

func WriteXMLTempFile

func WriteXMLTempFile(t interface {
	Fatal(args ...interface{})
}, body io.WriterTo) (fileName string, clean func())

WriteXMLTempFile writes the body into the file and returns the file name and a clean up function. The filename always has the suffix ".xml".

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, code int, serveErr error)
}

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 httpserver.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 httpserver.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 NewHTTPTripBytes

func NewHTTPTripBytes(code int, body []byte, err error) *HTTPTrip

NewHTTPTripBytes creates a new http.RoundTripper but body is a byte slice. In benchmarks this performs better with large bodies.

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.

type ResourceMock

type ResourceMock struct {
	DoRequestFn func(args *esitag.ResourceArgs) (http.Header, []byte, error)
	CloseFn     func() error
}

ResourceMock exported for testing

func (ResourceMock) Close

func (rm ResourceMock) Close() error

Close returns nil if CloseFn is nil otherwise calls CloseFn

func (ResourceMock) DoRequest

func (rm ResourceMock) DoRequest(a *esitag.ResourceArgs) (http.Header, []byte, error)

DoRequest calls DoRequestFn

Jump to

Keyboard shortcuts

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