2019-09-13 20:00:06 +02:00
package tcp
import (
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fakeConn struct {
2021-11-08 17:58:12 +01:00
writeCall map [ string ] int
closeCall int
2019-09-13 20:00:06 +02:00
}
func ( f * fakeConn ) Read ( b [ ] byte ) ( n int , err error ) {
panic ( "implement me" )
}
func ( f * fakeConn ) Write ( b [ ] byte ) ( n int , err error ) {
2021-11-08 17:58:12 +01:00
f . writeCall [ string ( b ) ] ++
2019-09-13 20:00:06 +02:00
return len ( b ) , nil
}
func ( f * fakeConn ) Close ( ) error {
2021-11-08 17:58:12 +01:00
f . closeCall ++
return nil
2019-09-13 20:00:06 +02:00
}
func ( f * fakeConn ) LocalAddr ( ) net . Addr {
panic ( "implement me" )
}
func ( f * fakeConn ) RemoteAddr ( ) net . Addr {
panic ( "implement me" )
}
func ( f * fakeConn ) SetDeadline ( t time . Time ) error {
panic ( "implement me" )
}
func ( f * fakeConn ) SetReadDeadline ( t time . Time ) error {
panic ( "implement me" )
}
func ( f * fakeConn ) SetWriteDeadline ( t time . Time ) error {
panic ( "implement me" )
}
func ( f * fakeConn ) CloseWrite ( ) error {
panic ( "implement me" )
}
func TestLoadBalancing ( t * testing . T ) {
testCases := [ ] struct {
desc string
serversWeight map [ string ] int
totalCall int
2021-11-08 17:58:12 +01:00
expectedWrite map [ string ] int
expectedClose int
2019-09-13 20:00:06 +02:00
} {
{
desc : "RoundRobin" ,
serversWeight : map [ string ] int {
"h1" : 1 ,
"h2" : 1 ,
} ,
totalCall : 4 ,
2021-11-08 17:58:12 +01:00
expectedWrite : map [ string ] int {
2019-09-13 20:00:06 +02:00
"h1" : 2 ,
"h2" : 2 ,
} ,
} ,
{
desc : "WeighedRoundRobin" ,
serversWeight : map [ string ] int {
"h1" : 3 ,
"h2" : 1 ,
} ,
totalCall : 4 ,
2021-11-08 17:58:12 +01:00
expectedWrite : map [ string ] int {
2019-09-13 20:00:06 +02:00
"h1" : 3 ,
"h2" : 1 ,
} ,
} ,
{
desc : "WeighedRoundRobin with more call" ,
serversWeight : map [ string ] int {
"h1" : 3 ,
"h2" : 1 ,
} ,
totalCall : 16 ,
2021-11-08 17:58:12 +01:00
expectedWrite : map [ string ] int {
2019-09-13 20:00:06 +02:00
"h1" : 12 ,
"h2" : 4 ,
} ,
} ,
{
2021-11-08 17:58:12 +01:00
desc : "WeighedRoundRobin with one 0 weight server" ,
2019-09-13 20:00:06 +02:00
serversWeight : map [ string ] int {
"h1" : 3 ,
"h2" : 0 ,
} ,
totalCall : 16 ,
2021-11-08 17:58:12 +01:00
expectedWrite : map [ string ] int {
2019-09-13 20:00:06 +02:00
"h1" : 16 ,
} ,
} ,
2021-11-08 17:58:12 +01:00
{
desc : "WeighedRoundRobin with all servers with 0 weight" ,
serversWeight : map [ string ] int {
"h1" : 0 ,
"h2" : 0 ,
"h3" : 0 ,
} ,
totalCall : 10 ,
expectedWrite : map [ string ] int { } ,
expectedClose : 10 ,
} ,
2019-09-13 20:00:06 +02:00
}
for _ , test := range testCases {
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
balancer := NewWRRLoadBalancer ( )
for server , weight := range test . serversWeight {
balancer . AddWeightServer ( HandlerFunc ( func ( conn WriteCloser ) {
_ , err := conn . Write ( [ ] byte ( server ) )
require . NoError ( t , err )
} ) , & weight )
}
2021-11-08 17:58:12 +01:00
conn := & fakeConn { writeCall : make ( map [ string ] int ) }
2024-02-19 15:44:03 +01:00
for range test . totalCall {
2019-09-13 20:00:06 +02:00
balancer . ServeTCP ( conn )
}
2021-11-08 17:58:12 +01:00
assert . Equal ( t , test . expectedWrite , conn . writeCall )
assert . Equal ( t , test . expectedClose , conn . closeCall )
2019-09-13 20:00:06 +02:00
} )
}
}