routing_test.go 3.42 KB
Newer Older
1
package mock
Brian Tiger Chow's avatar
Brian Tiger Chow committed
2 3 4 5 6 7 8 9 10 11 12 13

import (
	"bytes"
	"testing"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	"github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"
)

func TestKeyNotFound(t *testing.T) {

14
	vrs := VirtualRoutingServer()
15
	empty := vrs.Providers(u.Key("not there"))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
16 17 18 19 20 21 22 23 24 25 26
	if len(empty) != 0 {
		t.Fatal("should be empty")
	}
}

func TestSetAndGet(t *testing.T) {
	pid := peer.ID([]byte("the peer id"))
	p := &peer.Peer{
		ID: pid,
	}
	k := u.Key("42")
27
	rs := VirtualRoutingServer()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	err := rs.Announce(p, k)
	if err != nil {
		t.Fatal(err)
	}
	providers := rs.Providers(k)
	if len(providers) != 1 {
		t.Fatal("should be one")
	}
	for _, elem := range providers {
		if bytes.Equal(elem.ID, pid) {
			return
		}
	}
	t.Fatal("ID should have matched")
}

func TestClientFindProviders(t *testing.T) {
Jeromy's avatar
Jeromy committed
45
	peer := &peer.Peer{ID: []byte("42")}
46
	rs := VirtualRoutingServer()
Jeromy's avatar
Jeromy committed
47 48
	client := rs.Client(peer)

Brian Tiger Chow's avatar
Brian Tiger Chow committed
49
	k := u.Key("hello")
50
	err := client.Provide(context.Background(), k)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	if err != nil {
		t.Fatal(err)
	}
	max := 100

	providersFromHashTable := rs.Providers(k)

	isInHT := false
	for _, p := range providersFromHashTable {
		if bytes.Equal(p.ID, peer.ID) {
			isInHT = true
		}
	}
	if !isInHT {
		t.Fatal("Despite client providing key, peer wasn't in hash table as a provider")
	}
	providersFromClient := client.FindProvidersAsync(context.Background(), u.Key("hello"), max)
	isInClient := false
	for p := range providersFromClient {
		if bytes.Equal(p.ID, peer.ID) {
			isInClient = true
		}
	}
	if !isInClient {
		t.Fatal("Despite client providing key, client didn't receive peer when finding providers")
	}
}

func TestClientOverMax(t *testing.T) {
80
	rs := VirtualRoutingServer()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	k := u.Key("hello")
	numProvidersForHelloKey := 100
	for i := 0; i < numProvidersForHelloKey; i++ {
		peer := &peer.Peer{
			ID: []byte(string(i)),
		}
		err := rs.Announce(peer, k)
		if err != nil {
			t.Fatal(err)
		}
	}
	providersFromHashTable := rs.Providers(k)
	if len(providersFromHashTable) != numProvidersForHelloKey {
		t.Log(1 == len(providersFromHashTable))
		t.Fatal("not all providers were returned")
	}

	max := 10
Jeromy's avatar
Jeromy committed
99 100 101
	peer := &peer.Peer{ID: []byte("TODO")}
	client := rs.Client(peer)

Brian Tiger Chow's avatar
Brian Tiger Chow committed
102 103 104 105 106 107 108 109 110 111 112 113
	providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
	i := 0
	for _ = range providersFromClient {
		i++
	}
	if i != max {
		t.Fatal("Too many providers returned")
	}
}

// TODO does dht ensure won't receive self as a provider? probably not.
func TestCanceledContext(t *testing.T) {
114
	rs := VirtualRoutingServer()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	k := u.Key("hello")

	t.Log("async'ly announce infinite stream of providers for key")
	i := 0
	go func() { // infinite stream
		for {
			peer := &peer.Peer{
				ID: []byte(string(i)),
			}
			err := rs.Announce(peer, k)
			if err != nil {
				t.Fatal(err)
			}
			i++
		}
	}()

132
	local := &peer.Peer{ID: []byte("peer id doesn't matter")}
Jeromy's avatar
Jeromy committed
133
	client := rs.Client(local)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

	t.Log("warning: max is finite so this test is non-deterministic")
	t.Log("context cancellation could simply take lower priority")
	t.Log("and result in receiving the max number of results")
	max := 1000

	t.Log("cancel the context before consuming")
	ctx, cancelFunc := context.WithCancel(context.Background())
	cancelFunc()
	providers := client.FindProvidersAsync(ctx, k, max)

	numProvidersReturned := 0
	for _ = range providers {
		numProvidersReturned++
	}
	t.Log(numProvidersReturned)

	if numProvidersReturned == max {
		t.Fatal("Context cancel had no effect")
	}
}