Skip to content

Commit

Permalink
Added weight/priority balancer tests yyyar#204
Browse files Browse the repository at this point in the history
  • Loading branch information
illarion committed Jun 14, 2019
1 parent 743121c commit ec761f8
Showing 1 changed file with 134 additions and 4 deletions.
138 changes: 134 additions & 4 deletions test/weight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,124 @@ import (
"github.com/yyyar/gobetween/core"
)

func TestOnlyBestPriorityBackendsElected(t *testing.T) {
rand.Seed(time.Now().Unix())
balancer := &balance.WeightBalancer{}
var context core.Context

context = DummyContext{}

backends := []*core.Backend{
{
Priority: 0,
Weight: 0,
},
{
Priority: 0,
Weight: 1,
},
{
Priority: 0,
Weight: 2,
},
{
Priority: 1,
Weight: 0,
},
{
Priority: 1,
Weight: 1,
},
{
Priority: 1,
Weight: 2,
},
{
Priority: 2,
Weight: 0,
},
{
Priority: 2,
Weight: 1,
},
{
Priority: 2,
Weight: 2,
},
}

hits := make(map[int]bool)

for try := 0; try < 100; try++ {
backend, err := balancer.Elect(context, backends)
if err != nil {
t.Fatal(err)
}

hits[backend.Priority] = true
if len(hits) > 1 {
t.Error("Backends with different priority elected")
}

if backend.Priority != 0 {
t.Error("Backends with not optimal priority elected")
}

}

}

func TestAllWeightsEqualTo0Distribution(t *testing.T) {
rand.Seed(time.Now().Unix())
balancer := &balance.WeightBalancer{}
var context core.Context

context = DummyContext{}

backends := []*core.Backend{
{
Target: core.Target{
Host: "1",
Port: "1",
},
Weight: 0,
},
{
Target: core.Target{
Host: "2",
Port: "2",
},
Weight: 0,
},
{
Target: core.Target{
Host: "3",
Port: "3",
},
Weight: 0,
},
}

hits := make(map[string]bool)

for try := 0; try < 100; try++ {
backend, err := balancer.Elect(context, backends)
if err != nil {
t.Fatal(err)
}

hits[backend.Target.Host] = true
if len(hits) == 3 {
return
}

}

if len(hits) != 3 {
t.Error("Group of backends with weight = 0 has some backneds that are never elected")
}
}

func TestWeightDistribution(t *testing.T) {
rand.Seed(time.Now().Unix())
balancer := &balance.WeightBalancer{}
Expand All @@ -19,16 +137,25 @@ func TestWeightDistribution(t *testing.T) {

backends := []*core.Backend{
{
Weight: 20,
Priority: 1,
Weight: 20,
},
{
Weight: 15,
Priority: 1,
Weight: 15,
},
{
Weight: 25,
Priority: 1,
Weight: 25,
},
{
Weight: 40,
Priority: 1,
Weight: 40,
},
{
// this backend is ignored
Priority: 2,
Weight: 244,
},
}

Expand All @@ -45,6 +172,9 @@ func TestWeightDistribution(t *testing.T) {
quantity := make(map[int]int)

for _, backend := range backends {
if backend.Priority > 1 {
continue
}
quantity[backend.Weight] = 0
}

Expand Down

0 comments on commit ec761f8

Please sign in to comment.