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

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4 5
	"io"
	"io/ioutil"
6
	"math/rand"
Jeromy's avatar
Jeromy committed
7 8
	"testing"

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

16
	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
17
	ggio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io"
18
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
19
	dssync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
20

Jeromy's avatar
Jeromy committed
21 22 23
	"time"
)

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

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

37
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
	d := NewDHT(ctx, hosts[0], tsds)
39
	d.Update(ctx, peers[1])
40

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42 43 44 45 46 47
	// u.POut("NotFound Test\n")
	// Reply with failures to every message
	hosts[1].SetStreamHandler(ProtocolDHT, func(s inet.Stream) {
		defer s.Close()
		io.Copy(ioutil.Discard, s)
	})

48
	// This one should time out
49
	// u.POut("Timout Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50
	ctx1, _ := context.WithTimeout(context.Background(), time.Second)
51
	if _, err := d.GetValue(ctx1, u.Key("test")); err != nil {
52 53
		if err != context.DeadlineExceeded {
			t.Fatal("Got different error than we expected", err)
54 55 56
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
57 58
	}

59
	t.Log("Timeout test passed.")
60

61
	// Reply with failures to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62
	hosts[1].SetStreamHandler(ProtocolDHT, func(s inet.Stream) {
63 64 65 66 67
		defer s.Close()

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

68
		pmes := new(pb.Message)
69 70
		if err := pbr.ReadMsg(pmes); err != nil {
			panic(err)
71 72
		}

73
		resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
			Type: pmes.Type,
75
		}
76 77 78
		if err := pbw.WriteMsg(resp); err != nil {
			panic(err)
		}
79 80 81
	})

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

92 93
	t.Log("ErrNotFound check passed!")

94
	// Now we test this DHT's handleGetValue failure
95 96 97 98 99 100 101 102 103 104 105 106
	{
		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
107

108
		// u.POut("handleGetValue Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
109
		s, err := hosts[1].NewStream(ProtocolDHT, hosts[0].ID())
110 111 112 113
		if err != nil {
			t.Fatal(err)
		}
		defer s.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114

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

118 119 120
		if err := pbw.WriteMsg(&req); err != nil {
			t.Fatal(err)
		}
121

122 123 124 125 126 127 128 129 130 131 132
		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
133
}
Jeromy's avatar
Jeromy committed
134 135

func TestNotFound(t *testing.T) {
136 137 138
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139

140
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
141
	mn, err := mocknet.FullMeshConnected(ctx, 16)
142 143 144
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
145
	hosts := mn.Hosts()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146
	peers := mn.Peers()
147
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
148
	d := NewDHT(ctx, hosts[0], tsds)
Jeromy's avatar
Jeromy committed
149

150 151
	for _, p := range peers {
		d.Update(ctx, p)
Jeromy's avatar
Jeromy committed
152 153 154
	}

	// Reply with random peers to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
155 156 157
	for _, host := range hosts {
		host := host // shadow loop var
		host.SetStreamHandler(ProtocolDHT, func(s inet.Stream) {
158
			defer s.Close()
Jeromy's avatar
Jeromy committed
159

160 161
			pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
			pbw := ggio.NewDelimitedWriter(s)
Jeromy's avatar
Jeromy committed
162

163 164 165
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
Jeromy's avatar
Jeromy committed
166 167
			}

168 169 170 171
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
				resp := &pb.Message{Type: pmes.Type}

172
				ps := []peer.PeerInfo{}
173
				for i := 0; i < 7; i++ {
174
					p := peers[rand.Intn(len(peers))]
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
175
					pi := host.Peerstore().PeerInfo(p)
176
					ps = append(ps, pi)
177 178
				}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
179
				resp.CloserPeers = pb.PeerInfosToPBPeers(d.host.Network(), ps)
180 181 182 183 184 185 186 187 188
				if err := pbw.WriteMsg(resp); err != nil {
					panic(err)
				}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
190
	ctx, _ = context.WithTimeout(ctx, time.Second*5)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
191
	v, err := d.GetValue(ctx, u.Key("hello"))
192
	log.Debugf("get value got %v", v)
Jeromy's avatar
Jeromy committed
193 194
	if err != nil {
		switch err {
195
		case routing.ErrNotFound:
Jeromy's avatar
Jeromy committed
196 197 198 199 200 201 202 203 204 205
			//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.")
}
206 207 208 209

// 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
210 211
	// t.Skip("skipping test because it makes a lot of output")

212
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
213
	mn, err := mocknet.FullMeshConnected(ctx, 6)
214 215 216
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
217
	hosts := mn.Hosts()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
218
	peers := mn.Peers()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
219

220
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
221
	d := NewDHT(ctx, hosts[0], tsds)
222

223 224
	for i := 1; i < 5; i++ {
		d.Update(ctx, peers[i])
225 226 227
	}

	// Reply with random peers to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
228 229 230
	for _, host := range hosts {
		host := host // shadow loop var
		host.SetStreamHandler(ProtocolDHT, func(s inet.Stream) {
231 232 233 234
			defer s.Close()

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

236 237 238
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
239 240
			}

241 242
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
243
				pi := host.Peerstore().PeerInfo(peers[1])
244 245
				resp := &pb.Message{
					Type:        pmes.Type,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
246
					CloserPeers: pb.PeerInfosToPBPeers(d.host.Network(), []peer.PeerInfo{pi}),
247 248 249 250 251 252 253
				}

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

256 257
		})
	}
258

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
259
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
260
	if _, err := d.GetValue(ctx, u.Key("hello")); err != nil {
261
		switch err {
262
		case routing.ErrNotFound:
263 264 265 266 267 268 269 270 271 272
			//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.")
}