testutil.go 3.79 KB
Newer Older
1 2 3
package testutil

import (
Jakub Sztandera's avatar
Jakub Sztandera committed
4
	"math/rand"
5

6
	bsmsg "github.com/ipfs/go-bitswap/message"
7
	bssd "github.com/ipfs/go-bitswap/internal/sessiondata"
8
	"github.com/ipfs/go-bitswap/wantlist"
9
	blocks "github.com/ipfs/go-block-format"
10 11
	cid "github.com/ipfs/go-cid"
	blocksutil "github.com/ipfs/go-ipfs-blocksutil"
Raúl Kripalani's avatar
Raúl Kripalani committed
12
	peer "github.com/libp2p/go-libp2p-core/peer"
13 14 15 16
)

var blockGenerator = blocksutil.NewBlockGenerator()
var prioritySeq int
17 18 19 20 21

// GenerateBlocksOfSize generates a series of blocks of the given byte size
func GenerateBlocksOfSize(n int, size int64) []blocks.Block {
	generatedBlocks := make([]blocks.Block, 0, n)
	for i := 0; i < n; i++ {
Jakub Sztandera's avatar
Jakub Sztandera committed
22
		// rand.Read never errors
Steven Allen's avatar
Steven Allen committed
23
		buf := make([]byte, size)
Jakub Sztandera's avatar
Jakub Sztandera committed
24 25
		rand.Read(buf)
		b := blocks.NewBlock(buf)
26 27 28 29 30
		generatedBlocks = append(generatedBlocks, b)

	}
	return generatedBlocks
}
31

32
// GenerateCids produces n content identifiers.
33 34 35 36 37 38 39 40 41
func GenerateCids(n int) []cid.Cid {
	cids := make([]cid.Cid, 0, n)
	for i := 0; i < n; i++ {
		c := blockGenerator.Next().Cid()
		cids = append(cids, c)
	}
	return cids
}

42
// GenerateMessageEntries makes fake bitswap message entries.
43 44
func GenerateMessageEntries(n int, isCancel bool) []bsmsg.Entry {
	bsmsgs := make([]bsmsg.Entry, 0, n)
45 46
	for i := 0; i < n; i++ {
		prioritySeq++
47
		msg := bsmsg.Entry{
48 49 50 51 52 53 54 55 56 57
			Entry:  wantlist.NewRefEntry(blockGenerator.Next().Cid(), prioritySeq),
			Cancel: isCancel,
		}
		bsmsgs = append(bsmsgs, msg)
	}
	return bsmsgs
}

var peerSeq int

58
// GeneratePeers creates n peer ids.
59 60 61 62 63 64 65 66 67 68
func GeneratePeers(n int) []peer.ID {
	peerIds := make([]peer.ID, 0, n)
	for i := 0; i < n; i++ {
		peerSeq++
		p := peer.ID(peerSeq)
		peerIds = append(peerIds, p)
	}
	return peerIds
}

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
// GenerateOptimizedPeers creates n peer ids,
// with optimization fall off up to optCount, curveFunc to scale it
func GenerateOptimizedPeers(n int, optCount int, curveFunc func(float64) float64) []bssd.OptimizedPeer {
	peers := GeneratePeers(n)
	optimizedPeers := make([]bssd.OptimizedPeer, 0, n)
	for i, peer := range peers {
		var optimizationRating float64
		if i <= optCount {
			optimizationRating = 1.0 - float64(i)/float64(optCount)
		} else {
			optimizationRating = 0.0
		}
		optimizationRating = curveFunc(optimizationRating)
		optimizedPeers = append(optimizedPeers, bssd.OptimizedPeer{Peer: peer, OptimizationRating: optimizationRating})
	}
	return optimizedPeers
}

87 88
var nextSession uint64

89
// GenerateSessionID make a unit session identifier.
90 91 92 93 94
func GenerateSessionID() uint64 {
	nextSession++
	return uint64(nextSession)
}

95
// ContainsPeer returns true if a peer is found n a list of peers.
96 97 98 99 100 101 102 103
func ContainsPeer(peers []peer.ID, p peer.ID) bool {
	for _, n := range peers {
		if p == n {
			return true
		}
	}
	return false
}
hannahhoward's avatar
hannahhoward committed
104

105 106 107 108 109
// IndexOf returns the index of a given cid in an array of blocks
func IndexOf(blks []blocks.Block, c cid.Cid) int {
	for i, n := range blks {
		if n.Cid() == c {
			return i
hannahhoward's avatar
hannahhoward committed
110 111
		}
	}
112 113 114 115 116 117
	return -1
}

// ContainsBlock returns true if a block is found n a list of blocks
func ContainsBlock(blks []blocks.Block, block blocks.Block) bool {
	return IndexOf(blks, block.Cid()) != -1
hannahhoward's avatar
hannahhoward committed
118
}
dirkmc's avatar
dirkmc committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

// ContainsKey returns true if a key is found n a list of CIDs.
func ContainsKey(ks []cid.Cid, c cid.Cid) bool {
	for _, k := range ks {
		if c == k {
			return true
		}
	}
	return false
}

// MatchKeysIgnoreOrder returns true if the lists of CIDs match (even if
// they're in a different order)
func MatchKeysIgnoreOrder(ks1 []cid.Cid, ks2 []cid.Cid) bool {
	if len(ks1) != len(ks2) {
		return false
	}

	for _, k := range ks1 {
		if !ContainsKey(ks2, k) {
			return false
		}
	}
	return true
}

// MatchPeersIgnoreOrder returns true if the lists of peers match (even if
// they're in a different order)
func MatchPeersIgnoreOrder(ps1 []peer.ID, ps2 []peer.ID) bool {
	if len(ps1) != len(ps2) {
		return false
	}

	for _, p := range ps1 {
		if !ContainsPeer(ps2, p) {
			return false
		}
	}
	return true
}