Files
godoxy-yusing/internal/net/gphttp/loadbalancer/loadbalancer_test.go
yusing 35a3e3fef6 refactor(api): restructured API for type safety, maintainability and docs generation
- These changes makes the API incombatible with previous versions
- Added new types for error handling, success responses, and health checks.
- Updated health check logic to utilize the new types for better clarity and structure.
- Refactored existing handlers to improve response consistency and error handling.
- Updated Makefile to include a new target for generating API types from Swagger.
- Updated "new agent" API to respond an encrypted cert pair
2025-08-16 13:04:05 +08:00

45 lines
1.4 KiB
Go

package loadbalancer
import (
"testing"
"github.com/yusing/go-proxy/internal/types"
. "github.com/yusing/go-proxy/internal/utils/testing"
)
func TestRebalance(t *testing.T) {
t.Parallel()
t.Run("zero", func(t *testing.T) {
lb := New(new(types.LoadBalancerConfig))
for range 10 {
lb.AddServer(TestNewServer(0))
}
lb.rebalance()
ExpectEqual(t, lb.sumWeight, maxWeight)
})
t.Run("less", func(t *testing.T) {
lb := New(new(types.LoadBalancerConfig))
lb.AddServer(TestNewServer(float64(maxWeight) * .1))
lb.AddServer(TestNewServer(float64(maxWeight) * .2))
lb.AddServer(TestNewServer(float64(maxWeight) * .3))
lb.AddServer(TestNewServer(float64(maxWeight) * .2))
lb.AddServer(TestNewServer(float64(maxWeight) * .1))
lb.rebalance()
// t.Logf("%s", U.Must(json.MarshalIndent(lb.pool, "", " ")))
ExpectEqual(t, lb.sumWeight, maxWeight)
})
t.Run("more", func(t *testing.T) {
lb := New(new(types.LoadBalancerConfig))
lb.AddServer(TestNewServer(float64(maxWeight) * .1))
lb.AddServer(TestNewServer(float64(maxWeight) * .2))
lb.AddServer(TestNewServer(float64(maxWeight) * .3))
lb.AddServer(TestNewServer(float64(maxWeight) * .4))
lb.AddServer(TestNewServer(float64(maxWeight) * .3))
lb.AddServer(TestNewServer(float64(maxWeight) * .2))
lb.AddServer(TestNewServer(float64(maxWeight) * .1))
lb.rebalance()
// t.Logf("%s", U.Must(json.MarshalIndent(lb.pool, "", " ")))
ExpectEqual(t, lb.sumWeight, maxWeight)
})
}