(t *testing.T)
| 296 | } |
| 297 | |
| 298 | func 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 | |
| 340 | func 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"} |
nothing calls this directly
no test coverage detected
searching dependent graphs…