mockrouting_test.go 3.83 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1
package mockrouting
2 3 4 5

import (
	"bytes"
	"testing"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
6
	"time"
7 8

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
9
	peer "github.com/jbenet/go-ipfs/peer"
10
	u "github.com/jbenet/go-ipfs/util"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
11
	delay "github.com/jbenet/go-ipfs/util/delay"
12
	testutil "github.com/jbenet/go-ipfs/util/testutil"
13 14 15 16
)

func TestKeyNotFound(t *testing.T) {

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

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

func TestClientFindProviders(t *testing.T) {
30
	peer := testutil.NewPeerWithIDString("42")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
31
	rs := NewServer()
32 33 34 35 36 37 38 39 40
	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
41 42 43 44
	providersFromHashTable, err := rs.Client(peer).FindProviders(context.Background(), k)
	if err != nil {
		t.Fatal(err)
	}
45 46 47

	isInHT := false
	for _, p := range providersFromHashTable {
48
		if bytes.Equal(p.ID(), peer.ID()) {
49 50 51 52 53 54 55 56 57
			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 {
58
		if bytes.Equal(p.ID(), peer.ID()) {
59 60 61 62 63 64 65 66 67
			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
68
	rs := NewServer()
69 70 71
	k := u.Key("hello")
	numProvidersForHelloKey := 100
	for i := 0; i < numProvidersForHelloKey; i++ {
72
		peer := testutil.NewPeerWithIDString(string(i))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
73
		err := rs.Client(peer).Provide(context.Background(), k)
74 75 76 77 78 79
		if err != nil {
			t.Fatal(err)
		}
	}

	max := 10
80
	peer := testutil.NewPeerWithIDString("TODO")
81 82 83 84 85 86 87 88 89 90 91 92 93 94
	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
95
	rs := NewServer()
96 97 98 99 100 101
	k := u.Key("hello")

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

111
	local := testutil.NewPeerWithIDString("peer id doesn't matter")
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	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")
	}
}
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 155 156 157 158 159 160 161 162 163 164 165 166

func TestValidAfter(t *testing.T) {

	var p = testutil.NewPeerWithID(peer.ID([]byte("the peer id")))
	var key = u.Key("mock key")
	var ctx = context.Background()
	conf := DelayConfig{
		ValueVisibility: delay.Fixed(1 * time.Hour),
		Query:           delay.Fixed(0),
	}

	rs := NewServerWithDelay(conf)

	rs.Client(p).Provide(ctx, key)

	var providers []peer.Peer
	providers, err := rs.Client(p).FindProviders(ctx, key)
	if err != nil {
		t.Fatal(err)
	}
	if len(providers) > 0 {
		t.Fail()
	}

	conf.ValueVisibility.Set(0)
	providers, err = rs.Client(p).FindProviders(ctx, key)
	if err != nil {
		t.Fatal(err)
	}
	if len(providers) != 1 {
		t.Fail()
	}
}