fetch_ml/tests/unit/network/retry_test.go
Jeremie Fraeys c980167041 test: implement comprehensive test suite with multiple test types
- Add end-to-end tests for complete workflow validation
- Include integration tests for API and database interactions
- Add unit tests for all major components and utilities
- Include performance tests for payload handling
- Add CLI API integration tests
- Include Podman container integration tests
- Add WebSocket and queue execution tests
- Include shell script tests for setup validation

Provides comprehensive test coverage ensuring platform reliability
and functionality across all components and interactions.
2025-12-04 16:55:13 -05:00

126 lines
2.6 KiB
Go

package tests
import (
"context"
"errors"
"testing"
"time"
"github.com/jfraeys/fetch_ml/internal/network"
)
func TestRetry_Success(t *testing.T) {
t.Parallel() // Enable parallel execution
ctx := context.Background()
cfg := network.DefaultRetryConfig()
attempts := 0
err := network.Retry(ctx, cfg, func() error {
attempts++
if attempts < 2 {
return errors.New("temporary failure")
}
return nil
})
if err != nil {
t.Errorf("Expected success, got error: %v", err)
}
if attempts != 2 {
t.Errorf("Expected 2 attempts, got %d", attempts)
}
}
func TestRetry_MaxAttempts(t *testing.T) {
t.Parallel() // Enable parallel execution
ctx := context.Background()
cfg := network.RetryConfig{
MaxAttempts: 3,
InitialDelay: 10 * time.Millisecond,
MaxDelay: 100 * time.Millisecond,
Multiplier: 2.0,
}
attempts := 0
err := network.Retry(ctx, cfg, func() error {
attempts++
return errors.New("always fails")
})
if err == nil {
t.Error("Expected error after max attempts")
}
if attempts != 3 {
t.Errorf("Expected 3 attempts, got %d", attempts)
}
}
func TestRetry_ContextCancellation(t *testing.T) {
t.Parallel() // Enable parallel execution
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
cfg := network.DefaultRetryConfig()
attempts := 0
err := network.Retry(ctx, cfg, func() error {
attempts++
time.Sleep(20 * time.Millisecond) // Simulate work
return errors.New("always fails")
})
if err != context.DeadlineExceeded {
t.Errorf("Expected context deadline exceeded, got: %v", err)
}
// Should have attempted at least once but not all attempts due to timeout
if attempts == 0 {
t.Error("Expected at least one attempt")
}
}
func TestRetryWithBackoff(t *testing.T) {
t.Parallel() // Enable parallel execution
ctx := context.Background()
attempts := 0
err := network.RetryWithBackoff(ctx, 3, func() error {
attempts++
if attempts < 3 {
return errors.New("temporary failure")
}
return nil
})
if err != nil {
t.Errorf("Expected success, got error: %v", err)
}
if attempts != 3 {
t.Errorf("Expected 3 attempts, got %d", attempts)
}
}
func TestRetryForNetworkOperations(t *testing.T) {
t.Parallel() // Enable parallel execution
ctx := context.Background()
attempts := 0
err := network.RetryForNetworkOperations(ctx, func() error {
attempts++
if attempts < 5 {
return errors.New("network error")
}
return nil
})
if err != nil {
t.Errorf("Expected success, got error: %v", err)
}
if attempts != 5 {
t.Errorf("Expected 5 attempts, got %d", attempts)
}
}