fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Further pursue of perfection.

This commit is contained in:
2025-09-06 03:12:59 +01:00
parent f96940921b
commit 05c316002d
15 changed files with 4647 additions and 99 deletions
+77 -12
View File
@@ -1,8 +1,10 @@
package traefikoidc
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
@@ -34,30 +36,93 @@ func TestGlobalCleanupMechanism(t *testing.T) {
})
t.Run("Background task cleanup", func(t *testing.T) {
// Skip in short mode since it tests internal mechanics and can be flaky in full suite
if testing.Short() {
t.Skip("Skipping background task cleanup test in short mode")
}
// Create a unique task name with timestamp to avoid conflicts with other tests
taskName := fmt.Sprintf("test-task-%d", time.Now().UnixNano())
var taskRuns int64
taskStarted := make(chan bool, 1)
taskReady := make(chan bool, 1)
// Create a test-specific logger to avoid noise
logger := GetSingletonNoOpLogger()
task := NewBackgroundTask(
"test-task",
taskName,
10*time.Millisecond,
func() { atomic.AddInt64(&taskRuns, 1) },
nil,
func() {
atomic.AddInt64(&taskRuns, 1)
select {
case taskStarted <- true:
default:
}
},
logger,
)
// Reset circuit breaker state manually for test tasks before starting
registry := GetGlobalTaskRegistry()
// Clear any existing test tasks from the registry to avoid conflicts
registry.mu.Lock()
for name, task := range registry.tasks {
if strings.Contains(name, "test-task") {
task.Stop()
delete(registry.tasks, name)
}
}
registry.mu.Unlock()
// Register task for cleanup
globalCleanup.RegisterTask(task)
// Start the task
task.Start()
// Start the task in a goroutine to avoid blocking
go func() {
task.Start()
taskReady <- true
}()
// Let it run a few times
time.Sleep(50 * time.Millisecond)
runs := atomic.LoadInt64(&taskRuns)
if runs == 0 {
t.Error("Background task should have run at least once")
// Wait for task to be ready
select {
case <-taskReady:
// Task is started
case <-time.After(100 * time.Millisecond):
t.Fatal("Task failed to start")
}
t.Logf("Background task ran %d times", runs)
// Wait for task to start running with increased timeout for busy test environments
select {
case <-taskStarted:
// Task has run at least once
case <-time.After(1000 * time.Millisecond): // Increased from 500ms to 1000ms
// Don't fail immediately - check if task is running by looking at the run count
runs := atomic.LoadInt64(&taskRuns)
if runs == 0 {
t.Logf("Task may be slow to start in test environment, waiting longer...")
// Wait a bit more
time.Sleep(300 * time.Millisecond) // Increased from 200ms to 300ms
runs = atomic.LoadInt64(&taskRuns)
if runs == 0 {
t.Fatal("Task did not start after extended timeout")
}
}
}
// Let it run a few more times with longer duration for more stable results
time.Sleep(100 * time.Millisecond) // Increased from 50ms to 100ms
runs := atomic.LoadInt64(&taskRuns)
// More lenient expectations: allow 3-10 executions instead of just checking > 0
if runs < 3 {
t.Errorf("Background task should have run at least 3 times, got %d", runs)
} else if runs > 15 { // Upper bound to catch runaway tasks
t.Errorf("Background task ran too many times (%d), possible issue with cleanup", runs)
}
t.Logf("Background task ran %d times (expected 3-15)", runs)
})
t.Run("Cache cleanup", func(t *testing.T) {