Skip to content

Commit

Permalink
#43: Covered router workflow by tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Jan 8, 2024
1 parent 3f7b4c5 commit 020cf3a
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion pkg/routers/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"glide/pkg/telemetry"
)

func TestLangRouter_Priority_ChatRequest(t *testing.T) {
func TestLangRouter_Priority_PickFistHealthy(t *testing.T) {
budget := health.NewErrorBudget(3, health.SEC)
models := []*providers.LangModel{
providers.NewLangModel(
Expand Down Expand Up @@ -49,3 +49,73 @@ func TestLangRouter_Priority_ChatRequest(t *testing.T) {
require.NoError(t, err)
}
}

func TestLangRouter_Priority_PickSecondHealthy(t *testing.T) {
budget := health.NewErrorBudget(3, health.SEC)
models := []*providers.LangModel{
providers.NewLangModel(
"first",
providers.NewProviderMock([]providers.ResponseMock{{Err: &ErrNoModelAvailable}, {Msg: "2"}}),
*budget,
),
providers.NewLangModel(
"second",
providers.NewProviderMock([]providers.ResponseMock{{Msg: "1"}}),
*budget,
),
}

expectedModels := []string{"second", "first"}

router := LangRouter{
routerID: "test_router",
Config: &LangRouterConfig{},
retry: retry.NewExpRetry(3, 2, 1*time.Second, nil),
routing: routing.NewPriorityRouting(models),
models: models,
telemetry: telemetry.NewTelemetryMock(),
}

ctx := context.Background()
req := schemas.NewChatFromStr("tell me a dad joke")

for _, modelID := range expectedModels {
resp, err := router.Chat(ctx, req)

require.NoError(t, err)
require.Equal(t, modelID, resp.Model)
require.Equal(t, "test_router", resp.Router)
}
}

func TestLangRouter_Priority_AllModelsUnavailable(t *testing.T) {
budget := health.NewErrorBudget(3, health.SEC)
models := []*providers.LangModel{
providers.NewLangModel(
"first",
providers.NewProviderMock([]providers.ResponseMock{{Err: &ErrNoModelAvailable}, {Err: &ErrNoModelAvailable}}),
*budget,
),
providers.NewLangModel(
"second",
providers.NewProviderMock([]providers.ResponseMock{{Err: &ErrNoModelAvailable}, {Err: &ErrNoModelAvailable}}),
*budget,
),
}

router := LangRouter{
routerID: "test_router",
Config: &LangRouterConfig{},
retry: retry.NewExpRetry(1, 2, 1*time.Millisecond, nil),
routing: routing.NewPriorityRouting(models),
models: models,
telemetry: telemetry.NewTelemetryMock(),
}

ctx := context.Background()
req := schemas.NewChatFromStr("tell me a dad joke")

_, err := router.Chat(ctx, req)

require.Error(t, err)
}

0 comments on commit 020cf3a

Please sign in to comment.