mockrouting_test.go 3.13 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1
package mockrouting
2 3 4 5 6 7 8 9

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"
10
	testutil "github.com/jbenet/go-ipfs/util/testutil"
11 12 13 14
)

func TestKeyNotFound(t *testing.T) {

Brian Tiger Chow's avatar
Brian Tiger Chow committed
15 16 17
	var peer = testutil.NewPeerWithID(peer.ID([]byte("the peer id")))
	var key = u.Key("mock key")
	var ctx = context.Background()
18

Brian Tiger Chow's avatar
Brian Tiger Chow committed
19 20 21 22 23
	rs := NewServer()
	providers := rs.Client(peer).FindProvidersAsync(ctx, key, 10)
	_, ok := <-providers
	if ok {
		t.Fatal("should be closed")
24 25 26 27
	}
}

func TestClientFindProviders(t *testing.T) {
28
	peer := testutil.NewPeerWithIDString("42")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
29
	rs := NewServer()
30 31 32 33 34 35 36 37 38
	client := rs.Client(peer)

	k := u.Key("hello")
	err := client.Provide(context.Background(), k)
	if err != nil {
		t.Fatal(err)
	}
	max := 100

Brian Tiger Chow's avatar
Brian Tiger Chow committed
39 40 41 42
	providersFromHashTable, err := rs.Client(peer).FindProviders(context.Background(), k)
	if err != nil {
		t.Fatal(err)
	}
43 44 45

	isInHT := false
	for _, p := range providersFromHashTable {
46
		if bytes.Equal(p.ID(), peer.ID()) {
47 48 49 50 51 52 53 54 55
			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 {
56
		if bytes.Equal(p.ID(), peer.ID()) {
57 58 59 60 61 62 63 64 65
			isInClient = true
		}
	}
	if !isInClient {
		t.Fatal("Despite client providing key, client didn't receive peer when finding providers")
	}
}

func TestClientOverMax(t *testing.T) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
66
	rs := NewServer()
67 68 69
	k := u.Key("hello")
	numProvidersForHelloKey := 100
	for i := 0; i < numProvidersForHelloKey; i++ {
70
		peer := testutil.NewPeerWithIDString(string(i))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
71
		err := rs.Client(peer).Provide(context.Background(), k)
72 73 74 75 76 77
		if err != nil {
			t.Fatal(err)
		}
	}

	max := 10
78
	peer := testutil.NewPeerWithIDString("TODO")
79 80 81 82 83 84 85 86 87 88 89 90 91 92
	client := rs.Client(peer)

	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) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
93
	rs := NewServer()
94 95 96 97 98 99
	k := u.Key("hello")

	t.Log("async'ly announce infinite stream of providers for key")
	i := 0
	go func() { // infinite stream
		for {
100
			peer := testutil.NewPeerWithIDString(string(i))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
101
			err := rs.Client(peer).Provide(context.Background(), k)
102 103 104 105 106 107 108
			if err != nil {
				t.Fatal(err)
			}
			i++
		}
	}()

109
	local := testutil.NewPeerWithIDString("peer id doesn't matter")
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	client := rs.Client(local)

	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")
	}
}