strategy_test.go 2.76 KB
Newer Older
1 2 3
package strategy

import (
4
	"strings"
5 6
	"testing"

7
	blocks "github.com/jbenet/go-ipfs/blocks"
8
	message "github.com/jbenet/go-ipfs/exchange/bitswap/message"
9
	peer "github.com/jbenet/go-ipfs/peer"
10
	testutil "github.com/jbenet/go-ipfs/util/testutil"
11 12 13
)

type peerAndStrategist struct {
14
	peer.Peer
15
	Strategy
16 17 18 19
}

func newPeerAndStrategist(idStr string) peerAndStrategist {
	return peerAndStrategist{
20
		Peer:     testutil.NewPeerWithIDString(idStr),
21
		Strategy: New(true),
22 23 24
	}
}

25 26 27 28 29 30 31 32 33
func TestConsistentAccounting(t *testing.T) {
	sender := newPeerAndStrategist("Ernie")
	receiver := newPeerAndStrategist("Bert")

	// Send messages from Ernie to Bert
	for i := 0; i < 1000; i++ {

		m := message.New()
		content := []string{"this", "is", "message", "i"}
Jeromy's avatar
Jeromy committed
34
		m.AddBlock(blocks.NewBlock([]byte(strings.Join(content, " "))))
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

		sender.MessageSent(receiver.Peer, m)
		receiver.MessageReceived(sender.Peer, m)
	}

	// Ensure sender records the change
	if sender.NumBytesSentTo(receiver.Peer) == 0 {
		t.Fatal("Sent bytes were not recorded")
	}

	// Ensure sender and receiver have the same values
	if sender.NumBytesSentTo(receiver.Peer) != receiver.NumBytesReceivedFrom(sender.Peer) {
		t.Fatal("Inconsistent book-keeping. Strategies don't agree")
	}

	// Ensure sender didn't record receving anything. And that the receiver
	// didn't record sending anything
	if receiver.NumBytesSentTo(sender.Peer) != 0 || sender.NumBytesReceivedFrom(receiver.Peer) != 0 {
		t.Fatal("Bert didn't send bytes to Ernie")
	}
}

57 58 59 60
func TestBlockRecordedAsWantedAfterMessageReceived(t *testing.T) {
	beggar := newPeerAndStrategist("can't be chooser")
	chooser := newPeerAndStrategist("chooses JIF")

61
	block := blocks.NewBlock([]byte("data wanted by beggar"))
62 63

	messageFromBeggarToChooser := message.New()
Jeromy's avatar
Jeromy committed
64
	messageFromBeggarToChooser.AddEntry(block.Key(), 1, false)
65 66 67 68

	chooser.MessageReceived(beggar.Peer, messageFromBeggarToChooser)
	// for this test, doesn't matter if you record that beggar sent

69
	if !chooser.BlockIsWantedByPeer(block.Key(), beggar.Peer) {
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
		t.Fatal("chooser failed to record that beggar wants block")
	}
}

func TestPeerIsAddedToPeersWhenMessageReceivedOrSent(t *testing.T) {

	sanfrancisco := newPeerAndStrategist("sf")
	seattle := newPeerAndStrategist("sea")

	m := message.New()

	sanfrancisco.MessageSent(seattle.Peer, m)
	seattle.MessageReceived(sanfrancisco.Peer, m)

	if seattle.Peer.Key() == sanfrancisco.Peer.Key() {
		t.Fatal("Sanity Check: Peers have same Key!")
	}

88
	if !peerIsPartner(seattle.Peer, sanfrancisco.Strategy) {
89 90 91
		t.Fatal("Peer wasn't added as a Partner")
	}

92
	if !peerIsPartner(sanfrancisco.Peer, seattle.Strategy) {
93 94 95 96
		t.Fatal("Peer wasn't added as a Partner")
	}
}

97
func peerIsPartner(p peer.Peer, s Strategy) bool {
98 99 100 101 102 103 104
	for _, partner := range s.Peers() {
		if partner.Key() == p.Key() {
			return true
		}
	}
	return false
}