github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/balancer/roundrobin_test.go (about)

     1  package balancer
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func TestRoundRobin_NextIndex(t *testing.T) {
     9  	a := assert.New(t)
    10  	r := &RoundRobin{}
    11  	total := 5
    12  	for i := 1; i < total; i++ {
    13  		a.Equal(i, r.NextIndex(total))
    14  	}
    15  	for i := 0; i < total; i++ {
    16  		a.Equal(i, r.NextIndex(total))
    17  	}
    18  }
    19  
    20  func TestRoundRobin_NextPeer(t *testing.T) {
    21  	a := assert.New(t)
    22  	r := &RoundRobin{}
    23  
    24  	// not slice
    25  	{
    26  		err, _ := r.NextPeer("s")
    27  		a.Equal(ErrInputNotSlice, err)
    28  	}
    29  
    30  	// no nodes
    31  	{
    32  		err, _ := r.NextPeer([]string{})
    33  		a.Equal(ErrNoAvaliableNode, err)
    34  	}
    35  
    36  	// pass
    37  	{
    38  		err, res := r.NextPeer([]string{"a"})
    39  		a.NoError(err)
    40  		a.Equal("a", res.(string))
    41  	}
    42  }