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

Function TestConsistentHashKey

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

Source from the content-addressed store, hash-verified

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"}
391 ch := newConsistentHash(endpoints)
392
393 r, _ := http.NewRequest("GET", "http://127.0.0.1:1234/foo", nil)
394 r.RemoteAddr = "192.168.0.1:8765"
395
396 rt := NewAlgorithmProvider().Do([]*routing.Route{{
397 Route: eskip.Route{
398 BackendType: eskip.LBBackend,
399 LBAlgorithm: ConsistentHash.String(),
400 LBEndpoints: eskip.NewLBEndpoints(endpoints),
401 },
402 }})[0]
403
404 defaultEndpoint := ch.Apply(&routing.LBContext{Request: r, Route: rt, LBEndpoints: rt.LBEndpoints, Params: make(map[string]interface{})})
405 remoteHostEndpoint := ch.Apply(&routing.LBContext{Request: r, Route: rt, LBEndpoints: rt.LBEndpoints, Params: map[string]interface{}{ConsistentHashKey: net.RemoteHost(r).String()}})
406
407 if defaultEndpoint != remoteHostEndpoint {
408 t.Error("remote host should be used as a default key")
409 }
410
411 for i, ep := range endpoints {
412 key := fmt.Sprintf("%s-%d", ep, 1) // "ep-0" to "ep-99" is the range of keys for this endpoint. If we use this as the hash key it should select endpoint ep.
413 selected := ch.Apply(&routing.LBContext{Request: r, Route: rt, LBEndpoints: rt.LBEndpoints, Params: map[string]interface{}{ConsistentHashKey: key}})
414 if selected != rt.LBEndpoints[i] {
415 t.Errorf("expected: %v, got %v", rt.LBEndpoints[i], selected)
416 }
417 }
418}
419
420func TestConsistentHashBoundedLoadDistribution(t *testing.T) {
421 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 9

NewLBEndpointsFunction · 0.92
RemoteHostFunction · 0.92
newConsistentHashFunction · 0.85
NewAlgorithmProviderFunction · 0.85
DoMethod · 0.65
StringMethod · 0.65
ApplyMethod · 0.65
ErrorMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…