mockrouting_test.go 3.95 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
	client := rs.Client(peer)

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

	// This is bad... but simulating networks is hard
	time.Sleep(time.Millisecond * 300)
42 43
	max := 100

Brian Tiger Chow's avatar
Brian Tiger Chow committed
44 45 46 47
	providersFromHashTable, err := rs.Client(peer).FindProviders(context.Background(), k)
	if err != nil {
		t.Fatal(err)
	}
48 49 50

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

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

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

114
	local := testutil.NewPeerWithIDString("peer id doesn't matter")
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	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
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

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)
	}
166
	t.Log("providers", providers)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
167 168 169 170
	if len(providers) != 1 {
		t.Fail()
	}
}