ext_test.go 6.06 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3
package dht

import (
4
	"math/rand"
Jeromy's avatar
Jeromy committed
5 6
	"testing"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	inet "github.com/jbenet/go-ipfs/net"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
	mocknet "github.com/jbenet/go-ipfs/net/mock"
Jeromy's avatar
Jeromy committed
9
	peer "github.com/jbenet/go-ipfs/peer"
10
	routing "github.com/jbenet/go-ipfs/routing"
11
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
12
	u "github.com/jbenet/go-ipfs/util"
Jeromy's avatar
Jeromy committed
13

14
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
	ggio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io"
16
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
17
	dssync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
18

Jeromy's avatar
Jeromy committed
19 20 21
	"time"
)

22
func TestGetFailures(t *testing.T) {
23 24 25
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26

27
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28
	mn, err := mocknet.FullMeshConnected(ctx, 2)
29 30 31
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33
	nets := mn.Nets()
	peers := mn.Peers()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34

35 36
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
	d := NewDHT(ctx, peers[0], nets[0], tsds)
37
	d.Update(ctx, peers[1])
38 39

	// This one should time out
40
	// u.POut("Timout Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	ctx1, _ := context.WithTimeout(context.Background(), time.Second)
42
	if _, err := d.GetValue(ctx1, u.Key("test")); err != nil {
43 44
		if err != context.DeadlineExceeded {
			t.Fatal("Got different error than we expected", err)
45 46 47
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
48 49
	}

50 51
	msgs := make(chan *pb.Message, 100)

52
	// u.POut("NotFound Test\n")
53
	// Reply with failures to every message
54 55 56 57 58 59
	nets[1].SetHandler(inet.ProtocolDHT, func(s inet.Stream) {
		defer s.Close()

		pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
		pbw := ggio.NewDelimitedWriter(s)

60
		pmes := new(pb.Message)
61 62
		if err := pbr.ReadMsg(pmes); err != nil {
			panic(err)
63 64
		}

65
		resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66
			Type: pmes.Type,
67
		}
68 69 70 71 72
		if err := pbw.WriteMsg(resp); err != nil {
			panic(err)
		}

		msgs <- resp
73 74 75
	})

	// This one should fail with NotFound
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76
	ctx2, _ := context.WithTimeout(context.Background(), 3*time.Second)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
	_, err = d.GetValue(ctx2, u.Key("test"))
78
	if err != nil {
79
		if err != routing.ErrNotFound {
80
			t.Fatalf("Expected ErrNotFound, got: %s", err)
81 82 83 84
		}
	} else {
		t.Fatal("expected error, got none.")
	}
85 86

	// Now we test this DHT's handleGetValue failure
87 88 89 90 91 92 93 94 95 96 97 98
	{
		typ := pb.Message_GET_VALUE
		str := "hello"
		rec, err := d.makePutRecord(u.Key(str), []byte("blah"))
		if err != nil {
			t.Fatal(err)
		}
		req := pb.Message{
			Type:   &typ,
			Key:    &str,
			Record: rec,
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99

100 101 102 103 104 105
		// u.POut("handleGetValue Test\n")
		s, err := nets[1].NewStream(inet.ProtocolDHT, peers[0])
		if err != nil {
			t.Fatal(err)
		}
		defer s.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106

107 108
		pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
		pbw := ggio.NewDelimitedWriter(s)
109

110 111 112
		if err := pbw.WriteMsg(&req); err != nil {
			t.Fatal(err)
		}
113

114 115 116 117 118 119 120 121 122 123 124
		pmes := new(pb.Message)
		if err := pbr.ReadMsg(pmes); err != nil {
			t.Fatal(err)
		}
		if pmes.GetRecord() != nil {
			t.Fatal("shouldnt have value")
		}
		if pmes.GetProviderPeers() != nil {
			t.Fatal("shouldnt have provider peers")
		}
	}
Jeromy's avatar
Jeromy committed
125
}
Jeromy's avatar
Jeromy committed
126 127

func TestNotFound(t *testing.T) {
128 129 130
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
131

132
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133
	mn, err := mocknet.FullMeshConnected(ctx, 16)
134 135 136
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137 138
	nets := mn.Nets()
	peers := mn.Peers()
139 140
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
	d := NewDHT(ctx, peers[0], nets[0], tsds)
Jeromy's avatar
Jeromy committed
141

142 143
	for _, p := range peers {
		d.Update(ctx, p)
Jeromy's avatar
Jeromy committed
144 145 146
	}

	// Reply with random peers to every message
147
	for _, neti := range nets {
148
		neti := neti // shadow loop var
149 150
		neti.SetHandler(inet.ProtocolDHT, func(s inet.Stream) {
			defer s.Close()
Jeromy's avatar
Jeromy committed
151

152 153
			pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
			pbw := ggio.NewDelimitedWriter(s)
Jeromy's avatar
Jeromy committed
154

155 156 157
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
Jeromy's avatar
Jeromy committed
158 159
			}

160 161 162 163
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
				resp := &pb.Message{Type: pmes.Type}

164
				ps := []peer.PeerInfo{}
165
				for i := 0; i < 7; i++ {
166 167 168
					p := peers[rand.Intn(len(peers))]
					pi := neti.Peerstore().PeerInfo(p)
					ps = append(ps, pi)
169 170
				}

171
				resp.CloserPeers = pb.PeerInfosToPBPeers(d.network, ps)
172 173 174 175 176 177 178 179 180
				if err := pbw.WriteMsg(resp); err != nil {
					panic(err)
				}

			default:
				panic("Shouldnt recieve this.")
			}
		})
	}
