MCPcopy Create free account
hub / github.com/zalando/skipper / TestConsistentHashBoundedLoadSearch

Function TestConsistentHashBoundedLoadSearch

loadbalancer/algorithm_test.go:340–387  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

338}
339
340func TestConsistentHashBoundedLoadSearch(t *testing.T) {
341 endpoints := []string{"http://127.0.0.1:8080", "http://127.0.0.2:8080", "http://127.0.0.3:8080"}
342 r, _ := http.NewRequest("GET", "http://127.0.0.1:1234/foo", nil)
343 route := NewAlgorithmProvider().Do([]*routing.Route{{
344 Route: eskip.Route{
345 BackendType: eskip.LBBackend,
346 LBAlgorithm: ConsistentHash.String(),
347 LBEndpoints: eskip.NewLBEndpoints(endpoints),
348 },
349 }})[0]
350
351 ch := route.LBAlgorithm.(*consistentHash)
352 ctx := &routing.LBContext{
353 Request: r,
354 Route: route,
355 LBEndpoints: route.LBEndpoints,
356 Params: map[string]interface{}{ConsistentHashBalanceFactor: 1.25},
357 }
358 endpointRegistry := routing.NewEndpointRegistry(routing.RegistryOptions{})
359 defer endpointRegistry.Close()
360 endpointRegistry.Do([]*routing.Route{route})
361 noLoad := ch.Apply(ctx)
362 nonBounded := ch.Apply(&routing.LBContext{Request: r, Route: route, LBEndpoints: route.LBEndpoints, Params: map[string]interface{}{}})
363
364 if noLoad != nonBounded {
365 t.Error("When no endpoints are overloaded, the chosen endpoint should be the same as standard consistentHash")
366 }
367 // now we know that noLoad is the endpoint which should be requested for somekey if load is not an issue.
368 addInflightRequests(endpointRegistry, noLoad, 20)
369 failover1 := ch.Apply(ctx)
370 if failover1 == nonBounded {
371 t.Error("When the selected endpoint is overloaded, the chosen endpoint should be different to standard consistentHash")
372 }
373
374 // now if 2 endpoints are overloaded, the request should go to the final endpoint
375 addInflightRequests(endpointRegistry, failover1, 20)
376 failover2 := ch.Apply(ctx)
377 if failover2 == nonBounded || failover2 == failover1 {
378 t.Error("Only the final endpoint had load below the average * balanceFactor, so it should have been selected.")
379 }
380
381 // now all will have same load, should select the original endpoint again
382 addInflightRequests(endpointRegistry, failover2, 20)
383 allLoaded := ch.Apply(ctx)
384 if allLoaded != nonBounded {
385 t.Error("When all endpoints have the same load, the consistentHash endpoint should be chosen again.")
386 }
387}
388
389func TestConsistentHashKey(t *testing.T) {
390 endpoints := []string{"http://127.0.0.1:8080", "http://127.0.0.2:8080", "http://127.0.0.3:8080"}

Callers

nothing calls this directly

Calls 10

CloseMethod · 0.95
DoMethod · 0.95
NewLBEndpointsFunction · 0.92
NewEndpointRegistryFunction · 0.92
NewAlgorithmProviderFunction · 0.85
addInflightRequestsFunction · 0.85
DoMethod · 0.65
StringMethod · 0.65
ApplyMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…