@@ -14,6 +14,7 @@ import (
1414 "testing"
1515 "time"
1616
17+ "github.com/mostlygeek/llama-swap/event"
1718 "github.com/stretchr/testify/assert"
1819 "github.com/tidwall/gjson"
1920)
@@ -832,3 +833,62 @@ func TestProxyManager_HealthEndpoint(t *testing.T) {
832833 assert .Equal (t , http .StatusOK , rec .Code )
833834 assert .Equal (t , "OK" , rec .Body .String ())
834835}
836+
837+ func TestProxyManager_StartupHooks (t * testing.T ) {
838+
839+ // using real YAML as the configuration has gotten more complex
840+ // is the right approach as LoadConfigFromReader() does a lot more
841+ // than parse YAML now. Eventually migrate all tests to use this approach
842+ configStr := strings .Replace (`
843+ logLevel: error
844+ hooks:
845+ on_startup:
846+ preload:
847+ - model1
848+ - model2
849+ groups:
850+ preloadTestGroup:
851+ swap: false
852+ members:
853+ - model1
854+ - model2
855+ models:
856+ model1:
857+ cmd: ${simpleresponderpath} --port ${PORT} --silent --respond model1
858+ model2:
859+ cmd: ${simpleresponderpath} --port ${PORT} --silent --respond model2
860+ ` , "${simpleresponderpath}" , simpleResponderPath , - 1 )
861+
862+ // Create a test model configuration
863+ config , err := LoadConfigFromReader (strings .NewReader (configStr ))
864+ if ! assert .NoError (t , err , "Invalid configuration" ) {
865+ return
866+ }
867+
868+ preloadChan := make (chan ModelPreloadedEvent , 2 ) // buffer for 2 expected events
869+
870+ unsub := event .On (func (e ModelPreloadedEvent ) {
871+ preloadChan <- e
872+ })
873+
874+ defer unsub ()
875+
876+ // Create the proxy which should trigger preloading
877+ proxy := New (config )
878+ defer proxy .StopProcesses (StopWaitForInflightRequest )
879+
880+ for i := 0 ; i < 2 ; i ++ {
881+ select {
882+ case <- preloadChan :
883+ case <- time .After (5 * time .Second ):
884+ t .Fatal ("timed out waiting for models to preload" )
885+ }
886+ }
887+ // make sure they are both loaded
888+ _ , foundGroup := proxy .processGroups ["preloadTestGroup" ]
889+ if ! assert .True (t , foundGroup , "preloadTestGroup should exist" ) {
890+ return
891+ }
892+ assert .Equal (t , StateReady , proxy .processGroups ["preloadTestGroup" ].processes ["model1" ].CurrentState ())
893+ assert .Equal (t , StateReady , proxy .processGroups ["preloadTestGroup" ].processes ["model2" ].CurrentState ())
894+ }
0 commit comments