github.com/fufuok/balancer@v1.0.0/balancer_test.go (about)

     1  package balancer
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestBalancer(t *testing.T) {
     8  	lb := New(WeightedRoundRobin, nil, nil)
     9  	if lb.Name() != "WeightedRoundRobin" || lb.Name() != WeightedRoundRobin.String() {
    10  		t.Fatal("balancer.New wrong")
    11  	}
    12  
    13  	lb = New(SmoothWeightedRoundRobin, nil, nil)
    14  	if lb.Name() != "SmoothWeightedRoundRobin" || lb.Name() != SmoothWeightedRoundRobin.String() {
    15  		t.Fatal("balancer.New wrong")
    16  	}
    17  
    18  	lb = New(WeightedRand, nil, nil)
    19  	if lb.Name() != "WeightedRand" || lb.Name() != WeightedRand.String() {
    20  		t.Fatal("balancer.New wrong")
    21  	}
    22  
    23  	lb = New(ConsistentHash, nil, nil)
    24  	if lb.Name() != "ConsistentHash" || lb.Name() != ConsistentHash.String() {
    25  		t.Fatal("balancer.New wrong")
    26  	}
    27  
    28  	lb = New(RoundRobin, nil, nil)
    29  	if lb.Name() != "RoundRobin" || lb.Name() != RoundRobin.String() {
    30  		t.Fatal("balancer.New wrong")
    31  	}
    32  
    33  	lb = New(Random, nil, nil)
    34  	if lb.Name() != "Random" || lb.Name() != Random.String() {
    35  		t.Fatal("balancer.New wrong")
    36  	}
    37  
    38  	if Mode(777).String() != "" {
    39  		t.Fatal("balancer name wrong")
    40  	}
    41  
    42  	lb.Add("A")
    43  	best := lb.Select()
    44  	if best != "A" {
    45  		t.Fatal("balancer select wrong")
    46  	}
    47  }