responsebuilder_test.go 3.54 KB
Newer Older
1 2 3 4 5
package responsebuilder

import (
	"fmt"
	"math/rand"
6
	"reflect"
7 8 9
	"testing"

	gsmsg "github.com/ipfs/go-graphsync/message"
10
	"github.com/ipfs/go-graphsync/metadata"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	"github.com/ipfs/go-graphsync/testbridge"
	"github.com/ipfs/go-graphsync/testutil"
	"github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/linking/cid"
)

func TestMessageBuilding(t *testing.T) {
	ipldBridge := testbridge.NewMockIPLDBridge()
	rb := New()
	blocks := testutil.GenerateBlocksOfSize(3, 100)
	links := make([]ipld.Link, 0, len(blocks))
	for _, block := range blocks {
		links = append(links, cidlink.Link{Cid: block.Cid()})
	}
	requestID1 := gsmsg.GraphSyncRequestID(rand.Int31())
	requestID2 := gsmsg.GraphSyncRequestID(rand.Int31())
	requestID3 := gsmsg.GraphSyncRequestID(rand.Int31())
	requestID4 := gsmsg.GraphSyncRequestID(rand.Int31())

	rb.AddLink(requestID1, links[0], true)
	rb.AddLink(requestID1, links[1], false)
	rb.AddLink(requestID1, links[2], true)

34
	rb.AddCompletedRequest(requestID1, gsmsg.RequestCompletedPartial)
35 36 37 38

	rb.AddLink(requestID2, links[1], true)
	rb.AddLink(requestID2, links[2], true)

39
	rb.AddCompletedRequest(requestID2, gsmsg.RequestCompletedFull)
40 41 42 43

	rb.AddLink(requestID3, links[0], true)
	rb.AddLink(requestID3, links[1], true)

44
	rb.AddCompletedRequest(requestID4, gsmsg.RequestCompletedFull)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

	for _, block := range blocks {
		rb.AddBlock(block)
	}

	responses, sentBlocks, err := rb.Build(ipldBridge)

	if err != nil {
		t.Fatal("Error building responses")
	}

	if len(responses) != 4 {
		t.Fatal("Assembled wrong number of responses")
	}

	response1, err := findResponseForRequestID(responses, requestID1)
	if err != nil || response1.Status() != gsmsg.RequestCompletedPartial {
		t.Fatal("did not generate completed partial response")
	}

65 66
	response1Metadata, err := metadata.DecodeMetadata(response1.Extra(), ipldBridge)
	if err != nil || !reflect.DeepEqual(response1Metadata, metadata.Metadata{
67 68 69
		links[0]: true,
		links[1]: false,
		links[2]: true,
70 71 72 73
	}) {
		t.Fatal("Metadata did not match expected")
	}

74 75 76 77
	response2, err := findResponseForRequestID(responses, requestID2)
	if err != nil || response2.Status() != gsmsg.RequestCompletedFull {
		t.Fatal("did not generate completed partial response")
	}
78 79
	response2Metadata, err := metadata.DecodeMetadata(response2.Extra(), ipldBridge)
	if err != nil || !reflect.DeepEqual(response2Metadata, metadata.Metadata{
80 81
		links[1]: true,
		links[2]: true,
82 83 84
	}) {
		t.Fatal("Metadata did not match expected")
	}
85 86 87 88 89

	response3, err := findResponseForRequestID(responses, requestID3)
	if err != nil || response3.Status() != gsmsg.PartialResponse {
		t.Fatal("did not generate completed partial response")
	}
90 91
	response3Metadata, err := metadata.DecodeMetadata(response3.Extra(), ipldBridge)
	if err != nil || !reflect.DeepEqual(response3Metadata, metadata.Metadata{
92 93
		links[0]: true,
		links[1]: true,
94 95 96
	}) {
		t.Fatal("Metadata did not match expected")
	}
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

	response4, err := findResponseForRequestID(responses, requestID4)
	if err != nil || response4.Status() != gsmsg.RequestCompletedFull {
		t.Fatal("did not generate completed partial response")
	}

	if len(sentBlocks) != len(blocks) {
		t.Fatal("Did not send all blocks")
	}

	for _, block := range sentBlocks {
		if !testutil.ContainsBlock(blocks, block) {
			t.Fatal("Sent incorrect block")
		}
	}
}

func findResponseForRequestID(responses []gsmsg.GraphSyncResponse, requestID gsmsg.GraphSyncRequestID) (gsmsg.GraphSyncResponse, error) {
	for _, response := range responses {
		if response.RequestID() == requestID {
			return response, nil
		}
	}
	return gsmsg.GraphSyncResponse{}, fmt.Errorf("Response Not Found")
}