Skip to content

Commit

Permalink
test promised don't get allocated where not wanted
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <[email protected]>
  • Loading branch information
whyrusleeping committed Jan 2, 2018
1 parent 092df58 commit dea8c4d
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions unixfs/io/dagreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"io/ioutil"
"math/rand"
"strings"
"testing"

Expand Down Expand Up @@ -72,6 +73,55 @@ func TestSeekAndRead(t *testing.T) {
}
}

func TestSeekAndReadLarge(t *testing.T) {
dserv := testu.GetDAGServ()
inbuf := make([]byte, 20000)
rand.Read(inbuf)

node := testu.GetNode(t, dserv, inbuf, testu.UseProtoBufLeaves)
ctx, closer := context.WithCancel(context.Background())
defer closer()

reader, err := NewDagReader(ctx, node, dserv)
if err != nil {
t.Fatal(err)
}

_, err = reader.Seek(10000, io.SeekStart)
if err != nil {
t.Fatal(err)
}

buf := make([]byte, 100)
_, err = io.ReadFull(reader, buf)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(buf, inbuf[10000:10100]) {
t.Fatal("seeked read failed")
}

pbdr := reader.(*pbDagReader)
var count int
for i, p := range pbdr.promises {
if i > 20 && i < 30 {
if p == nil {
t.Fatal("expected index to be not nil: ", i)
}
count++
} else {
if p != nil {
t.Fatal("expected index to be nil: ", i)
}
}
}
// -1 because we read some and it cleared one
if count != preloadSize-1 {
t.Fatalf("expected %d preloaded promises, got %d", preloadSize-1, count)
}
}

func TestRelativeSeek(t *testing.T) {
dserv := testu.GetDAGServ()
ctx, closer := context.WithCancel(context.Background())
Expand Down

0 comments on commit dea8c4d

Please sign in to comment.