(t *testing.T)
| 418 | } |
| 419 | |
| 420 | func 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"} |
| 422 | r, _ := http.NewRequest("GET", "http://127.0.0.1:1234/foo", nil) |
| 423 | route := NewAlgorithmProvider().Do([]*routing.Route{{ |
| 424 | Route: eskip.Route{ |
| 425 | BackendType: eskip.LBBackend, |
| 426 | LBAlgorithm: ConsistentHash.String(), |
| 427 | LBEndpoints: eskip.NewLBEndpoints(endpoints), |
| 428 | }, |
| 429 | }})[0] |
| 430 | |
| 431 | ch := route.LBAlgorithm.(*consistentHash) |
| 432 | balanceFactor := 1.25 |
| 433 | ctx := &routing.LBContext{ |
| 434 | Request: r, |
| 435 | Route: route, |
| 436 | LBEndpoints: route.LBEndpoints, |
| 437 | Params: map[string]interface{}{ConsistentHashBalanceFactor: balanceFactor}, |
| 438 | } |
| 439 | endpointRegistry := routing.NewEndpointRegistry(routing.RegistryOptions{}) |
| 440 | defer endpointRegistry.Close() |
| 441 | endpointRegistry.Do([]*routing.Route{route}) |
| 442 | |
| 443 | for i := 0; i < 100; i++ { |
| 444 | ep := ch.Apply(ctx) |
| 445 | ifr0 := route.LBEndpoints[0].Metrics.InflightRequests() |
| 446 | ifr1 := route.LBEndpoints[1].Metrics.InflightRequests() |
| 447 | ifr2 := route.LBEndpoints[2].Metrics.InflightRequests() |
| 448 | |
| 449 | assert.Equal(t, int64(ifr0), endpointRegistry.GetMetrics(route.LBEndpoints[0].Host).InflightRequests()) |
| 450 | assert.Equal(t, int64(ifr1), endpointRegistry.GetMetrics(route.LBEndpoints[1].Host).InflightRequests()) |
| 451 | assert.Equal(t, int64(ifr2), endpointRegistry.GetMetrics(route.LBEndpoints[2].Host).InflightRequests()) |
| 452 | |
| 453 | avg := float64(ifr0+ifr1+ifr2) / 3.0 |
| 454 | limit := int64(avg*balanceFactor) + 1 |
| 455 | if ifr0 > limit || ifr1 > limit || ifr2 > limit { |
| 456 | t.Errorf("Expected in-flight requests for each endpoint to be less than %d. In-flight request counts: %d, %d, %d", limit, ifr0, ifr1, ifr2) |
| 457 | } |
| 458 | endpointRegistry.GetMetrics(ep.Host).IncInflightRequest() |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | func TestConsistentHashKeyDistribution(t *testing.T) { |
| 463 | endpoints := []string{"http://10.2.0.1:8080", "http://10.2.0.2:8080", "http://10.2.0.3:8080", "http://10.2.0.4:8080", "http://10.2.0.5:8080", "http://10.2.0.6:8080", "http://10.2.0.7:8080", "http://10.2.0.8:8080", "http://10.2.0.9:8080", "http://10.2.0.10:8080"} |
nothing calls this directly
no test coverage detected
searching dependent graphs…