Skip to content

Commit

Permalink
Added tests for loadbalancer and round robin
Browse files Browse the repository at this point in the history
  • Loading branch information
sinadarbouy committed Jul 16, 2024
1 parent 0e7c86f commit 5717cad
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
39 changes: 39 additions & 0 deletions network/loadbalancer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package network

import (
"testing"

gerr "github.com/gatewayd-io/gatewayd/errors"
)

func TestNewLoadBalancerStrategy(t *testing.T) {
serverValid := &Server{
LoadbalancerStrategyName: RoundRobinStrategy,
Proxies: []IProxy{MockProxy{}},
}

// Test case 1: Valid strategy name
strategy, err := NewLoadBalancerStrategy(serverValid)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

_, ok := strategy.(*RoundRobin)
if !ok {
t.Errorf("Expected strategy to be of type RoundRobin")
}

// Test case 1: InValid strategy name
serverInvalid := &Server{
LoadbalancerStrategyName: "InvalidStrategy",
Proxies: []IProxy{MockProxy{}},
}

strategy, err = NewLoadBalancerStrategy(serverInvalid)
if err != gerr.ErrLoadBalancerStrategyNotFound {
t.Errorf("Expected ErrLoadBalancerStrategyNotFound, got %v", err)
}
if strategy != nil {
t.Errorf("Expected strategy to be nil for invalid strategy name")
}
}
91 changes: 91 additions & 0 deletions network/round-robin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package network

import (
"sync"
"testing"

gerr "github.com/gatewayd-io/gatewayd/errors"
)

// MockProxy implements IProxy interface for testing
type MockProxy struct {
name string
}

func (m MockProxy) Connect(conn *ConnWrapper) *gerr.GatewayDError { return nil }
func (m MockProxy) Disconnect(conn *ConnWrapper) *gerr.GatewayDError { return nil }
func (m MockProxy) PassThroughToServer(conn *ConnWrapper, stack *Stack) *gerr.GatewayDError {
return nil
}
func (m MockProxy) PassThroughToClient(conn *ConnWrapper, stack *Stack) *gerr.GatewayDError {
return nil
}
func (m MockProxy) IsHealthy(cl *Client) (*Client, *gerr.GatewayDError) { return nil, nil }
func (m MockProxy) IsExhausted() bool { return false }
func (m MockProxy) Shutdown() {}
func (m MockProxy) AvailableConnectionsString() []string { return nil }
func (m MockProxy) BusyConnectionsString() []string { return nil }

func (m MockProxy) GetName() string {
return m.name
}

func TestNewRoundRobin(t *testing.T) {
proxies := []IProxy{
MockProxy{name: "proxy1"},
MockProxy{name: "proxy2"},
MockProxy{name: "proxy3"},
}
server := &Server{Proxies: proxies}
rr := NewRoundRobin(server)

if len(rr.proxies) != len(proxies) {
t.Errorf("expected %d proxies, got %d", len(proxies), len(rr.proxies))
}
}

func TestRoundRobin_GetNextProxy(t *testing.T) {
proxies := []IProxy{
MockProxy{name: "proxy1"},
MockProxy{name: "proxy2"},
MockProxy{name: "proxy3"},
}
server := &Server{Proxies: proxies}
rr := NewRoundRobin(server)

expectedOrder := []string{"proxy2", "proxy3", "proxy1", "proxy2", "proxy3"}

for i, expected := range expectedOrder {
proxy := rr.GetNextProxy()
if proxy.(MockProxy).GetName() != expected {
t.Errorf("test %d: expected proxy name %s, got %s", i, expected, proxy.(MockProxy).GetName())
}
}
}

func TestRoundRobin_ConcurrentAccess(t *testing.T) {
proxies := []IProxy{
MockProxy{name: "proxy1"},
MockProxy{name: "proxy2"},
MockProxy{name: "proxy3"},
}
server := &Server{Proxies: proxies}
rr := NewRoundRobin(server)

var wg sync.WaitGroup
numGoroutines := 100
wg.Add(numGoroutines)

for i := 0; i < numGoroutines; i++ {
go func() {
defer wg.Done()
_ = rr.GetNextProxy()
}()
}

wg.Wait()
nextIndex := rr.next.Load()
if nextIndex != uint32(numGoroutines) {
t.Errorf("expected next index to be %d, got %d", numGoroutines, nextIndex)
}
}

0 comments on commit 5717cad

Please sign in to comment.