go-proxy/proxy_runtime_test.go
Virgil 22e98635e7 fix(proxy): hot-reload log file paths
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-04 13:14:08 +00:00

134 lines
4.2 KiB
Go

package proxy
import (
"net"
"os"
"strings"
"testing"
)
func TestProxy_Reload_Good(t *testing.T) {
tempDir := t.TempDir()
cfg := &Config{
Mode: "nicehash",
Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}},
Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}},
CustomDiff: 100,
Workers: WorkersDisabled,
AccessLogFile: tempDir + "/access-a.log",
ShareLogFile: tempDir + "/share-a.log",
}
proxyValue, errorValue := New(cfg)
if errorValue != nil {
t.Fatal(errorValue)
}
miner := &Miner{user: "wallet", agent: "agent", ip: "10.0.0.1"}
proxyValue.events.Dispatch(Event{Type: EventLogin, Miner: miner})
proxyValue.events.Dispatch(Event{Type: EventAccept, Miner: miner, Diff: 100, Latency: 10})
reloadCfg := &Config{
Mode: "simple",
Bind: []BindAddr{{Host: "0.0.0.0", Port: 4444}},
Pools: []PoolConfig{{URL: "pool-b:4444", Enabled: true}},
CustomDiff: 250,
Workers: WorkersByUser,
AccessLogFile: tempDir + "/access-b.log",
ShareLogFile: tempDir + "/share-b.log",
}
proxyValue.Reload(reloadCfg)
if len(proxyValue.config.Bind) != 1 || proxyValue.config.Bind[0].Port != 3333 {
t.Fatalf("expected bind addresses to remain unchanged, got %+v", proxyValue.config.Bind)
}
if len(proxyValue.config.Pools) != 1 || proxyValue.config.Pools[0].URL != "pool-b:4444" {
t.Fatalf("expected pools to reload, got %+v", proxyValue.config.Pools)
}
if proxyValue.config.CustomDiff != 250 {
t.Fatalf("expected custom diff to reload, got %d", proxyValue.config.CustomDiff)
}
if proxyValue.customDiff == nil || proxyValue.customDiff.globalDiff != 250 {
t.Fatalf("expected live custom diff to update, got %+v", proxyValue.customDiff)
}
proxyValue.events.Dispatch(Event{Type: EventLogin, Miner: miner})
proxyValue.events.Dispatch(Event{Type: EventAccept, Miner: miner, Diff: 250, Latency: 12})
oldAccessLog, errorValue := os.ReadFile(tempDir + "/access-a.log")
if errorValue != nil {
t.Fatal(errorValue)
}
newAccessLog, errorValue := os.ReadFile(tempDir + "/access-b.log")
if errorValue != nil {
t.Fatal(errorValue)
}
if strings.Count(string(oldAccessLog), "CONNECT") != 1 || strings.Count(string(newAccessLog), "CONNECT") != 1 {
t.Fatalf("expected access log writes to move across reload, got old=%q new=%q", oldAccessLog, newAccessLog)
}
oldShareLog, errorValue := os.ReadFile(tempDir + "/share-a.log")
if errorValue != nil {
t.Fatal(errorValue)
}
newShareLog, errorValue := os.ReadFile(tempDir + "/share-b.log")
if errorValue != nil {
t.Fatal(errorValue)
}
if strings.Count(string(oldShareLog), "ACCEPT") != 1 || strings.Count(string(newShareLog), "ACCEPT") != 1 {
t.Fatalf("expected share log writes to move across reload, got old=%q new=%q", oldShareLog, newShareLog)
}
}
func TestProxy_CurrentMiners_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}},
Workers: WorkersDisabled,
}
firstProxy, errorValue := New(cfg)
if errorValue != nil {
t.Fatal(errorValue)
}
secondProxy, errorValue := New(cfg)
if errorValue != nil {
t.Fatal(errorValue)
}
miner := &Miner{}
firstProxy.events.Dispatch(Event{Type: EventLogin, Miner: miner})
if got := firstProxy.CurrentMiners(); got != 1 {
t.Fatalf("expected first proxy miner count 1, got %d", got)
}
if got := secondProxy.CurrentMiners(); got != 0 {
t.Fatalf("expected second proxy miner count 0, got %d", got)
}
firstProxy.events.Dispatch(Event{Type: EventClose, Miner: miner})
if got := firstProxy.CurrentMiners(); got != 0 {
t.Fatalf("expected first proxy miner count to return to 0, got %d", got)
}
}
func TestProxy_AcceptConn_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}},
Workers: WorkersDisabled,
}
proxyValue, errorValue := New(cfg)
if errorValue != nil {
t.Fatal(errorValue)
}
serverConn, clientConn := net.Pipe()
proxyValue.acceptConn(serverConn, 3333)
if got := proxyValue.stats.connections.Load(); got != 1 {
t.Fatalf("expected connection counter to increment on accept, got %d", got)
}
_ = clientConn.Close()
_ = serverConn.Close()
}