servicetest

package
v0.0.0-...-84d73a6 Latest Latest
Warning

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

Go to latest
Published: May 7, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package servicetest contains tools and tests for the service package.

Please do not use any of the types exposed by this package.

To run the tests with code coverage, ensure you pass the packages to 'go test' like so:

-coverpkg=github.com/shabbyrobe/go-service

Because there are so many pieces needed to test all this stuff, the files are rather irritatingly named test_blahblah_test.go. Maybe it can be broken into more packages so this isn't necessary but it'll do for now.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CauseListSorted

func CauseListSorted(err error) (out []error)

func ErrorListSorted

func ErrorListSorted(err error) (out []error)

func FuzzErrs

func FuzzErrs(err error) (out []string)

func ListErrs

func ListErrs(err error) (out []error)

Types

type BlockingService

type BlockingService struct {
	StartFailure error
	StartLimit   int
	StartDelay   time.Duration
	RunFailure   error
	HaltDelay    time.Duration
	// contains filtered or unexported fields
}

BlockingService is a testing service that does no work, but blocks until it is Halted.

func (*BlockingService) Halts

func (d *BlockingService) Halts() int

func (*BlockingService) Init

func (d *BlockingService) Init() *BlockingService

func (*BlockingService) Run

func (d *BlockingService) Run(ctx service.Context) error

func (*BlockingService) Starts

func (d *BlockingService) Starts() int

type ErrorCounter

type ErrorCounter struct {
	// contains filtered or unexported fields
}

func (*ErrorCounter) Add

func (e *ErrorCounter) Add(err error)

func (*ErrorCounter) Clone

func (e *ErrorCounter) Clone() *ErrorCounter

func (*ErrorCounter) Failed

func (e *ErrorCounter) Failed() (out int)

func (*ErrorCounter) MarshalJSON

func (e *ErrorCounter) MarshalJSON() ([]byte, error)

func (*ErrorCounter) Percent

func (e *ErrorCounter) Percent() (out float64)

func (*ErrorCounter) Succeeded

func (e *ErrorCounter) Succeeded() (out int)

func (*ErrorCounter) Total

func (e *ErrorCounter) Total() (out int)

type ErrorService

type ErrorService struct {
	Name       service.Name
	StartDelay time.Duration
	// contains filtered or unexported fields
}

func (*ErrorService) Init

func (d *ErrorService) Init() *ErrorService

func (*ErrorService) Run

func (d *ErrorService) Run(ctx service.Context) error

func (*ErrorService) ServiceName

func (d *ErrorService) ServiceName() service.Name

type FloatRange

type FloatRange struct {
	Min float64
	Max float64
}

func (FloatRange) Rand

func (t FloatRange) Rand() float64

type FloatRangeMaker

type FloatRangeMaker struct {
	Min FloatRange
	Max FloatRange
}

func (FloatRangeMaker) Rand

func (t FloatRangeMaker) Rand() FloatRange

type FuzzError

type FuzzError struct {
	Name  string
	Err   string
	Count int
}

type FuzzServiceStats

type FuzzServiceStats struct {
	ServiceHalt    *ErrorCounter
	ServiceStart   *ErrorCounter
	ServiceRestart *ErrorCounter
	// contains filtered or unexported fields
}

func NewFuzzServiceStats

func NewFuzzServiceStats() *FuzzServiceStats

func (*FuzzServiceStats) AddServiceEnd

func (s *FuzzServiceStats) AddServiceEnd(err error)

func (*FuzzServiceStats) AddServiceError

func (s *FuzzServiceStats) AddServiceError(err error)

func (*FuzzServiceStats) Clone

func (s *FuzzServiceStats) Clone() *FuzzServiceStats

func (*FuzzServiceStats) Errors

func (s *FuzzServiceStats) Errors() (out []FuzzError)

func (*FuzzServiceStats) Map

func (s *FuzzServiceStats) Map() map[string]interface{}

func (*FuzzServiceStats) ServiceEnded

func (s *FuzzServiceStats) ServiceEnded() (out int)

type FuzzStats

type FuzzStats struct {
	Duration        time.Duration
	Seed            int64
	Tick            int32
	RunnersStarted  int32
	RunnersCurrent  int32
	RunnersHalted   int32
	ServicesCurrent int32

	Service           *FuzzServiceStats
	StateCheckResults map[service.State]int
	// contains filtered or unexported fields
}

func NewFuzzStats

func NewFuzzStats() *FuzzStats

func (*FuzzStats) AddRunnersCurrent

func (s *FuzzStats) AddRunnersCurrent(n int)

