mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
les/lespay: add tests
This commit is contained in:
parent
8c7a91cf1a
commit
a4a4370991
3 changed files with 44 additions and 5 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -50,8 +50,6 @@ type NodeValueTracker struct {
|
|||
basket serverBasket
|
||||
reqCosts []uint64
|
||||
reqValues *[]float64
|
||||
|
||||
lastReqCosts []uint64 // accessed by ValueTracker only
|
||||
}
|
||||
|
||||
// init initializes a NodeValueTracker.
|
||||
|
|
@ -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)
|
||||
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue