Skip to content

Commit

Permalink
Merge branch 'master' into tsachi/merge15
Browse files Browse the repository at this point in the history
  • Loading branch information
tsachiherman committed Jul 12, 2021
2 parents 6fdf336 + 10e91da commit 2f2761a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
20 changes: 15 additions & 5 deletions catchup/peerSelector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package catchup

import (
"bytes"
"context"
"encoding/binary"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -253,6 +255,7 @@ func TestHistoricData(t *testing.T) {
var counters [5]int
for i := 0; i < 1000; i++ {
psp, getPeerErr := peerSelector.getNextPeer()
require.NoError(t, getPeerErr)
peer := psp.Peer

switch peer.(*mockHTTPPeer).address {
Expand All @@ -266,11 +269,11 @@ func TestHistoricData(t *testing.T) {
counters[3]++
case "b2":
counters[4]++
default:
require.Fail(t, "unexpected peer address `%s`", peer.(*mockHTTPPeer).address)
}

require.NoError(t, getPeerErr)
randVal := float64(crypto.RandUint64()%uint64(100)) / 100
randVal = randVal + 1
randVal := peerSelectorTestRandVal(t, i)
if randVal < 1.98 {
var duration time.Duration
switch peer.(*mockHTTPPeer).address {
Expand All @@ -294,6 +297,14 @@ func TestHistoricData(t *testing.T) {
require.Equal(t, counters[4], 0)
}

func peerSelectorTestRandVal(t *testing.T, seed int) float64 {
iterationDigest := crypto.Hash([]byte{byte(seed), byte(seed >> 8), byte(seed >> 16)})
randUint64, err := binary.ReadUvarint(bytes.NewReader(append([]byte{0}, iterationDigest[:]...)))
require.NoError(t, err)
randVal := float64(randUint64%uint64(100)) / 100
randVal = randVal + 1
return randVal
}
func TestPeersDownloadFailed(t *testing.T) {

peers1 := []network.Peer{&mockHTTPPeer{address: "a1"}, &mockHTTPPeer{address: "a2"}, &mockHTTPPeer{address: "a3"}}
Expand Down Expand Up @@ -334,8 +345,7 @@ func TestPeersDownloadFailed(t *testing.T) {
require.NoError(t, getPeerErr)

if i < 500 || peerAddress(peer) == "b1" || peerAddress(peer) == "b2" {
randVal := float64(crypto.RandUint64()%uint64(100)) / 100
randVal = randVal + 1
randVal := peerSelectorTestRandVal(t, i)
if randVal < 1.98 {
duration := time.Duration(100 * float64(time.Millisecond) * randVal)
peerRank := peerSelector.peerDownloadDurationToRank(psp, duration)
Expand Down
64 changes: 38 additions & 26 deletions protocol/codec_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ func RandomizeObject(template interface{}) (interface{}, error) {
if tt.Kind() != reflect.Ptr {
return nil, fmt.Errorf("RandomizeObject: must be ptr")
}

v := reflect.New(tt.Elem())
err := randomizeValue(v.Elem(), tt.String(), "")
hasAllocBound := checkBoundsLimitingTag(v.Elem(), v.Elem().Type().Name(), "codec:\",\"", false)
pseudoTag := ""
if hasAllocBound {
// we emulate a pseudo tag here on an item that has a alloc bound msgp directive.
pseudoTag = "codec:\",allocbound=1\""
}
err := randomizeValue(v.Elem(), tt.String(), pseudoTag)
return v.Interface(), err
}

Expand Down Expand Up @@ -90,7 +95,31 @@ func printWarning(warnMsg string) {
var testedDatatypesForAllocBound = map[string]bool{}
var testedDatatypesForAllocBoundMu = deadlock.Mutex{}

func checkBoundsLimitingTag(val reflect.Value, datapath string, structTag string) (hasAllocBound bool) {
func checkMsgpAllocBoundDirective(dataType reflect.Type) bool {
// does any of the go files in the package directory has the msgp:allocbound defined for that datatype ?
gopath := os.Getenv("GOPATH")
packageFilesPath := path.Join(gopath, "src", dataType.PkgPath())
packageFiles := []string{}
filepath.Walk(packageFilesPath, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".go" {
packageFiles = append(packageFiles, path)
}
return nil
})
for _, packageFile := range packageFiles {
fileBytes, err := ioutil.ReadFile(packageFile)
if err != nil {
continue
}
if strings.Index(string(fileBytes), fmt.Sprintf("msgp:allocbound %s", dataType.Name())) != -1 {
// message pack alloc bound definition was found.
return true
}
}
return false
}

func checkBoundsLimitingTag(val reflect.Value, datapath string, structTag string, warnMissingAllocBound bool) (hasAllocBound bool) {
if structTag == "" {
return
}
Expand Down Expand Up @@ -132,28 +161,11 @@ func checkBoundsLimitingTag(val reflect.Value, datapath string, structTag string

if val.Type().Name() != "" {
// does any of the go files in the package directory has the msgp:allocbound defined for that datatype ?
gopath := os.Getenv("GOPATH")
packageFilesPath := path.Join(gopath, "src", val.Type().PkgPath())
packageFiles := []string{}
filepath.Walk(packageFilesPath, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".go" {
packageFiles = append(packageFiles, path)
}
return nil
})
for _, packageFile := range packageFiles {
fileBytes, err := ioutil.ReadFile(packageFile)
if err != nil {
continue
}
if strings.Index(string(fileBytes), fmt.Sprintf("msgp:allocbound %s", val.Type().Name())) != -1 {
// message pack alloc bound definition was found.
hasAllocBound = true
return
}
}
hasAllocBound = checkMsgpAllocBoundDirective(val.Type())
}
if warnMissingAllocBound {
printWarning(fmt.Sprintf("%s %s does not have an allocbound defined - %s", objType, datapath, val.Type().PkgPath()))
}
printWarning(fmt.Sprintf("%s %s does not have an allocbound defined - %s", objType, datapath, val.Type().PkgPath()))
return
}

Expand Down Expand Up @@ -201,7 +213,7 @@ func randomizeValue(v reflect.Value, datapath string, tag string) error {
}
}
case reflect.Slice:
hasAllocBound := checkBoundsLimitingTag(v, datapath, tag)
hasAllocBound := checkBoundsLimitingTag(v, datapath, tag, true)
l := rand.Int() % 32
if hasAllocBound {
l = 1
Expand All @@ -217,7 +229,7 @@ func randomizeValue(v reflect.Value, datapath string, tag string) error {
case reflect.Bool:
v.SetBool(rand.Uint32()%2 == 0)
case reflect.Map:
hasAllocBound := checkBoundsLimitingTag(v, datapath, tag)
hasAllocBound := checkBoundsLimitingTag(v, datapath, tag, true)
mt := v.Type()
v.Set(reflect.MakeMap(mt))
l := rand.Int() % 32
Expand Down

0 comments on commit 2f2761a

Please sign in to comment.