influxdb/balancer_test.go

116 lines
2.3 KiB
Go
Raw Normal View History

package influxdb_test
import (
"fmt"
"testing"
"github.com/influxdb/influxdb"
2015-05-27 19:41:58 +00:00
"github.com/influxdb/influxdb/meta"
)
2015-05-27 19:41:58 +00:00
func NewNodes() []meta.NodeInfo {
var nodes []meta.NodeInfo
for i := 1; i <= 2; i++ {
2015-05-27 19:41:58 +00:00
nodes = append(nodes, meta.NodeInfo{
ID: uint64(i),
Host: fmt.Sprintf("localhost:999%d", i),
})
}
return nodes
}
func TestBalancerEmptyNodes(t *testing.T) {
2015-05-27 19:41:58 +00:00
b := influxdb.NewNodeBalancer([]meta.NodeInfo{})
got := b.Next()
if got != nil {
t.Errorf("expected nil, got %v", got)
}
}
func TestBalancerUp(t *testing.T) {
2015-05-27 19:41:58 +00:00
nodes := NewNodes()
b := influxdb.NewNodeBalancer(nodes)
// First node in randomized round-robin order
first := b.Next()
if first == nil {
t.Errorf("expected datanode, got %v", first)
}
// Second node in randomized round-robin order
second := b.Next()
if second == nil {
t.Errorf("expected datanode, got %v", second)
}
// Should never get the same node in order twice
if first.ID == second.ID {
t.Errorf("expected first != second. got %v = %v", first.ID, second.ID)
}
}
2015-05-27 19:41:58 +00:00
/*
func TestBalancerDown(t *testing.T) {
2015-05-27 19:41:58 +00:00
nodes := NewNodes()
b := influxdb.NewNodeBalancer(nodes)
nodes[0].Down()
// First node in randomized round-robin order
first := b.Next()
if first == nil {
t.Errorf("expected datanode, got %v", first)
}
// Second node should rollover to the first up node
second := b.Next()
if second == nil {
t.Errorf("expected datanode, got %v", second)
}
// Health node should be returned each time
if first.ID != 2 && first.ID != second.ID {
t.Errorf("expected first != second. got %v = %v", first.ID, second.ID)
}
}
2015-05-27 19:41:58 +00:00
*/
2015-05-27 19:41:58 +00:00
/*
func TestBalancerBackUp(t *testing.T) {
nodes := newDataNodes()
2015-05-27 19:41:58 +00:00
b := influxdb.NewNodeBalancer(nodes)
nodes[0].Down()
for i := 0; i < 3; i++ {
got := b.Next()
if got == nil {
t.Errorf("expected datanode, got %v", got)
}
if exp := uint64(2); got.ID != exp {
t.Errorf("wrong node id: exp %v, got %v", exp, got.ID)
}
}
nodes[0].Up()
// First node in randomized round-robin order
first := b.Next()
if first == nil {
t.Errorf("expected datanode, got %v", first)
}
// Second node should rollover to the first up node
second := b.Next()
if second == nil {
t.Errorf("expected datanode, got %v", second)
}
// Should get both nodes returned
if first.ID == second.ID {
t.Errorf("expected first != second. got %v = %v", first.ID, second.ID)
}
}
2015-05-27 19:41:58 +00:00
*/