engine_test.go 2.58 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

11
	blocks "github.com/jbenet/go-ipfs/blocks"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
12
	blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
13
	message "github.com/jbenet/go-ipfs/exchange/bitswap/message"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
	peer "github.com/jbenet/go-ipfs/p2p/peer"
15 16
)

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

22 23
func newPeerAndLedgermanager(idStr string) peerAndEngine {
	return peerAndEngine{
24
		Peer: peer.ID(idStr),
25
		//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
Brian Tiger Chow's avatar
Brian Tiger Chow committed
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
Brian Tiger Chow's avatar
Brian Tiger Chow committed
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
Brian Tiger Chow's avatar
Brian Tiger Chow committed
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 64
func TestPeerIsAddedToPeersWhenMessageReceivedOrSent(t *testing.T) {

Jeromy's avatar
Jeromy committed
65 66
	sanfrancisco := newPeerAndLedgermanager("sf")
	seattle := newPeerAndLedgermanager("sea")
67 68 69

	m := message.New()

70 71
	sanfrancisco.Engine.MessageSent(seattle.Peer, m)
	seattle.Engine.MessageReceived(sanfrancisco.Peer, m)
72

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

77
	if !peerIsPartner(seattle.Peer, sanfrancisco.Engine) {
78 79 80
		t.Fatal("Peer wasn't added as a Partner")
	}

81
	if !peerIsPartner(sanfrancisco.Peer, seattle.Engine) {
82 83 84 85
		t.Fatal("Peer wasn't added as a Partner")
	}
}

86
func peerIsPartner(p peer.ID, e *Engine) bool {
87
	for _, partner := range e.Peers() {
88
		if partner == p {
89 90 91 92 93
			return true
		}
	}
	return false
}