go-proxy/splitter/nicehash/storage_test.go
Virgil f2fd83caad fix(nicehash): count stale job hits
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-04 14:19:22 +00:00

120 lines
3.5 KiB
Go

package nicehash
import (
"strings"
"testing"
"time"
"dappco.re/go/core/proxy"
)
func TestNonceStorage_Add_Good(t *testing.T) {
storage := NewNonceStorage()
miner := proxy.NewMiner(nil, 0, nil)
miner.SetUser("wallet")
if !storage.Add(miner) {
t.Fatal("expected slot allocation to succeed")
}
}
func TestNonceStorage_Add_Bad(t *testing.T) {
storage := NewNonceStorage()
if storage.Add(nil) {
t.Fatal("expected nil miner allocation to fail")
}
}
func TestNonceStorage_Add_Ugly(t *testing.T) {
storage := NewNonceStorage()
for index := 0; index < 256; index++ {
miner := proxy.NewMiner(nil, 0, nil)
if !storage.Add(miner) {
t.Fatalf("expected miner %d to fit", index)
}
}
if storage.Add(proxy.NewMiner(nil, 0, nil)) {
t.Fatal("expected 257th miner to fail")
}
}
func TestNonceStorage_IsValidJobID_Good(t *testing.T) {
storage := NewNonceStorage()
storage.SetJob(proxy.Job{Blob: "abcd", JobID: "job-1"})
if !storage.IsValidJobID("job-1") {
t.Fatal("expected current job ID to be valid")
}
}
func TestNonceStorage_IsValidJobID_Bad(t *testing.T) {
storage := NewNonceStorage()
storage.SetJob(proxy.Job{Blob: "abcd", JobID: "job-1"})
if storage.IsValidJobID("job-2") {
t.Fatal("expected unknown job ID to be invalid")
}
}
func TestNonceStorage_IsValidJobID_Ugly(t *testing.T) {
storage := NewNonceStorage()
storage.SetJob(proxy.Job{Blob: "abcd", JobID: "job-1", ClientID: "pool-a"})
storage.SetJob(proxy.Job{Blob: "efgh", JobID: "job-2", ClientID: "pool-a"})
if !storage.IsValidJobID("job-1") {
t.Fatal("expected previous job ID from same client to remain valid")
}
if got := storage.ExpiredCount(); got != 1 {
t.Fatalf("expected stale job lookups to increment the expired counter, got %d", got)
}
}
func TestNonceStorage_IsValidJobID_BadClientID(t *testing.T) {
storage := NewNonceStorage()
storage.SetJob(proxy.Job{Blob: "abcd", JobID: "job-1", ClientID: "pool-a"})
storage.SetJob(proxy.Job{Blob: "efgh", JobID: "job-2", ClientID: "pool-b"})
if storage.IsValidJobID("job-1") {
t.Fatal("expected previous job ID from a different client to be invalid")
}
}
func TestNonceMapper_OnDisconnect_Ugly(t *testing.T) {
mapper := NewNonceMapper(1, &proxy.Config{}, nil)
mapper.pending[1] = SubmitContext{RequestID: 7}
mapper.OnDisconnect()
if len(mapper.pending) != 0 {
t.Fatalf("expected pending submits to be cleared, got %d", len(mapper.pending))
}
}
func TestNonceMapper_OnResultAccepted_Good(t *testing.T) {
bus := proxy.NewEventBus()
resultCh := make(chan proxy.Event, 1)
bus.Subscribe(proxy.EventAccept, func(event proxy.Event) {
resultCh <- event
})
miner := proxy.NewMiner(nil, 0, nil)
mapper := NewNonceMapper(1, &proxy.Config{}, nil)
mapper.events = bus
if !mapper.storage.Add(miner) {
t.Fatal("expected miner slot allocation")
}
mapper.storage.SetJob(proxy.Job{Blob: strings.Repeat("0", 160), JobID: "job-a", Target: "b88d0600"})
mapper.mu.Lock()
mapper.pending[1] = SubmitContext{
RequestID: 7,
MinerID: miner.ID(),
Job: proxy.Job{Blob: strings.Repeat("0", 160), JobID: "job-a", Target: "b88d0600"},
SubmittedAt: time.Now().UTC(),
}
mapper.mu.Unlock()
mapper.storage.SetJob(proxy.Job{Blob: strings.Repeat("1", 160), JobID: "job-b", Target: "b88d0600"})
mapper.OnResultAccepted(1, true, "")
select {
case event := <-resultCh:
if event.Job == nil || event.Job.JobID != "job-a" {
t.Fatalf("expected submitted job to be reported, got %#v", event.Job)
}
case <-time.After(time.Second):
t.Fatal("expected accept event")
}
}