diff --git a/splitter/nicehash/impl.go b/splitter/nicehash/impl.go index 1d8ef9f..09d2b13 100644 --- a/splitter/nicehash/impl.go +++ b/splitter/nicehash/impl.go @@ -148,11 +148,11 @@ func (s *NonceSplitter) Upstreams() proxy.UpstreamStats { for _, mapper := range s.mappers { if mapper.strategy != nil && mapper.strategy.IsActive() { stats.Active++ - } else if mapper.suspended > 0 { + } else if mapper.suspended > 0 || !mapper.active { stats.Error++ } } - stats.Total = uint64(len(s.mappers)) + stats.Total = stats.Active + stats.Sleep + stats.Error return stats } diff --git a/splitter/nicehash/upstreams_test.go b/splitter/nicehash/upstreams_test.go new file mode 100644 index 0000000..f4b39f5 --- /dev/null +++ b/splitter/nicehash/upstreams_test.go @@ -0,0 +1,69 @@ +package nicehash + +import ( + "testing" + + "dappco.re/go/proxy" +) + +type upstreamStateStrategy struct { + active bool +} + +func (s *upstreamStateStrategy) Connect() {} + +func (s *upstreamStateStrategy) Submit(jobID, nonce, result, algo string) int64 { + return 0 +} + +func (s *upstreamStateStrategy) Disconnect() {} + +func (s *upstreamStateStrategy) IsActive() bool { return s.active } + +func TestNonceSplitter_Upstreams_Good(t *testing.T) { + splitter := &NonceSplitter{ + mappers: []*NonceMapper{ + {strategy: &upstreamStateStrategy{active: true}, active: true}, + {strategy: &upstreamStateStrategy{active: false}, active: false, suspended: 1}, + }, + } + + stats := splitter.Upstreams() + + if stats.Active != 1 { + t.Fatalf("expected one active upstream, got %d", stats.Active) + } + if stats.Error != 1 { + t.Fatalf("expected one error upstream, got %d", stats.Error) + } + if stats.Total != 2 { + t.Fatalf("expected total to equal active + sleep + error, got %d", stats.Total) + } +} + +func TestNonceSplitter_Upstreams_Bad(t *testing.T) { + var splitter *NonceSplitter + + stats := splitter.Upstreams() + + if stats != (proxy.UpstreamStats{}) { + t.Fatalf("expected zero-value stats for nil splitter, got %+v", stats) + } +} + +func TestNonceSplitter_Upstreams_Ugly(t *testing.T) { + splitter := &NonceSplitter{ + mappers: []*NonceMapper{ + {strategy: &upstreamStateStrategy{active: false}, active: false}, + }, + } + + stats := splitter.Upstreams() + + if stats.Error != 1 { + t.Fatalf("expected an unready mapper to be counted as error, got %+v", stats) + } + if stats.Total != 1 { + t.Fatalf("expected total to remain internally consistent, got %+v", stats) + } +}