func (*FuzzStats) AddRunnersHalted

func (s *FuzzStats) AddRunnersHalted(n int)

func (*FuzzStats) AddRunnersStarted

func (s *FuzzStats) AddRunnersStarted(n int)

func (*FuzzStats) AddServicesCurrent

func (s *FuzzStats) AddServicesCurrent(n int)

func (*FuzzStats) AddStateCheckResult

func (s *FuzzStats) AddStateCheckResult(state service.State)

func (*FuzzStats) AddTick

func (s *FuzzStats) AddTick()

func (*FuzzStats) Clone

func (s *FuzzStats) Clone() *FuzzStats

func (*FuzzStats) Ends

func (s *FuzzStats) Ends() int

func (*FuzzStats) Errors

func (s *FuzzStats) Errors() (out []FuzzError)

func (*FuzzStats) GetRunnersCurrent

func (s *FuzzStats) GetRunnersCurrent() int

func (*FuzzStats) GetRunnersHalted

func (s *FuzzStats) GetRunnersHalted() int

func (*FuzzStats) GetRunnersStarted

func (s *FuzzStats) GetRunnersStarted() int

func (*FuzzStats) GetServicesCurrent

func (s *FuzzStats) GetServicesCurrent() int

func (*FuzzStats) GetTick

func (s *FuzzStats) GetTick() int

func (*FuzzStats) Map

func (s *FuzzStats) Map() map[string]interface{}

func (*FuzzStats) MarshalJSON

func (s *FuzzStats) MarshalJSON() ([]byte, error)

func (*FuzzStats) Start

func (s *FuzzStats) Start()

func (*FuzzStats) Starts

func (s *FuzzStats) Starts() int

type IntRange

type IntRange struct {
	Min int
	Max int
}

func (IntRange) Rand

func (t IntRange) Rand() int

type IntRangeMaker

type IntRangeMaker struct {
	Min IntRange
	Max IntRange
}

func (IntRangeMaker) Rand

func (t IntRangeMaker) Rand() IntRange

type Listener

type Listener interface {
	OnServiceEnd() service.OnEnd
	OnServiceError() service.OnError
}

type ListenerCollector

type ListenerCollector struct {
	// contains filtered or unexported fields
}

ListenerCollector allows you to test things that make use of service.Runner's listener.

Please do not use it for any other purpose, it is only built to be useful in a test.

func NewListenerCollector

func NewListenerCollector() *ListenerCollector

func (*ListenerCollector) EndWaiter

func (t *ListenerCollector) EndWaiter(svc *service.Service, cap int) Waiter

func (*ListenerCollector) Ends

func (t *ListenerCollector) Ends(svc *service.Service) (out []ListenerCollectorEnd)

func (*ListenerCollector) ErrWaiter

func (t *ListenerCollector) ErrWaiter(svc *service.Service, cap int) Waiter

func (*ListenerCollector) Errs

func (t *ListenerCollector) Errs(svc *service.Service) (out []error)

func (*ListenerCollector) OnServiceEnd

func (t *ListenerCollector) OnServiceEnd(stage service.Stage, svc *service.Service, err error)

func (*ListenerCollector) OnServiceError

func (t *ListenerCollector) OnServiceError(stage service.Stage, svc *service.Service, err error)

func (*ListenerCollector) RunnerOptions

func (t *ListenerCollector) RunnerOptions(opts ...service.RunnerOption) []service.RunnerOption

func (*ListenerCollector) StateWaiter

func (t *ListenerCollector) StateWaiter(svc *service.Service, cap int) StateWaiter

type ListenerCollectorEnd

type ListenerCollectorEnd struct {
	Stage service.Stage
	Err   error
}

type RunnerFuzzer

type RunnerFuzzer struct {
	RunnerSource RunnerSource

	Duration     time.Duration
	Tick         time.Duration
	RunnerLimit  int
	ServiceLimit int

	RunnerCreateChance float64

	// This is a wonderful mess-maker. It will attempt, in a goroutine,
	// to halt every service it finds in a runner. It does NOT a happy
	// path make.
	RunnerHaltChance float64

	ServiceCreateChance float64

	// Chance that the fuzzer will randomly halt a service, regardless of
	// whether it is an appropriate time:
	ServiceHaltChance float64

	ServiceStartFailureChance float64
	ServiceRunFailureChance   float64

	// Chance that the fuzzer will attempt to restart a random service at a
	// random moment.
	ServiceRestartChance float64

	// Chance that a service started by the fuzzer will be restartable.
	ServiceRestartableChance float64

	// The amount of time the service will take before it calls ctx.Ready().
	ServiceStartTime TimeRange

	// The amount of time the fuzzer will wait for the service to start. If
	// this is shorter than ServiceStartTime, the fuzzer will experience start
	// failures.
	StartWaitTimeout TimeRange

	ServiceRunTime     TimeRange
	ServiceHaltAfter   TimeRange
	ServiceHaltDelay   TimeRange
	ServiceHaltTimeout TimeRange
	StateCheckChance   float64

	Stats *FuzzStats `json:"-"`
	// contains filtered or unexported fields
}

