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

Function TestConsistentHashSearch

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

Source from the content-addressed store, hash-verified

296}
297
298func TestConsistentHashSearch(t *testing.T) {
299 apply := func(key string, endpoints []string) string {
300 p := NewAlgorithmProvider()
301 endpointRegistry := routing.NewEndpointRegistry(routing.RegistryOptions{})
302 defer endpointRegistry.Close()
303 r := &routing.Route{
304 Route: eskip.Route{
305 BackendType: eskip.LBBackend,
306 LBAlgorithm: ConsistentHash.String(),
307 LBEndpoints: eskip.NewLBEndpoints(endpoints),
308 },
309 }
310 p.Do([]*routing.Route{r})
311 endpointRegistry.Do([]*routing.Route{r})
312
313 ch := newConsistentHash(endpoints).(*consistentHash)
314 ctx := &routing.LBContext{Route: r, LBEndpoints: r.LBEndpoints, Params: map[string]interface{}{ConsistentHashKey: key}}
315 return endpoints[ch.search(key, ctx)]
316 }
317
318 endpoints := []string{"http://127.0.0.1:8080", "http://127.0.0.2:8080", "http://127.0.0.3:8080"}
319 const key = "192.168.0.1"
320
321 ep := apply(key, endpoints)
322
323 // remove endpoint
324 endpoints = endpoints[1:]
325
326 ep1 := apply(key, endpoints)
327 if ep != ep1 {
328 t.Errorf("expected to select %s, got %s", ep, ep1)
329 }
330
331 // add endpoint
332 endpoints = append(endpoints, "http://127.0.0.4:8080")
333
334 ep2 := apply(key, endpoints)
335 if ep != ep2 {
336 t.Errorf("expected to select %s, got %s", ep, ep2)
337 }
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"}

Callers

nothing calls this directly

Calls 10

CloseMethod · 0.95
DoMethod · 0.95
NewEndpointRegistryFunction · 0.92
NewLBEndpointsFunction · 0.92
NewAlgorithmProviderFunction · 0.85
newConsistentHashFunction · 0.85
StringMethod · 0.65
DoMethod · 0.65
ErrorfMethod · 0.65
searchMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…