ext_test.go 6.52 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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41 42 43 44 45 46
	// 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)
	})

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

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

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

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

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

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

80 81 82 83 84 85
	// This one should fail with NotFound.
	// long context timeout to ensure we dont end too early.
	// the dht should be exhausting its query and returning not found.
	// (was 3 seconds before which should be _plenty_ of time, but maybe
	// travis machines really have a hard time...)
	ctx2, _ := context.WithTimeout(context.Background(), 20*time.Second)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86
	_, err = d.GetValue(ctx2, u.Key("test"))
87
	if err != nil {
88
		if err != routing.ErrNotFound {
89
			t.Fatalf("Expected ErrNotFound, got: %s", err)
90 91 92 93
		}
	} else {
		t.Fatal("expected error, got none.")
	}
94

95 96
	t.Log("ErrNotFound check passed!")

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

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

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

121 122 123
		if err := pbw.WriteMsg(&req); err != nil {
			t.Fatal(err)
		}
124

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

func TestNotFound(t *testing.T) {
139 140 141
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
142

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

152 153
	for _, p := range hosts {
		d.Update(ctx, p.ID())
Jeromy's avatar
Jeromy committed
154 155 156
	}

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

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

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

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

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

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

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

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

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

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

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

225
	for i := 1; i < 5; i++ {
226
		d.Update(ctx, hosts[i].ID())
227 228 229
	}

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

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

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

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

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

258 259
		})
	}
260

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