Move unit tests from tests/unit/ to internal/ following Go conventions: - tests/unit/logging/* -> internal/logging/* (logging tests) - tests/unit/manifest/* -> internal/manifest/* (run_manifest, schema tests) - tests/unit/network/* -> internal/network/* (retry, ssh_pool, ssh tests) - tests/unit/privacy/* -> internal/privacy/* (pii tests) - tests/unit/metrics/* -> internal/prommetrics/* (metrics tests) Update import paths in test files to reflect new locations. Note: metrics_test.go moved from tests/unit/metrics/ to internal/prommetrics/ to match the actual package name.
126 lines
2.6 KiB
Go
126 lines
2.6 KiB
Go
package network_test
|
|
|
|
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)
|
|
}
|
|
}
|