98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
|
|
package persistence
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestDefaultRetryWorkerConfig(t *testing.T) {
|
||
|
|
config := DefaultRetryWorkerConfig()
|
||
|
|
|
||
|
|
if config.RetryInterval != 30*time.Second {
|
||
|
|
t.Errorf("expected RetryInterval to be 30s, got %v", config.RetryInterval)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.MaxRetryCount != 5 {
|
||
|
|
t.Errorf("expected MaxRetryCount to be 5, got %d", config.MaxRetryCount)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.BatchSize != 100 {
|
||
|
|
t.Errorf("expected BatchSize to be 100, got %d", config.BatchSize)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.BackoffMultiplier != 2.0 {
|
||
|
|
t.Errorf("expected BackoffMultiplier to be 2.0, got %f", config.BackoffMultiplier)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.InitialBackoff != 1*time.Minute {
|
||
|
|
t.Errorf("expected InitialBackoff to be 1m, got %v", config.InitialBackoff)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRetryWorkerConfig_CustomValues(t *testing.T) {
|
||
|
|
config := RetryWorkerConfig{
|
||
|
|
RetryInterval: 1 * time.Minute,
|
||
|
|
MaxRetryCount: 10,
|
||
|
|
BatchSize: 50,
|
||
|
|
BackoffMultiplier: 3.0,
|
||
|
|
InitialBackoff: 2 * time.Minute,
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.RetryInterval != 1*time.Minute {
|
||
|
|
t.Errorf("expected RetryInterval to be 1m, got %v", config.RetryInterval)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.MaxRetryCount != 10 {
|
||
|
|
t.Errorf("expected MaxRetryCount to be 10, got %d", config.MaxRetryCount)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.BatchSize != 50 {
|
||
|
|
t.Errorf("expected BatchSize to be 50, got %d", config.BatchSize)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.BackoffMultiplier != 3.0 {
|
||
|
|
t.Errorf("expected BackoffMultiplier to be 3.0, got %f", config.BackoffMultiplier)
|
||
|
|
}
|
||
|
|
|
||
|
|
if config.InitialBackoff != 2*time.Minute {
|
||
|
|
t.Errorf("expected InitialBackoff to be 2m, got %v", config.InitialBackoff)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRetryWorker_CalculateNextRetry(t *testing.T) {
|
||
|
|
config := RetryWorkerConfig{
|
||
|
|
InitialBackoff: 1 * time.Minute,
|
||
|
|
BackoffMultiplier: 2.0,
|
||
|
|
}
|
||
|
|
|
||
|
|
worker := &RetryWorker{
|
||
|
|
config: config,
|
||
|
|
}
|
||
|
|
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
retryCount int
|
||
|
|
minDuration time.Duration
|
||
|
|
maxDuration time.Duration
|
||
|
|
}{
|
||
|
|
{"first retry", 0, 1 * time.Minute, 2 * time.Minute},
|
||
|
|
{"second retry", 1, 2 * time.Minute, 3 * time.Minute},
|
||
|
|
{"third retry", 2, 4 * time.Minute, 5 * time.Minute},
|
||
|
|
{"fourth retry", 3, 8 * time.Minute, 9 * time.Minute},
|
||
|
|
}
|
||
|
|
|
||
|
|
now := time.Now()
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
nextRetry := worker.calculateNextRetry(tt.retryCount)
|
||
|
|
duration := nextRetry.Sub(now)
|
||
|
|
|
||
|
|
if duration < tt.minDuration || duration > tt.maxDuration {
|
||
|
|
t.Errorf("retry %d: expected duration between %v and %v, got %v",
|
||
|
|
tt.retryCount, tt.minDuration, tt.maxDuration, duration)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|