testing

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package testing provides error simulation tools for testing download scenarios.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateErrorServer

func CreateErrorServer(statusCode int, responseBody string) *httptest.Server

CreateErrorServer creates a server that returns a specific HTTP error.

func CreatePartialResponseServer

func CreatePartialResponseServer(totalSize, sendSize int64) *httptest.Server

CreatePartialResponseServer creates a server that sends partial responses.

func CreateTimeoutServer

func CreateTimeoutServer(delay time.Duration) *httptest.Server

CreateTimeoutServer creates a server that times out.

Types

type DiskSpaceSimulator

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

DiskSpaceSimulator simulates disk space exhaustion scenarios.

func NewDiskSpaceSimulator

func NewDiskSpaceSimulator(spaceLimit int64) *DiskSpaceSimulator

NewDiskSpaceSimulator creates a disk space simulator.

func (*DiskSpaceSimulator) CreateLimitedWriter

func (dss *DiskSpaceSimulator) CreateLimitedWriter(w io.Writer) io.Writer

CreateLimitedWriter returns a writer that simulates disk space exhaustion.

func (*DiskSpaceSimulator) GetBytesWritten

func (dss *DiskSpaceSimulator) GetBytesWritten() int64

GetBytesWritten returns the total bytes written.

func (*DiskSpaceSimulator) Reset

func (dss *DiskSpaceSimulator) Reset()

Reset resets the bytes written counter.

type ErrorSimulator

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

ErrorSimulator provides comprehensive error simulation capabilities for testing download scenarios including network failures, disk space issues, HTTP errors, and timeout conditions.

func NewErrorSimulator

func NewErrorSimulator(config *SimulationConfig) *ErrorSimulator

NewErrorSimulator creates a new error simulator with the given configuration.

func (*ErrorSimulator) Close

func (es *ErrorSimulator) Close()

Close shuts down the error simulator.

func (*ErrorSimulator) GetEndpoint

func (es *ErrorSimulator) GetEndpoint(path string) string

GetEndpoint returns a specific endpoint URL.

func (*ErrorSimulator) GetURL

func (es *ErrorSimulator) GetURL() string

GetURL returns the base URL of the test server.

type HTTPErrorSimulator

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

HTTPErrorSimulator provides utilities for testing HTTP error responses.

func NewHTTPErrorSimulator

func NewHTTPErrorSimulator() *HTTPErrorSimulator

NewHTTPErrorSimulator creates an HTTP error simulator.

func (*HTTPErrorSimulator) Close

func (hes *HTTPErrorSimulator) Close()

Close shuts down the HTTP error simulator.

func (*HTTPErrorSimulator) GetURL

func (hes *HTTPErrorSimulator) GetURL() string

GetURL returns the server URL.

type NetworkFailureSimulator

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

NetworkFailureSimulator simulates various network failure scenarios.

func NewNetworkFailureSimulator

func NewNetworkFailureSimulator() *NetworkFailureSimulator

NewNetworkFailureSimulator creates a network failure simulator.

func (*NetworkFailureSimulator) CreateFailingDialer

func (nfs *NetworkFailureSimulator) CreateFailingDialer(
	failureType string,
) func(context.Context, string, string) (net.Conn, error)

CreateFailingDialer returns a dialer that simulates network failures.

type SimulationConfig

type SimulationConfig struct {
	// NetworkLatency adds artificial latency to responses
	NetworkLatency time.Duration

	// FailureRate determines the probability of failures (0.0 to 1.0)
	FailureRate float64

	// MaxResponseSize limits response body size
	MaxResponseSize int64

	// ChunkDelay adds delay between response chunks
	ChunkDelay time.Duration

	// TimeoutAfter causes requests to timeout after this duration
	TimeoutAfter time.Duration

	// HTTPStatusCode overrides the HTTP status code to return
	HTTPStatusCode int

	// FailAfterBytes causes failure after reading a certain number of bytes
	FailAfterBytes int64

	// DiskSpaceLimit simulates insufficient disk space
	DiskSpaceLimit uint64

	// EnableConnectionReset simulates connection resets
	EnableConnectionReset bool

	// EnableDNSFailure simulates DNS resolution failures
	EnableDNSFailure bool

	// SlowResponseRate limits response byte rate (bytes per second)
	SlowResponseRate int64
}

SimulationConfig configures the error simulation behavior.

func DefaultSimulationConfig

func DefaultSimulationConfig() *SimulationConfig

DefaultSimulationConfig returns a basic simulation configuration.

type SimulatorSuite

type SimulatorSuite struct {
	ErrorSim     *ErrorSimulator
	NetworkSim   *NetworkFailureSimulator
	DiskSpaceSim *DiskSpaceSimulator
	HTTPSim      *HTTPErrorSimulator
}

SimulatorSuite combines all simulators for comprehensive testing.

func NewSimulatorSuite

func NewSimulatorSuite() *SimulatorSuite

NewSimulatorSuite creates a complete simulator suite.

func (*SimulatorSuite) Close

func (ss *SimulatorSuite) Close()

Close shuts down all simulators in the suite.

type TestScenario

type TestScenario struct {
	Name              string
	NetworkFailure    string           // Type of network failure to simulate
	HTTPStatusCode    int              // HTTP status code to return
	DiskSpaceLimit    int64            // Disk space limit in bytes
	TimeoutAfter      time.Duration    // Timeout duration
	PartialFailureAt  int64            // Bytes after which to fail
	ExpectedErrorCode errors.ErrorCode // Expected error code
	ExpectedRetryable bool             // Whether error should be retryable
}

TestScenario represents a complete test scenario with multiple error conditions.

func GetCommonTestScenarios

func GetCommonTestScenarios() []TestScenario

GetCommonTestScenarios returns a set of common error scenarios for testing.

Jump to

Keyboard shortcuts

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