go-proxy/config_runtime_test.go
Virgil 9e997554fa fix(config): validate mode and workers enums
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-04 17:14:54 +00:00

82 lines
2.1 KiB
Go

package proxy
import (
"os"
"testing"
"time"
)
func TestConfig_Validate_Good(t *testing.T) {
cfg := &Config{
Mode: "nicehash",
Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}},
Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}},
}
if errorValue := cfg.Validate(); errorValue != nil {
t.Fatalf("expected valid config, got %v", errorValue)
}
}
func TestConfig_Validate_Bad(t *testing.T) {
cfg := &Config{
Mode: "bogus",
Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}},
Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}},
}
if errorValue := cfg.Validate(); errorValue == nil {
t.Fatal("expected invalid mode to fail validation")
}
}
func TestConfig_Validate_Ugly(t *testing.T) {
cfg := &Config{
Mode: "simple",
Workers: WorkersMode("bogus"),
Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}},
Pools: []PoolConfig{{Enabled: true}},
}
if errorValue := cfg.Validate(); errorValue == nil {
t.Fatal("expected invalid workers mode to fail validation")
}
}
func TestConfig_Validate_EnabledPoolURL(t *testing.T) {
cfg := &Config{
Mode: "simple",
Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}},
Pools: []PoolConfig{{Enabled: true}},
}
if errorValue := cfg.Validate(); errorValue == nil {
t.Fatal("expected empty enabled pool URL to fail validation")
}
}
func TestConfigWatcher_Start_Bad(t *testing.T) {
path := t.TempDir() + "/config.json"
errorValue := os.WriteFile(path, []byte(`{"bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-a:3333","enabled":true}]}`), 0o644)
if errorValue != nil {
t.Fatal(errorValue)
}
triggered := make(chan struct{}, 1)
watcher := newConfigWatcher(path, func(cfg *Config) {
triggered <- struct{}{}
}, false)
watcher.Start()
defer watcher.Stop()
errorValue = os.WriteFile(path, []byte(`{"bind":[{"host":"127.0.0.1","port":3333}],"pools":[{"url":"pool-b:3333","enabled":true}]}`), 0o644)
if errorValue != nil {
t.Fatal(errorValue)
}
select {
case <-triggered:
t.Fatal("expected disabled watcher to stay quiet")
case <-time.After(1200 * time.Millisecond):
}
}