Jeromy's avatar
Jeromy committed
181

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
182
	ctx, _ = context.WithTimeout(ctx, time.Second*5)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
183
	v, err := d.GetValue(ctx, u.Key("hello"))
184
	log.Debugf("get value got %v", v)
Jeromy's avatar
Jeromy committed
185 186
	if err != nil {
		switch err {
187
		case routing.ErrNotFound:
Jeromy's avatar
Jeromy committed
188 189 190 191 192 193 194 195 196 197
			//Success!
			return
		case u.ErrTimeout:
			t.Fatal("Should not have gotten timeout!")
		default:
			t.Fatalf("Got unexpected error: %s", err)
		}
	}
	t.Fatal("Expected to recieve an error.")
}
198 199 200 201

// If less than K nodes are in the entire network, it should fail when we make
// a GET rpc and nobody has the value
func TestLessThanKResponses(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202 203
	// t.Skip("skipping test because it makes a lot of output")

204
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
205
	mn, err := mocknet.FullMeshConnected(ctx, 6)
206 207 208
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
209 210
	nets := mn.Nets()
	peers := mn.Peers()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
211

212 213
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
	d := NewDHT(ctx, peers[0], nets[0], tsds)
214

215 216
	for i := 1; i < 5; i++ {
		d.Update(ctx, peers[i])
217 218 219
	}

	// Reply with random peers to every message
220
	for _, neti := range nets {
221
		neti := neti // shadow loop var
222 223 224 225 226
		neti.SetHandler(inet.ProtocolDHT, func(s inet.Stream) {
			defer s.Close()

			pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
			pbw := ggio.NewDelimitedWriter(s)
227

228 229 230
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
231 232
			}

233 234
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
235
				pi := neti.Peerstore().PeerInfo(peers[1])
236 237
				resp := &pb.Message{
					Type:        pmes.Type,
238
					CloserPeers: pb.PeerInfosToPBPeers(d.network, []peer.PeerInfo{pi}),
239 240 241 242 243 244 245
				}

				if err := pbw.WriteMsg(resp); err != nil {
					panic(err)
				}
			default:
				panic("Shouldnt recieve this.")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
246
			}
247

248 249
		})
	}
250

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
251
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
252
	if _, err := d.GetValue(ctx, u.Key("hello")); err != nil {
253
		switch err {
254
		case routing.ErrNotFound:
255 256 257 258 259 260 261 262 263 264
			//Success!
			return
		case u.ErrTimeout:
			t.Fatal("Should not have gotten timeout!")
		default:
			t.Fatalf("Got unexpected error: %s", err)
		}
	}
	t.Fatal("Expected to recieve an error.")
}