Skip to content

Commit

Permalink
#46: Covered LL by tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Jan 14, 2024
1 parent da5df0e commit 36b1fad
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions pkg/routers/routing/least_latency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package routing
import (
"strconv"
"testing"
"time"

"github.com/stretchr/testify/require"
"glide/pkg/providers"
Expand Down Expand Up @@ -49,6 +50,76 @@ func TestLeastLatencyRouting_Warmup(t *testing.T) {
}
}

func TestLeastLatencyRouting_Routing(t *testing.T) {
type Model struct {
modelID string
healthy bool
latency float64
expireAt time.Time
}

type TestCase struct {
models []Model
expectedModelIDs []string
}

tests := map[string]TestCase{
"no cold expired models": {
[]Model{
{"first", true, 100.0, time.Now().Add(30 * time.Second)},
{"second", true, 80.0, time.Now().Add(30 * time.Second)},
{"third", true, 101.0, time.Now().Add(30 * time.Second)},
},
[]string{"second", "second", "second"},
},
"one expired model": {
[]Model{
{"first", true, 100.0, time.Now().Add(30 * time.Second)},
{"second", true, 80.0, time.Now().Add(30 * time.Second)},
{"third", true, 101.0, time.Now().Add(-time.Duration(30) * time.Second)},
},
[]string{"third", "second", "second"},
},
"two expired models": {
[]Model{
{"first", true, 100.0, time.Now().Add(-time.Duration(60) * time.Second)},
{"second", true, 80.0, time.Now().Add(30 * time.Second)},
{"third", true, 101.0, time.Now().Add(-time.Duration(30) * time.Second)},
},
[]string{"first", "third", "second"},
},
"all expired models": {
[]Model{
{"first", true, 100.0, time.Now().Add(-time.Duration(30) * time.Second)},
{"second", true, 80.0, time.Now().Add(-time.Duration(20) * time.Second)},
{"third", true, 101.0, time.Now().Add(-time.Duration(60) * time.Second)},
},
[]string{"third", "first", "second"},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
models := make([]providers.Model, 0, len(tc.models))

for _, model := range tc.models {
models = append(models, providers.NewLangModelMock(model.modelID, model.healthy, model.latency))
}

routing := NewLeastLatencyRouting(models)
iterator := routing.Iterator()

// loop three times over the whole pool to check if we return back to the begging of the list
for _, modelID := range tc.expectedModelIDs {
model, err := iterator.Next()

require.NoError(t, err)
require.Equal(t, modelID, model.ID())
}
})
}
}

func TestLeastLatencyRouting_NoHealthyModels(t *testing.T) {
tests := map[string][]float64{
"all cold models unhealthy": {0.0, 0.0, 0.0},
Expand Down

0 comments on commit 36b1fad

Please sign in to comment.