feat: add testutil package for proxy config helpers
This commit is contained in:
parent
1ce42c039b
commit
094976df2b
89
internal/testutil/proxy.go
Normal file
89
internal/testutil/proxy.go
Normal file
@ -0,0 +1,89 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"rua.plus/lolly/internal/config"
|
||||
"rua.plus/lolly/internal/loadbalance"
|
||||
)
|
||||
|
||||
// NewTestProxyConfig 创建测试用的代理配置
|
||||
//
|
||||
// 参数:
|
||||
// - path: 代理路径
|
||||
// - targetURLs: 后端目标 URL 列表
|
||||
//
|
||||
// 返回值:
|
||||
// - *config.ProxyConfig: 配置好的代理配置
|
||||
func NewTestProxyConfig(path string, targetURLs ...string) *config.ProxyConfig {
|
||||
cfg := &config.ProxyConfig{
|
||||
Path: path,
|
||||
LoadBalance: "round_robin",
|
||||
Timeout: config.ProxyTimeout{
|
||||
Connect: 5 * time.Second,
|
||||
Read: 30 * time.Second,
|
||||
Write: 30 * time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
if len(targetURLs) > 0 {
|
||||
cfg.Targets = make([]config.ProxyTarget, len(targetURLs))
|
||||
for i, url := range targetURLs {
|
||||
cfg.Targets[i] = config.ProxyTarget{URL: url}
|
||||
}
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// NewTestProxyConfigWithCache 创建带缓存的测试代理配置
|
||||
func NewTestProxyConfigWithCache(path string, maxAge time.Duration, targetURLs ...string) *config.ProxyConfig {
|
||||
cfg := NewTestProxyConfig(path, targetURLs...)
|
||||
cfg.Cache = config.ProxyCacheConfig{
|
||||
Enabled: true,
|
||||
MaxAge: maxAge,
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// NewTestTarget 创建测试用的代理目标
|
||||
//
|
||||
// 参数:
|
||||
// - url: 目标 URL
|
||||
//
|
||||
// 返回值:
|
||||
// - *loadbalance.Target: 测试目标
|
||||
func NewTestTarget(url string) *loadbalance.Target {
|
||||
return &loadbalance.Target{URL: url}
|
||||
}
|
||||
|
||||
// NewTestTargets 批量创建测试目标
|
||||
func NewTestTargets(urls ...string) []*loadbalance.Target {
|
||||
targets := make([]*loadbalance.Target, len(urls))
|
||||
for i, url := range urls {
|
||||
targets[i] = NewTestTarget(url)
|
||||
}
|
||||
return targets
|
||||
}
|
||||
|
||||
// NewTestHealthyTarget 创建已标记为健康的测试目标
|
||||
//
|
||||
// 参数:
|
||||
// - url: 目标 URL
|
||||
//
|
||||
// 返回值:
|
||||
// - *loadbalance.Target: 已标记为健康的测试目标
|
||||
func NewTestHealthyTarget(url string) *loadbalance.Target {
|
||||
t := NewTestTarget(url)
|
||||
t.Healthy.Store(true)
|
||||
return t
|
||||
}
|
||||
|
||||
// NewTestHealthyTargets 批量创建健康测试目标
|
||||
func NewTestHealthyTargets(urls ...string) []*loadbalance.Target {
|
||||
targets := make([]*loadbalance.Target, len(urls))
|
||||
for i, url := range urls {
|
||||
targets[i] = NewTestHealthyTarget(url)
|
||||
}
|
||||
return targets
|
||||
}
|
||||
44
internal/testutil/proxy_test.go
Normal file
44
internal/testutil/proxy_test.go
Normal file
@ -0,0 +1,44 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewTestProxyConfig(t *testing.T) {
|
||||
cfg := NewTestProxyConfig("/api", "http://localhost:8080")
|
||||
|
||||
if cfg.Path != "/api" {
|
||||
t.Errorf("expected path /api, got %s", cfg.Path)
|
||||
}
|
||||
if len(cfg.Targets) != 1 {
|
||||
t.Errorf("expected 1 target, got %d", len(cfg.Targets))
|
||||
}
|
||||
if cfg.Timeout.Connect != 5*time.Second {
|
||||
t.Errorf("expected 5s connect timeout, got %v", cfg.Timeout.Connect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTestHealthyTarget(t *testing.T) {
|
||||
target := NewTestHealthyTarget("http://localhost:8080")
|
||||
|
||||
if target.URL != "http://localhost:8080" {
|
||||
t.Errorf("expected URL http://localhost:8080, got %s", target.URL)
|
||||
}
|
||||
if !target.Healthy.Load() {
|
||||
t.Error("expected target to be healthy")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTestHealthyTargets(t *testing.T) {
|
||||
targets := NewTestHealthyTargets("http://localhost:8080", "http://localhost:8081")
|
||||
|
||||
if len(targets) != 2 {
|
||||
t.Errorf("expected 2 targets, got %d", len(targets))
|
||||
}
|
||||
for i, target := range targets {
|
||||
if !target.Healthy.Load() {
|
||||
t.Errorf("expected target %d to be healthy", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user