Files
LocalAI/core/application/failover_test.go
T
Ettore Di Giacinto ba42a195b4 fix(failover): stop the preload loop from blocking the scheduler goroutine
applyFailoverWarmTargets ran on the manager's single scheduler goroutine
(Sync -> Tick), so a slow or hung PreloadModelByName call froze probing
and fail-back for every chain. Keep the watchdog pin synchronous but run
the preload loop in its own goroutine. Adds a seam (preloadModelByName)
so a unit test can substitute a blocking loader and assert the callback
still returns promptly.

Assisted-by: Claude:claude-opus-5-5
2026-09-26 16:13:04 +00:00

42 lines
1.4 KiB
Go

package application
import (
"context"
"time"
"github.com/mudler/LocalAI/core/config"
"github.com/mudler/LocalAI/pkg/model"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("applyFailoverWarmTargets", func() {
It("returns promptly even when the preload call blocks", func() {
// Guards against a regression to a synchronous preload loop: onWarm
// runs on the failover manager's single scheduler goroutine, so a
// blocking loader here must not block the caller.
started := make(chan struct{})
release := make(chan struct{})
orig := preloadModelByName
preloadModelByName = func(ctx context.Context, cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig, name string) ([]string, error) {
close(started)
<-release // never released within the test's timeout
return nil, nil
}
DeferCleanup(func() { preloadModelByName = orig })
DeferCleanup(func() { close(release) })
app := &Application{applicationConfig: &config.ApplicationConfig{Context: context.Background()}}
callReturned := make(chan struct{})
go func() {
defer GinkgoRecover()
app.applyFailoverWarmTargets([]string{"warm-a"})
close(callReturned)
}()
Eventually(callReturned, time.Second).Should(BeClosed(), "applyFailoverWarmTargets must not wait on the preload goroutine")
Eventually(started, time.Second).Should(BeClosed(), "the preload goroutine should still run in the background")
})
})