Documentation
¶
Overview ¶
Package testutil provides testing utilities for the Surge download manager.
Index ¶
- func AssertDownloadSuccess(result DownloadResult, expectedBytes int64) error
- func CompareFiles(path1, path2 string) (bool, error)
- func CreateSurgeFile(dir, name string, totalSize, downloadedSize int64) (string, error)
- func CreateTestFile(dir, name string, size int64, random bool) (string, error)
- func FileExists(path string) bool
- func NewHTTPServer(handler http.Handler) *httptest.Server
- func NewHTTPServerT(t *testing.T, handler http.Handler) *httptest.Server
- func ReadFileChunk(path string, offset, length int64) ([]byte, error)
- func SeedMasterList(t *testing.T, entry types.DownloadEntry)
- func SetupStateDB(t *testing.T) string
- func SuppressNotificationsInTests()
- func TempDir(prefix string) (string, func(), error)
- func VerifyFileSize(path string, expectedSize int64) error
- type DownloadResult
- type FileSizeMismatchError
- type MockServer
- type MockServerOption
- func WithByteLatency(d time.Duration) MockServerOption
- func WithContentType(ct string) MockServerOption
- func WithFailAfterBytes(n int64) MockServerOption
- func WithFailOnNthRequest(n int) MockServerOption
- func WithFileSize(size int64) MockServerOption
- func WithFilename(name string) MockServerOption
- func WithHandler(h http.HandlerFunc) MockServerOption
- func WithLatency(d time.Duration) MockServerOption
- func WithMaxConcurrentRequests(n int) MockServerOption
- func WithRandomData(random bool) MockServerOption
- func WithRangeSupport(enabled bool) MockServerOption
- type MockServerStats
- type StreamingMockServer
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 ¶
CompareFiles checks if two files have identical content.
func CreateSurgeFile ¶
CreateSurgeFile creates a .surge partial download file for resume testing.
func CreateTestFile ¶
CreateTestFile creates a test file with the specified size filled with either zeros or random data.
func NewHTTPServer ¶
NewHTTPServer starts an httptest server bound to IPv4 to avoid IPv6 listener issues in sandboxed environments.
func NewHTTPServerT ¶
NewHTTPServerT starts an httptest server bound to IPv4 and skips the test if binding fails.
func ReadFileChunk ¶
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 ¶
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 VerifyFileSize ¶
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 ¶
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) Stats ¶
func (m *MockServer) Stats() MockServerStats
Stats returns a summary of server statistics.
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.