engine_test.go 3.26 KB
Newer Older
1
package decision
2 3

import (
4
	"strings"
5 6
	"testing"

Brian Tiger Chow's avatar
Brian Tiger Chow committed
7 8 9
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
10
	blocks "github.com/jbenet/go-ipfs/blocks"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
11
	blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
12
	message "github.com/jbenet/go-ipfs/exchange/bitswap/message"
13
	peer "github.com/jbenet/go-ipfs/peer"
14
	testutil "github.com/jbenet/go-ipfs/util/testutil"
15 16
)

17
type peerAndEngine struct {
18
	peer.Peer
19
	Engine *Engine
20 21
}

22 23
func newPeerAndLedgermanager(idStr string) peerAndEngine {
	return peerAndEngine{
24 25
		Peer: testutil.NewPeerWithIDString(idStr),
		//Strategy: New(true),
26
		Engine: NewEngine(context.TODO(),
27
			blockstore.NewBlockstore(sync.MutexWrap(ds.NewMapDatastore()))),
28 29 30
	}
}

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

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

42 43
		sender.Engine.MessageSent(receiver.Peer, m)
		receiver.Engine.MessageReceived(sender.Peer, m)
44 45 46
	}

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

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

63
func TestBlockRecordedAsWantedAfterMessageReceived(t *testing.T) {
Jeromy's avatar
Jeromy committed
64 65
	beggar := newPeerAndLedgermanager("can't be chooser")
	chooser := newPeerAndLedgermanager("chooses JIF")
66

67
	block := blocks.NewBlock([]byte("data wanted by beggar"))
68 69

	messageFromBeggarToChooser := message.New()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
70
	messageFromBeggarToChooser.AddEntry(block.Key(), 1)
71

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

75
	if !chooser.Engine.BlockIsWantedByPeer(block.Key(), beggar.Peer) {
76 77 78 79 80 81
		t.Fatal("chooser failed to record that beggar wants block")
	}
}

func TestPeerIsAddedToPeersWhenMessageReceivedOrSent(t *testing.T) {

Jeromy's avatar
Jeromy committed
82 83
	sanfrancisco := newPeerAndLedgermanager("sf")
	seattle := newPeerAndLedgermanager("sea")
84 85 86

	m := message.New()

87 88
	sanfrancisco.Engine.MessageSent(seattle.Peer, m)
	seattle.Engine.MessageReceived(sanfrancisco.Peer, m)
89 90 91 92 93

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

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

98
	if !peerIsPartner(sanfrancisco.Peer, seattle.Engine) {
99 100 101 102
		t.Fatal("Peer wasn't added as a Partner")
	}
}

103 104
func peerIsPartner(p peer.Peer, e *Engine) bool {
	for _, partner := range e.Peers() {
105 106 107 108 109 110
		if partner.Key() == p.Key() {
			return true
		}
	}
	return false
}