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