ledgerset_test.go 2.95 KB
Newer Older
1 2 3
package strategy

import (
4
	"strings"
5 6
	"testing"

Jeromy's avatar
Jeromy committed
7 8
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"

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

Jeromy's avatar
Jeromy committed
15
type peerAndLedgermanager struct {
16
	peer.Peer
Jeromy's avatar
Jeromy committed
17
	ls *LedgerManager
18 19
}

Jeromy's avatar
Jeromy committed
20 21
func newPeerAndLedgermanager(idStr string) peerAndLedgermanager {
	return peerAndLedgermanager{
22 23
		Peer: testutil.NewPeerWithIDString(idStr),
		//Strategy: New(true),
Jeromy's avatar
Jeromy committed
24
		ls: NewLedgerManager(nil, context.TODO()),
25 26 27
	}
}

28
func TestConsistentAccounting(t *testing.T) {
Jeromy's avatar
Jeromy committed
29 30
	sender := newPeerAndLedgermanager("Ernie")
	receiver := newPeerAndLedgermanager("Bert")
31 32 33 34 35 36

	// 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
37
		m.AddBlock(blocks.NewBlock([]byte(strings.Join(content, " "))))
38

39 40
		sender.ls.MessageSent(receiver.Peer, m)
		receiver.ls.MessageReceived(sender.Peer, m)
41 42 43
	}

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

	// Ensure sender and receiver have the same values
49
	if sender.ls.NumBytesSentTo(receiver.Peer) != receiver.ls.NumBytesReceivedFrom(sender.Peer) {
50 51 52 53 54
		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
55
	if receiver.ls.NumBytesSentTo(sender.Peer) != 0 || sender.ls.NumBytesReceivedFrom(receiver.Peer) != 0 {
56 57 58 59
		t.Fatal("Bert didn't send bytes to Ernie")
	}
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
60
func TestBlockRecordedAsWantedAfterMessageReceived(t *testing.T) {
Jeromy's avatar
Jeromy committed
61 62
	beggar := newPeerAndLedgermanager("can't be chooser")
	chooser := newPeerAndLedgermanager("chooses JIF")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
63

64
	block := blocks.NewBlock([]byte("data wanted by beggar"))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
65 66

	messageFromBeggarToChooser := message.New()
Jeromy's avatar
Jeromy committed
67
	messageFromBeggarToChooser.AddEntry(block.Key(), 1, false)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
68

69
	chooser.ls.MessageReceived(beggar.Peer, messageFromBeggarToChooser)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
70 71
	// for this test, doesn't matter if you record that beggar sent

72
	if !chooser.ls.BlockIsWantedByPeer(block.Key(), beggar.Peer) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
73 74 75 76
		t.Fatal("chooser failed to record that beggar wants block")
	}
}

77 78
func TestPeerIsAddedToPeersWhenMessageReceivedOrSent(t *testing.T) {

Jeromy's avatar
Jeromy committed
79 80
	sanfrancisco := newPeerAndLedgermanager("sf")
	seattle := newPeerAndLedgermanager("sea")
81 82 83

	m := message.New()

84 85
	sanfrancisco.ls.MessageSent(seattle.Peer, m)
	seattle.ls.MessageReceived(sanfrancisco.Peer, m)
86 87 88 89 90

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

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

95
	if !peerIsPartner(sanfrancisco.Peer, seattle.ls) {
96 97 98 99
		t.Fatal("Peer wasn't added as a Partner")
	}
}

Jeromy's avatar
Jeromy committed
100
func peerIsPartner(p peer.Peer, ls *LedgerManager) bool {
101
	for _, partner := range ls.Peers() {
102 103 104 105 106 107
		if partner.Key() == p.Key() {
			return true
		}
	}
	return false
}