sessionrequestsplitter_test.go 2.72 KB
Newer Older
1 2 3 4 5 6 7 8 9
package sessionrequestsplitter

import (
	"context"
	"testing"

	"github.com/ipfs/go-bitswap/testutil"
)

10 11
func quadEaseOut(t float64) float64 { return t * t }

12 13
func TestSplittingRequests(t *testing.T) {
	ctx := context.Background()
14
	optimizedPeers := testutil.GenerateOptimizedPeers(10, 5, quadEaseOut)
15 16 17 18
	keys := testutil.GenerateCids(6)

	srs := New(ctx)

19
	partialRequests := srs.SplitRequest(optimizedPeers, keys)
20 21 22 23 24 25 26 27 28 29 30 31
	if len(partialRequests) != 2 {
		t.Fatal("Did not generate right number of partial requests")
	}
	for _, partialRequest := range partialRequests {
		if len(partialRequest.Peers) != 5 && len(partialRequest.Keys) != 3 {
			t.Fatal("Did not split request into even partial requests")
		}
	}
}

func TestSplittingRequestsTooFewKeys(t *testing.T) {
	ctx := context.Background()
32
	optimizedPeers := testutil.GenerateOptimizedPeers(10, 5, quadEaseOut)
33 34 35 36
	keys := testutil.GenerateCids(1)

	srs := New(ctx)

37
	partialRequests := srs.SplitRequest(optimizedPeers, keys)
38 39 40 41 42 43 44 45 46 47 48 49
	if len(partialRequests) != 1 {
		t.Fatal("Should only generate as many requests as keys")
	}
	for _, partialRequest := range partialRequests {
		if len(partialRequest.Peers) != 5 && len(partialRequest.Keys) != 1 {
			t.Fatal("Should still split peers up between keys")
		}
	}
}

func TestSplittingRequestsTooFewPeers(t *testing.T) {
	ctx := context.Background()
50
	optimizedPeers := testutil.GenerateOptimizedPeers(1, 1, quadEaseOut)
51 52 53 54
	keys := testutil.GenerateCids(6)

	srs := New(ctx)

55
	partialRequests := srs.SplitRequest(optimizedPeers, keys)
56 57 58 59 60 61 62 63 64 65 66 67
	if len(partialRequests) != 1 {
		t.Fatal("Should only generate as many requests as peers")
	}
	for _, partialRequest := range partialRequests {
		if len(partialRequest.Peers) != 1 && len(partialRequest.Keys) != 6 {
			t.Fatal("Should not split keys if there are not enough peers")
		}
	}
}

func TestSplittingRequestsIncreasingSplitDueToDupes(t *testing.T) {
	ctx := context.Background()
68
	optimizedPeers := testutil.GenerateOptimizedPeers(maxSplit, maxSplit, quadEaseOut)
69 70 71 72 73 74 75 76
	keys := testutil.GenerateCids(maxSplit)

	srs := New(ctx)

	for i := 0; i < maxSplit+minReceivedToAdjustSplit; i++ {
		srs.RecordDuplicateBlock()
	}

77
	partialRequests := srs.SplitRequest(optimizedPeers, keys)
78 79 80 81 82 83 84
	if len(partialRequests) != maxSplit {
		t.Fatal("Did not adjust split up as duplicates came in")
	}
}

func TestSplittingRequestsDecreasingSplitDueToNoDupes(t *testing.T) {
	ctx := context.Background()
85
	optimizedPeers := testutil.GenerateOptimizedPeers(maxSplit, maxSplit, quadEaseOut)
86 87 88 89 90 91 92 93
	keys := testutil.GenerateCids(maxSplit)

	srs := New(ctx)

	for i := 0; i < 5+minReceivedToAdjustSplit; i++ {
		srs.RecordUniqueBlock()
	}

94
	partialRequests := srs.SplitRequest(optimizedPeers, keys)
95 96 97 98
	if len(partialRequests) != 1 {
		t.Fatal("Did not adjust split down as unique blocks came in")
	}
}