Plugin/Proxy - add new policy always_first to mimic windows dns resolvers (#1459)

* add new policy always_first to mimic windows dns resolvers
fill documentation, add UT and cleanup fmt

* change name of policy from always_first to first. Update docs
This commit is contained in:
Francois Tur
2018-01-30 16:29:49 -05:00
committed by John Belamaric
parent 0af9b9b16f
commit b93a36b213
4 changed files with 44 additions and 6 deletions

View File

@@ -32,8 +32,8 @@ func (r *customPolicy) Select(pool HostPool) *UpstreamHost {
func testPool() HostPool {
pool := []*UpstreamHost{
{Name: workableServer.URL}, // this should resolve (healthcheck test)
{Name: "http://shouldnot.resolve"}, // this shouldn't
{Name: workableServer.URL}, // this should resolve (healthcheck test)
{Name: "http://shouldnot.resolve:85"}, // this shouldn't, especially on port other than 80
{Name: "http://C"},
}
return HostPool(pool)
@@ -136,3 +136,24 @@ func TestCustomPolicy(t *testing.T) {
t.Error("Expected custom policy host to be the first host.")
}
}
func TestFirstPolicy(t *testing.T) {
pool := testPool()
rrPolicy := &First{}
h := rrPolicy.Select(pool)
// First selected host is 1, because counter starts at 0
// and increments before host is selected
if h != pool[0] {
t.Error("Expected always first to be first host in the pool.")
}
h = rrPolicy.Select(pool)
if h != pool[0] {
t.Error("Expected always first to be first host in the pool, even in second call")
}
// set this first in pool as failed
pool[0].Fails = 1
h = rrPolicy.Select(pool)
if h != pool[1] {
t.Error("Expected first to be he second in pool if the first one is down.")
}
}