-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Feat: remove circular dependencies in merkledag package tests #4704
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee64729
Feat: remove circular dependencies in merkledag package tests
hsanjuan c42e684
Golint merkledag_test.go
hsanjuan 701fb82
merkledag_test: address #4704 review comments
hsanjuan 050a699
merkledag_test.go: Handle short reads in makeTestDAG
hsanjuan cd9c8c5
Use ReadFull. Remove duplicated code
hsanjuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,11 @@ import ( | |
bserv "github.com/ipfs/go-ipfs/blockservice" | ||
bstest "github.com/ipfs/go-ipfs/blockservice/test" | ||
offline "github.com/ipfs/go-ipfs/exchange/offline" | ||
imp "github.com/ipfs/go-ipfs/importer" | ||
. "github.com/ipfs/go-ipfs/merkledag" | ||
mdpb "github.com/ipfs/go-ipfs/merkledag/pb" | ||
dstest "github.com/ipfs/go-ipfs/merkledag/test" | ||
uio "github.com/ipfs/go-ipfs/unixfs/io" | ||
|
||
u "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util" | ||
chunker "gx/ipfs/QmWo8jYc19ppG7YoTsrr2kEtLRbARTJho5oNXFTR6B7Peq/go-ipfs-chunker" | ||
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" | ||
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" | ||
blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format" | ||
|
@@ -129,26 +126,73 @@ func TestBatchFetchDupBlock(t *testing.T) { | |
runBatchFetchTest(t, read) | ||
} | ||
|
||
// makeTestDAG creates a simple DAG from the data in a reader. | ||
// First, a node is created from each 512 bytes of data from the reader | ||
// (like a the Size chunker would do). Then all nodes are added as children | ||
// to a root node, which is returned. | ||
func makeTestDAG(t *testing.T, read io.Reader, ds ipld.DAGService) ipld.Node { | ||
p := make([]byte, 512) | ||
nodes := []*ProtoNode{} | ||
var err error | ||
_, err = read.Read(p) | ||
for err == nil { | ||
protoNode := NodeWithData(p) | ||
nodes = append(nodes, protoNode) | ||
_, err = read.Read(p) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. Needs a ReadFull. I'd just deduplicate the code and have a |
||
} | ||
|
||
if err != io.EOF { | ||
t.Fatal(err) | ||
} | ||
|
||
ctx := context.Background() | ||
// Add a root referencing all created nodes | ||
root := NodeWithData(nil) | ||
for _, n := range nodes { | ||
root.AddNodeLink(n.Cid().String(), n) | ||
err := ds.Add(ctx, n) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
err = ds.Add(ctx, root) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return root | ||
} | ||
|
||
// makeTestDAGReader takes the root node as returned by makeTestDAG and | ||
// provides a reader that reads all the RawData from that node and its children. | ||
func makeTestDAGReader(t *testing.T, root ipld.Node, ds ipld.DAGService) io.Reader { | ||
ctx := context.Background() | ||
buf := new(bytes.Buffer) | ||
buf.Write(root.RawData()) | ||
for _, l := range root.Links() { | ||
n, err := ds.Get(ctx, l.Cid) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
_, err = buf.Write(n.RawData()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
return buf | ||
} | ||
|
||
func runBatchFetchTest(t *testing.T, read io.Reader) { | ||
ctx := context.Background() | ||
var dagservs []ipld.DAGService | ||
for _, bsi := range bstest.Mocks(5) { | ||
dagservs = append(dagservs, NewDAGService(bsi)) | ||
} | ||
|
||
spl := chunker.NewSizeSplitter(read, 512) | ||
|
||
root, err := imp.BuildDagFromReader(dagservs[0], spl) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
root := makeTestDAG(t, read, dagservs[0]) | ||
|
||
t.Log("finished setup.") | ||
|
||
dagr, err := uio.NewDagReader(ctx, root, dagservs[0]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
dagr := makeTestDAGReader(t, root, dagservs[0]) | ||
|
||
expected, err := ioutil.ReadAll(dagr) | ||
if err != nil { | ||
|
@@ -181,11 +225,9 @@ func runBatchFetchTest(t *testing.T, read io.Reader) { | |
if !ok { | ||
errs <- ErrNotProtobuf | ||
} | ||
|
||
read, err := uio.NewDagReader(ctx, firstpb, dagservs[i]) | ||
if err != nil { | ||
errs <- err | ||
} | ||
_ = firstpb | ||
_ = expected | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't need these lines. |
||
read := makeTestDAGReader(t, firstpb, dagservs[i]) | ||
datagot, err := ioutil.ReadAll(read) | ||
if err != nil { | ||
errs <- err | ||
|
@@ -228,12 +270,9 @@ func TestFetchGraph(t *testing.T) { | |
} | ||
|
||
read := io.LimitReader(u.NewTimeSeededRand(), 1024*32) | ||
root, err := imp.BuildDagFromReader(dservs[0], chunker.NewSizeSplitter(read, 512)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
root := makeTestDAG(t, read, dservs[0]) | ||
|
||
err = FetchGraph(context.TODO(), root.Cid(), dservs[1]) | ||
err := FetchGraph(context.TODO(), root.Cid(), dservs[1]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -254,14 +293,11 @@ func TestEnumerateChildren(t *testing.T) { | |
ds := NewDAGService(bsi[0]) | ||
|
||
read := io.LimitReader(u.NewTimeSeededRand(), 1024*1024) | ||
root, err := imp.BuildDagFromReader(ds, chunker.NewSizeSplitter(read, 512)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
root := makeTestDAG(t, read, ds) | ||
|
||
set := cid.NewSet() | ||
|
||
err = EnumerateChildren(context.Background(), ds.GetLinks, root.Cid(), set.Visit) | ||
err := EnumerateChildren(context.Background(), ds.GetLinks, root.Cid(), set.Visit) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be using
io.ReadFull
(Read
isn't guaranteed to fill the buffer). It should probably also handle short reads. It may currently "work" but I'm worried that users will misuse it.