linktracker_test.go 3.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package linktracker

import (
	"math/rand"
	"testing"

	gsmsg "github.com/ipfs/go-graphsync/message"
	"github.com/ipfs/go-graphsync/testbridge"
)

11
func TestBlockRefCount(t *testing.T) {
12 13 14
	linkTracker := New()
	link1 := testbridge.NewMockLink()
	link2 := testbridge.NewMockLink()
15 16
	if linkTracker.BlockRefCount(link1) != 0 || linkTracker.BlockRefCount(link2) != 0 {
		t.Fatal("Links not traversed should have refcount 0")
17 18 19 20 21 22 23 24
	}
	requestID1 := gsmsg.GraphSyncRequestID(rand.Int31())
	requestID2 := gsmsg.GraphSyncRequestID(rand.Int31())

	linkTracker.RecordLinkTraversal(requestID1, link1, true)
	linkTracker.RecordLinkTraversal(requestID1, link2, true)
	linkTracker.RecordLinkTraversal(requestID2, link1, true)

25 26
	if linkTracker.BlockRefCount(link1) == 0 || linkTracker.BlockRefCount(link2) == 0 {
		t.Fatal("Links already traversed with blocks should not have ref count 0")
27 28 29
	}

	linkTracker.FinishRequest(requestID1)
30 31
	if linkTracker.BlockRefCount(link1) == 0 || linkTracker.BlockRefCount(link2) != 0 {
		t.Fatal("Finishing request decrement refcount for block traversed by request")
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	}
}

func TestHasAllBlocks(t *testing.T) {
	linkTracker := New()
	link1 := testbridge.NewMockLink()
	link2 := testbridge.NewMockLink()
	requestID1 := gsmsg.GraphSyncRequestID(rand.Int31())
	requestID2 := gsmsg.GraphSyncRequestID(rand.Int31())

	linkTracker.RecordLinkTraversal(requestID1, link1, true)
	linkTracker.RecordLinkTraversal(requestID1, link2, false)
	linkTracker.RecordLinkTraversal(requestID2, link1, true)

	hasAllBlocksRequest1 := linkTracker.FinishRequest(requestID1)
	hasAllBlocksRequest2 := linkTracker.FinishRequest(requestID2)
	if hasAllBlocksRequest1 || !hasAllBlocksRequest2 {
		t.Fatal("A request has all blocks if and only if all link traversals occurred with blocks present")
	}
}

func TestBlockBecomesAvailable(t *testing.T) {
	linkTracker := New()
	link1 := testbridge.NewMockLink()
56
	if linkTracker.BlockRefCount(link1) != 0 {
57 58 59 60 61 62 63 64
		t.Fatal("Links not traversed should send blocks")
	}
	requestID1 := gsmsg.GraphSyncRequestID(rand.Int31())
	requestID2 := gsmsg.GraphSyncRequestID(rand.Int31())

	linkTracker.RecordLinkTraversal(requestID1, link1, false)
	linkTracker.RecordLinkTraversal(requestID2, link1, false)

65
	if linkTracker.BlockRefCount(link1) != 0 {
66 67 68 69
		t.Fatal("Links traversed without blocks should still send them if they become availabe")
	}

	linkTracker.RecordLinkTraversal(requestID1, link1, true)
70
	if linkTracker.BlockRefCount(link1) == 0 {
71 72 73 74 75 76 77 78
		t.Fatal("Links traversed with blocks should no longer send")
	}

	hasAllBlocks := linkTracker.FinishRequest(requestID1)
	if hasAllBlocks {
		t.Fatal("Even if block becomes available, traversal may be incomplete, request still should not be considered to have all blocks")
	}

79
	if linkTracker.BlockRefCount(link1) != 0 {
80 81 82
		t.Fatal("Block traversals should resend for requests that never traversed while block was present")
	}
}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

func TestMissingLink(t *testing.T) {
	linkTracker := New()
	link1 := testbridge.NewMockLink()
	link2 := testbridge.NewMockLink()
	requestID1 := gsmsg.GraphSyncRequestID(rand.Int31())
	requestID2 := gsmsg.GraphSyncRequestID(rand.Int31())

	linkTracker.RecordLinkTraversal(requestID1, link1, true)
	linkTracker.RecordLinkTraversal(requestID1, link2, false)
	linkTracker.RecordLinkTraversal(requestID2, link1, true)

	if linkTracker.IsKnownMissingLink(requestID1, link1) ||
		!linkTracker.IsKnownMissingLink(requestID1, link2) ||
		linkTracker.IsKnownMissingLink(requestID2, link1) ||
		linkTracker.IsKnownMissingLink(requestID2, link2) {
		t.Fatal("Did not record which links are known missing correctly")
	}
}