106 lines
2.8 KiB
Go
106 lines
2.8 KiB
Go
package proxy
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestConfig_Validate_Good(t *testing.T) {
|
|
cfg := &Config{
|
|
Mode: "nicehash",
|
|
Workers: WorkersByRigID,
|
|
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_EmptyMode(t *testing.T) {
|
|
cfg := &Config{
|
|
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 empty 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_EmptyWorkers(t *testing.T) {
|
|
cfg := &Config{
|
|
Mode: "simple",
|
|
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 empty 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(`{"mode":"nicehash","workers":"rig-id","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(`{"mode":"nicehash","workers":"rig-id","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):
|
|
}
|
|
}
|