Simplify LRU eviction tests

This commit is contained in:
2025-08-31 11:46:16 +02:00
parent 447f441fd0
commit 9579930a6a

View File

@@ -185,149 +185,117 @@ func TestEvictLRUInstance_Success(t *testing.T) {
} }
func TestEvictLRUInstance_NoEligibleInstances(t *testing.T) { func TestEvictLRUInstance_NoEligibleInstances(t *testing.T) {
manager := createTestManager() // Helper function to create instances with different timeout configurations
defer manager.Shutdown() createInstanceWithTimeout := func(manager manager.InstanceManager, name, model string, timeout *int) *instance.Process {
options := &instance.CreateInstanceOptions{
// Test 1: No running instances LlamaServerOptions: llamacpp.LlamaServerOptions{
err := manager.EvictLRUInstance() Model: model,
if err == nil { },
t.Error("Expected error when no running instances exist") IdleTimeout: timeout,
} }
if err.Error() != "failed to find lru instance" { inst, err := manager.CreateInstance(name, options)
t.Errorf("Expected 'failed to find lru instance' error, got: %v", err) if err != nil {
t.Fatalf("CreateInstance failed: %v", err)
}
return inst
} }
// Test 2: Only instances with IdleTimeout <= 0 t.Run("no running instances", func(t *testing.T) {
options1 := &instance.CreateInstanceOptions{ manager := createTestManager()
LlamaServerOptions: llamacpp.LlamaServerOptions{ defer manager.Shutdown()
Model: "/path/to/model1.gguf",
},
IdleTimeout: func() *int { timeout := 0; return &timeout }(), // 0 = no timeout
}
options2 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model2.gguf",
},
IdleTimeout: func() *int { timeout := -1; return &timeout }(), // negative = no timeout
}
options3 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model3.gguf",
},
// No IdleTimeout set (nil)
}
inst1, err := manager.CreateInstance("no-timeout-1", options1) err := manager.EvictLRUInstance()
if err != nil { if err == nil {
t.Fatalf("CreateInstance failed: %v", err) t.Error("Expected error when no running instances exist")
} }
inst2, err := manager.CreateInstance("no-timeout-2", options2) if err.Error() != "failed to find lru instance" {
if err != nil { t.Errorf("Expected 'failed to find lru instance' error, got: %v", err)
t.Fatalf("CreateInstance failed: %v", err) }
} })
inst3, err := manager.CreateInstance("no-timeout-3", options3)
if err != nil {
t.Fatalf("CreateInstance failed: %v", err)
}
// Set instances to running t.Run("only instances without timeout", func(t *testing.T) {
inst1.SetStatus(instance.Running) manager := createTestManager()
inst2.SetStatus(instance.Running) defer manager.Shutdown()
inst3.SetStatus(instance.Running)
// Try to evict - should fail because no eligible instances // Create instances with various non-eligible timeout configurations
err = manager.EvictLRUInstance() zeroTimeout := 0
if err == nil { negativeTimeout := -1
t.Error("Expected error when no eligible instances exist") inst1 := createInstanceWithTimeout(manager, "no-timeout-1", "/path/to/model1.gguf", &zeroTimeout)
} inst2 := createInstanceWithTimeout(manager, "no-timeout-2", "/path/to/model2.gguf", &negativeTimeout)
if err.Error() != "failed to find lru instance" { inst3 := createInstanceWithTimeout(manager, "no-timeout-3", "/path/to/model3.gguf", nil)
t.Errorf("Expected 'failed to find lru instance' error, got: %v", err)
}
// Verify all instances are still running // Set instances to running
if !inst1.IsRunning() { instances := []*instance.Process{inst1, inst2, inst3}
t.Error("Expected no-timeout-1 to still be running") for _, inst := range instances {
} inst.SetStatus(instance.Running)
if !inst2.IsRunning() { }
t.Error("Expected no-timeout-2 to still be running") defer func() {
} // Reset instances to stopped to avoid shutdown panics
if !inst3.IsRunning() { for _, inst := range instances {
t.Error("Expected no-timeout-3 to still be running") inst.SetStatus(instance.Stopped)
} }
}()
// Reset instances to stopped to avoid shutdown panics // Try to evict - should fail because no eligible instances
inst1.SetStatus(instance.Stopped) err := manager.EvictLRUInstance()
inst2.SetStatus(instance.Stopped) if err == nil {
inst3.SetStatus(instance.Stopped) t.Error("Expected error when no eligible instances exist")
} }
if err.Error() != "failed to find lru instance" {
t.Errorf("Expected 'failed to find lru instance' error, got: %v", err)
}
func TestEvictLRUInstance_SkipsInstancesWithoutTimeout(t *testing.T) { // Verify all instances are still running
manager := createTestManager() for i, inst := range instances {
defer manager.Shutdown() if !inst.IsRunning() {
t.Errorf("Expected instance %d to still be running", i+1)
}
}
})
// Create mix of instances: some with timeout enabled, some disabled t.Run("mixed instances - evicts only eligible ones", func(t *testing.T) {
optionsWithTimeout := &instance.CreateInstanceOptions{ manager := createTestManager()
LlamaServerOptions: llamacpp.LlamaServerOptions{ defer manager.Shutdown()
Model: "/path/to/model-with-timeout.gguf",
},
IdleTimeout: func() *int { timeout := 1; return &timeout }(), // Any value > 0
}
optionsWithoutTimeout1 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model-no-timeout1.gguf",
},
IdleTimeout: func() *int { timeout := 0; return &timeout }(), // 0 = no timeout
}
optionsWithoutTimeout2 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model-no-timeout2.gguf",
},
// No IdleTimeout set (nil)
}
instWithTimeout, err := manager.CreateInstance("with-timeout", optionsWithTimeout) // Create mix of instances: some with timeout enabled, some disabled
if err != nil { validTimeout := 1
t.Fatalf("CreateInstance failed: %v", err) zeroTimeout := 0
} instWithTimeout := createInstanceWithTimeout(manager, "with-timeout", "/path/to/model-with-timeout.gguf", &validTimeout)
instNoTimeout1, err := manager.CreateInstance("no-timeout-1", optionsWithoutTimeout1) instNoTimeout1 := createInstanceWithTimeout(manager, "no-timeout-1", "/path/to/model-no-timeout1.gguf", &zeroTimeout)
if err != nil { instNoTimeout2 := createInstanceWithTimeout(manager, "no-timeout-2", "/path/to/model-no-timeout2.gguf", nil)
t.Fatalf("CreateInstance failed: %v", err)
}
instNoTimeout2, err := manager.CreateInstance("no-timeout-2", optionsWithoutTimeout2)
if err != nil {
t.Fatalf("CreateInstance failed: %v", err)
}
// Set all instances to running // Set all instances to running
instWithTimeout.SetStatus(instance.Running) instances := []*instance.Process{instWithTimeout, instNoTimeout1, instNoTimeout2}
instNoTimeout1.SetStatus(instance.Running) for _, inst := range instances {
instNoTimeout2.SetStatus(instance.Running) inst.SetStatus(instance.Running)
inst.UpdateLastRequestTime()
}
defer func() {
// Reset instances to stopped to avoid shutdown panics
for _, inst := range instances {
if inst.IsRunning() {
inst.SetStatus(instance.Stopped)
}
}
}()
// Update request times // Evict LRU instance - should only consider the one with timeout
instWithTimeout.UpdateLastRequestTime() err := manager.EvictLRUInstance()
instNoTimeout1.UpdateLastRequestTime() if err != nil {
instNoTimeout2.UpdateLastRequestTime() t.Fatalf("EvictLRUInstance failed: %v", err)
}
// Evict LRU instance - should only consider the one with timeout // Verify only the instance with timeout was evicted
err = manager.EvictLRUInstance() if instWithTimeout.IsRunning() {
if err != nil { t.Error("Expected with-timeout instance to be stopped after eviction")
t.Fatalf("EvictLRUInstance failed: %v", err) }
} if !instNoTimeout1.IsRunning() {
t.Error("Expected no-timeout-1 instance to still be running")
// Verify only the instance with timeout was evicted }
if instWithTimeout.IsRunning() { if !instNoTimeout2.IsRunning() {
t.Error("Expected with-timeout instance to be stopped after eviction") t.Error("Expected no-timeout-2 instance to still be running")
} }
if !instNoTimeout1.IsRunning() { })
t.Error("Expected no-timeout-1 instance to still be running")
}
if !instNoTimeout2.IsRunning() {
t.Error("Expected no-timeout-2 instance to still be running")
}
// Reset instances to stopped to avoid shutdown panics
instNoTimeout1.SetStatus(instance.Stopped)
instNoTimeout2.SetStatus(instance.Stopped)
} }
// Helper for timeout tests // Helper for timeout tests