Skip to content

Commit

Permalink
Add test for EnumerateChildren progress reporting.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Feb 13, 2017
1 parent 8f952b1 commit 995c4ec
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions merkledag/merkledag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -547,3 +548,81 @@ func TestEnumerateAsyncFailsNotFound(t *testing.T) {
t.Fatal("this should have failed")
}
}

func TestProgressIndicator(t *testing.T) {
testProgressIndicator(t, 5)
}

func TestProgressIndicatorNoChildren(t *testing.T) {
testProgressIndicator(t, 0)
}

func testProgressIndicator(t *testing.T, depth int) {
ds := dstest.Mock()

top, numChildren := mkDag(ds, depth)
cset := cid.NewSet()

v := new(ProgressTracker)
ctx := context.WithValue(context.Background(), "progress", v)

err := EnumerateChildren(ctx, ds, top, cset.Visit, false)
if err != nil {
t.Fatal(err)
}

if v.Total != numChildren {
t.Errorf("wrong number of children reported in progress indicator, expected %d, got %d",
numChildren, v.Total)
}
}

func mkDag(ds DAGService, depth int) (*cid.Cid, int) {
totalChildren := 0
f := func() *ProtoNode {
p := new(ProtoNode)
buf := make([]byte, 16)
rand.Read(buf)

p.SetData(buf)
_, err := ds.Add(p)
if err != nil {
panic(err)
}
return p
}

for i := 0; i < depth; i++ {
thisf := f
f = func() *ProtoNode {
pn := mkNodeWithChildren(thisf, 10)
_, err := ds.Add(pn)
if err != nil {
panic(err)
}
totalChildren += 10
return pn
}
}

nd := f()
c, err := ds.Add(nd)
if err != nil {
panic(err)
}

return c, totalChildren
}

func mkNodeWithChildren(getChild func() *ProtoNode, width int) *ProtoNode {
cur := new(ProtoNode)

for i := 0; i < width; i++ {
c := getChild()
if err := cur.AddNodeLinkClean(fmt.Sprint(i), c); err != nil {
panic(err)
}
}

return cur
}

0 comments on commit 995c4ec

Please sign in to comment.