testutil

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package testutil provides testing utilities for the Surge download manager.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertDownloadSuccess

func AssertDownloadSuccess(result DownloadResult, expectedBytes int64) error

AssertDownloadSuccess is a test helper that checks download success.

func CompareFiles

func CompareFiles(path1, path2 string) (bool, error)

CompareFiles checks if two files have identical content.

func CreateSurgeFile

func CreateSurgeFile(dir, name string, totalSize, downloadedSize int64) (string, error)

CreateSurgeFile creates a .surge partial download file for resume testing.

func CreateTestFile

func CreateTestFile(dir, name string, size int64, random bool) (string, error)

CreateTestFile creates a test file with the specified size filled with either zeros or random data.

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists.

func NewHTTPServer

func NewHTTPServer(handler http.Handler) *httptest.Server

NewHTTPServer starts an httptest server bound to IPv4 to avoid IPv6 listener issues in sandboxed environments.

func NewHTTPServerT

func NewHTTPServerT(t *testing.T, handler http.Handler) *httptest.Server

NewHTTPServerT starts an httptest server bound to IPv4 and skips the test if binding fails.

func ReadFileChunk

func ReadFileChunk(path string, offset, length int64) ([]byte, error)

ReadFileChunk reads a specific byte range from a file.

func SeedMasterList added in v0.7.8

func SeedMasterList(t *testing.T, entry types.DownloadEntry)

SeedMasterList inserts a DownloadEntry into the master list for test setups.

func SetupStateDB

func SetupStateDB(t *testing.T) string

SetupStateDB configures a fresh temp SQLite DB for tests that exercise state persistence.

func SuppressNotificationsInTests added in v0.7.8

func SuppressNotificationsInTests()

SuppressNotificationsInTests disables desktop notifications for any test that imports this package. Call this from a per-package init() or TestMain.

func TempDir

func TempDir(prefix string) (string, func(), error)

TempDir creates a temporary directory for test files and returns a cleanup function.

func VerifyFileSize

func VerifyFileSize(path string, expectedSize int64) error

VerifyFileSize checks if a file has the expected size.

Types

type DownloadResult

type DownloadResult struct {
	Error       error
	BytesRead   int64
	Duration    time.Duration
	Resumed     bool
	Connections int
}

DownloadResult represents the result of a download operation for testing.

type FileSizeMismatchError

type FileSizeMismatchError struct {
	Path     string
	Expected int64
	Actual   int64
}

FileSizeMismatchError indicates a file size doesn't match expected.

func (*FileSizeMismatchError) Error

func (e *FileSizeMismatchError) Error() string

type MockServer

type MockServer struct {
	Server *httptest.Server

	// Configuration
	FileSize          int64         // Size of the served file
	SupportsRanges    bool          // Whether to support HTTP Range requests
	ContentType       string        // Content-Type header value
	Filename          string        // Filename in Content-Disposition header
	RandomData        bool          // If true, serve random data; otherwise serve zeros
	Latency           time.Duration // Artificial latency per request
	ByteLatency       time.Duration // Latency per byte (simulates slow connection)
	FailAfterBytes    int64         // Fail connection after this many bytes (0 = no fail)
	FailOnNthRequest  int           // Fail on Nth request (0 = don't fail)
	MaxConcurrentReqs int           // Max concurrent requests (0 = unlimited)

	// Tracking
	RequestCount   atomic.Int64
	BytesServed    atomic.Int64
	ActiveRequests atomic.Int64
	RangeRequests  atomic.Int64
	FullRequests   atomic.Int64
	FailedRequests atomic.Int64

	CustomHandler http.HandlerFunc
	// contains filtered or unexported fields
}

MockServer is a configurable HTTP test server for download testing.

func NewMockServer

func NewMockServer(opts ...MockServerOption) *MockServer

NewMockServer creates a new mock HTTP server with the given options.

func NewMockServerT

func NewMockServerT(t *testing.T, opts ...MockServerOption) *MockServer

NewMockServerT creates a new mock HTTP server and skips the test if binding fails.

func (*MockServer) Close

func (m *MockServer) Close()

Close shuts down the mock server.

func (*MockServer) Reset

func (m *MockServer) Reset()

Reset clears all tracking counters.

func (*MockServer) Stats

func (m *MockServer) Stats() MockServerStats

Stats returns a summary of server statistics.

func (*MockServer) URL

func (m *MockServer) URL() string

URL returns the server's URL.

type MockServerOption

type MockServerOption func(*MockServer)

MockServerOption is a function that configures a MockServer.

func WithByteLatency

func WithByteLatency(d time.Duration) MockServerOption

WithByteLatency adds artificial latency per byte served.

func WithContentType

func WithContentType(ct string) MockServerOption

WithContentType sets the Content-Type header.

func WithFailAfterBytes

func WithFailAfterBytes(n int64) MockServerOption

WithFailAfterBytes causes the connection to fail after serving N bytes.

func WithFailOnNthRequest

func WithFailOnNthRequest(n int) MockServerOption

WithFailOnNthRequest causes the Nth request to fail.

func WithFileSize

func WithFileSize(size int64) MockServerOption

WithFileSize sets the file size to serve.

func WithFilename

func WithFilename(name string) MockServerOption

WithFilename sets the filename in Content-Disposition header.

func WithHandler

func WithHandler(h http.HandlerFunc) MockServerOption

WithHandler sets a custom request handler.

func WithLatency

func WithLatency(d time.Duration) MockServerOption

WithLatency adds artificial latency per request.

func WithMaxConcurrentRequests

func WithMaxConcurrentRequests(n int) MockServerOption

WithMaxConcurrentRequests limits concurrent requests.

func WithRandomData

func WithRandomData(random bool) MockServerOption

WithRandomData enables serving random bytes instead of zeros.

func WithRangeSupport

func WithRangeSupport(enabled bool) MockServerOption

WithRangeSupport enables or disables Range request support.

type MockServerStats

type MockServerStats struct {
	TotalRequests  int64
	BytesServed    int64
	RangeRequests  int64
	FullRequests   int64
	FailedRequests int64
}

MockServerStats contains server statistics.

type StreamingMockServer

type StreamingMockServer struct {
	*MockServer
}

StreamingMockServer provides a variant that generates data on-the-fly instead of pre-allocating, useful for very large file simulations.

func NewStreamingMockServer

func NewStreamingMockServer(fileSize int64, opts ...MockServerOption) *StreamingMockServer

NewStreamingMockServer creates a mock server that streams generated data.

func NewStreamingMockServerT

func NewStreamingMockServerT(t *testing.T, fileSize int64, opts ...MockServerOption) *StreamingMockServer

NewStreamingMockServerT creates a streaming mock server and skips the test if binding fails.

Jump to

Keyboard shortcuts

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