Skip to content

les/lespay: add tests #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions les/lespay/client/requestbasket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ func TestReqValueFactor(t *testing.T) {
checkF64(t, "reqValueFactor", rvf, 333.333, 1)
}

func TestNormalize(t *testing.T) {
for cycle := 0; cycle < 100; cycle += 1 {
// Initialize data for testing
valueRange, lower := 1000000, 1000000
ref := referenceBasket{basket: requestBasket{items: make([]basketItem, 10)}}
for i := 0; i < 10; i++ {
ref.basket.items[i].amount = uint64(rand.Intn(valueRange) + lower)
ref.basket.items[i].value = uint64(rand.Intn(valueRange) + lower)
}
ref.normalize()

// Check whether SUM(amount) ~= SUM(value)
var sumAmount, sumValue uint64
for i := 0; i < 10; i++ {
sumAmount += ref.basket.items[i].amount
sumValue += ref.basket.items[i].value
}
var epsilon = 0.01
if float64(sumAmount)*(1+epsilon) < float64(sumValue) || float64(sumAmount)*(1-epsilon) > float64(sumValue) {
t.Fatalf("Failed to normalize sumAmount: %d sumValue: %d", sumAmount, sumValue)
}
}
}

func TestReqValueAdjustment(t *testing.T) {
var s1, s2 serverBasket
s1.init(3)
Expand Down
19 changes: 19 additions & 0 deletions les/lespay/client/timestats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ import (
"github.com/ethereum/go-ethereum/les/utils"
)

func TestTransition(t *testing.T) {
var epsilon = 0.01
var cases = []time.Duration{
time.Millisecond, minResponseTime,
time.Second, time.Second * 5, maxResponseTime,
}
for _, c := range cases {
got := StatScaleToTime(TimeToStatScale(c))
if float64(got)*(1+epsilon) < float64(c) || float64(got)*(1-epsilon) > float64(c) {
t.Fatalf("Failed to transition back")
}
}
// If the time is too large(exceeds the max response time.
got := StatScaleToTime(TimeToStatScale(2 * maxResponseTime))
if float64(got)*(1+epsilon) < float64(maxResponseTime) || float64(got)*(1-epsilon) > float64(maxResponseTime) {
t.Fatalf("Failed to transition back")
}
}

var maxResponseWeights = TimeoutWeights(maxResponseTime)

func TestValue(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions les/lespay/client/valuetracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ type NodeValueTracker struct {
basket serverBasket
reqCosts []uint64
reqValues *[]float64

lastReqCosts []uint64 // accessed by ValueTracker only
}

// init initializes a NodeValueTracker.
Expand All @@ -60,7 +58,6 @@ type NodeValueTracker struct {
func (nv *NodeValueTracker) init(now mclock.AbsTime, reqValues *[]float64) {
reqTypeCount := len(*reqValues)
nv.reqCosts = make([]uint64, reqTypeCount)
nv.lastReqCosts = nv.reqCosts
nv.lastTransfer = now
nv.reqValues = reqValues
nv.basket.init(reqTypeCount)
Expand Down Expand Up @@ -424,7 +421,6 @@ func (vt *ValueTracker) UpdateCosts(nv *NodeValueTracker, reqCosts []uint64) {
vt.lock.Lock()
defer vt.lock.Unlock()

nv.lastReqCosts = reqCosts
nv.updateCosts(reqCosts, &vt.refBasket.reqValues, vt.refBasket.reqValueFactor(reqCosts))
}

Expand Down Expand Up @@ -454,7 +450,7 @@ func (vt *ValueTracker) periodicUpdate() {
vt.refBasket.normalize()
vt.refBasket.updateReqValues()
for _, nv := range vt.connected {
nv.updateCosts(nv.lastReqCosts, &vt.refBasket.reqValues, vt.refBasket.reqValueFactor(nv.lastReqCosts))
nv.updateCosts(nv.reqCosts, &vt.refBasket.reqValues, vt.refBasket.reqValueFactor(nv.reqCosts))
}
vt.saveToDb()
}
Expand Down