diff --git a/autopilot/contractor/hosts_test.go b/autopilot/contractor/hosts_test.go index 028da682d..b2aacb1af 100644 --- a/autopilot/contractor/hosts_test.go +++ b/autopilot/contractor/hosts_test.go @@ -52,20 +52,27 @@ func TestScoredHostsRandSelectByScore(t *testing.T) { t.Fatal("unexpected") } - // assert select is random on equal inputs - counts := make([]int, 2) + // assert select is random on equal inputs, we calculate the chi-square + // statistic and assert it's less than critical value of 6.635 (1 degree of + // freedom, using alpha of .01) + var counts [2]int hosts = scoredHosts{ {score: .1, host: api.Host{PublicKey: types.PublicKey{1}}}, {score: .1, host: api.Host{PublicKey: types.PublicKey{2}}}, } - for i := 0; i < 100; i++ { + nRuns := 1e5 + for i := 0; i < int(nRuns); i++ { if hosts.randSelectByScore(1)[0].host.PublicKey == (types.PublicKey{1}) { counts[0]++ } else { counts[1]++ } } - if diff := absDiffInt(counts[0], counts[1]); diff > 40 { - t.Fatal("unexpected", counts[0], counts[1], diff) + var chi2 float64 + for i := 0; i < 2; i++ { + chi2 += math.Pow(float64(counts[i])-nRuns/2, 2) / (nRuns / 2) + } + if chi2 > 6.635 { + t.Fatal("unexpected", counts[0], counts[1], chi2) } } diff --git a/autopilot/contractor/hostscore_test.go b/autopilot/contractor/hostscore_test.go index f6d3b12b2..41347ec72 100644 --- a/autopilot/contractor/hostscore_test.go +++ b/autopilot/contractor/hostscore_test.go @@ -240,10 +240,3 @@ func TestCollateralScore(t *testing.T) { t.Errorf("expected %v but got %v", 0, s) } } - -func absDiffInt(x, y int) int { - if x < y { - return y - x - } - return x - y -} diff --git a/worker/worker.go b/worker/worker.go index 60dfb475d..a01813e2f 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -1480,7 +1480,7 @@ func discardTxnOnErr(ctx context.Context, bus Bus, l *zap.SugaredLogger, txn typ ctx, cancel := context.WithTimeout(ctx, 10*time.Second) if dErr := bus.WalletDiscard(ctx, txn); dErr != nil { - l.Errorf("%w: %v, failed to discard txn: %v", *err, errContext, dErr) + l.Errorf("%v: %s, failed to discard txn: %v", *err, errContext, dErr) } cancel() }