func (*RunnerFuzzer) Init

func (r *RunnerFuzzer) Init(tt assert.T)

func (*RunnerFuzzer) OnServiceEnd

func (r *RunnerFuzzer) OnServiceEnd() service.OnEnd

func (*RunnerFuzzer) OnServiceError

func (r *RunnerFuzzer) OnServiceError() service.OnError

func (*RunnerFuzzer) Run

func (r *RunnerFuzzer) Run(tt assert.T)

type RunnerFuzzerBuilder

type RunnerFuzzerBuilder struct {
	StateCheckChance FloatRange

	RunnerCreateChance FloatRange
	RunnerHaltChance   FloatRange

	ServiceCreateChance FloatRange

	StartWaitChance           FloatRange
	ServiceStartFailureChance FloatRange
	ServiceRunFailureChance   FloatRange

	ServiceStartTime   TimeRangeMaker
	StartWaitTimeout   TimeRangeMaker
	ServiceRunTime     TimeRangeMaker
	ServiceHaltAfter   TimeRangeMaker
	ServiceHaltDelay   TimeRangeMaker
	ServiceHaltTimeout TimeRangeMaker
}

func (RunnerFuzzerBuilder) Next

type RunnerSource

type RunnerSource interface {
	CanHaltRunner() bool
	CanCreateRunner() bool

	FirstRunner(l Listener) service.Runner
	CreateRunner(l Listener) service.Runner

	End()
}

type ServiceRunnerSource

type ServiceRunnerSource struct{}

func (*ServiceRunnerSource) CanCreateRunner

func (srs *ServiceRunnerSource) CanCreateRunner() bool

func (*ServiceRunnerSource) CanHaltRunner

func (srs *ServiceRunnerSource) CanHaltRunner() bool

func (*ServiceRunnerSource) CreateRunner

func (srs *ServiceRunnerSource) CreateRunner(l Listener) service.Runner

func (*ServiceRunnerSource) End

func (srs *ServiceRunnerSource) End()

func (*ServiceRunnerSource) FirstRunner

func (srs *ServiceRunnerSource) FirstRunner(l Listener) service.Runner

type StateWaiter

type StateWaiter interface {
	TakeN(n int, timeout time.Duration) []service.StateChange
	Take(timeout time.Duration) *service.StateChange
	C() <-chan service.StateChange
}

type TimeRange

type TimeRange struct {
	Min time.Duration
	Max time.Duration
}

func (TimeRange) Rand

func (t TimeRange) Rand() time.Duration

type TimeRangeMaker

type TimeRangeMaker struct {
	Min TimeRange
	Max TimeRange
}

func (TimeRangeMaker) Rand

func (t TimeRangeMaker) Rand() TimeRange

type TimedService

type TimedService struct {
	StartFailure    error
	StartDelay      time.Duration
	StartLimit      int
	RunFailure      error
	RunTime         time.Duration
	HaltDelay       time.Duration
	UnhaltableSleep bool
	// contains filtered or unexported fields
}

func (*TimedService) Halts

func (d *TimedService) Halts() int

func (*TimedService) Init

func (d *TimedService) Init() *TimedService

func (*TimedService) Run

func (d *TimedService) Run(ctx service.Context) error

func (*TimedService) Starts

func (d *TimedService) Starts() int

type UnhaltableService

type UnhaltableService struct {
	Name service.Name
	// contains filtered or unexported fields
}

UnhaltableService is a defective service implementation for testing purposes.

It deliberately ignores service.Context.Done() and service.Context.ShouldHalt().

To stop it properly and ensure you are not leaking resources, use the Kill() function.

func (*UnhaltableService) Init

func (*UnhaltableService) Kill

func (u *UnhaltableService) Kill()

func (*UnhaltableService) Run

func (u *UnhaltableService) Run(ctx service.Context) error

type Waiter

type Waiter interface {
	TakeN(n int, timeout time.Duration) []error
	Take(timeout time.Duration) error
	C() <-chan error
}

Jump to

Keyboard shortcuts

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