ext_test.go 6.74 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"
Jeromy's avatar
Jeromy committed
14
	record "github.com/jbenet/go-ipfs/routing/record"
15
	u "github.com/jbenet/go-ipfs/util"
Jeromy's avatar
Jeromy committed
16

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

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

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

30
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
	mn, err := mocknet.FullMeshConnected(ctx, 2)
32 33 34
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
	hosts := mn.Hosts()
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, hosts[1].ID())
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")
50
	ctx1, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
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 82 83 84 85 86
	// 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
87
	_, err = d.GetValue(ctx2, u.Key("test"))
88
	if err != nil {
89
		if err != routing.ErrNotFound {
90
			t.Fatalf("Expected ErrNotFound, got: %s", err)
91 92 93 94
		}
	} else {
		t.Fatal("expected error, got none.")
	}
95

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

98
	// Now we test this DHT's handleGetValue failure
99 100 101
	{
		typ := pb.Message_GET_VALUE
		str := "hello"
Jeromy's avatar
Jeromy committed
102 103 104 105 106 107

		sk, err := d.getOwnPrivateKey()
		if err != nil {
			t.Fatal(err)
		}

Jeromy's avatar
Jeromy committed
108
		rec, err := record.MakePutRecord(sk, u.Key(str), []byte("blah"))
109 110 111 112 113 114 115 116
		if err != nil {
			t.Fatal(err)
		}
		req := pb.Message{
			Type:   &typ,
			Key:    &str,
			Record: rec,
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117

118
		// u.POut("handleGetValue Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119
		s, err := hosts[1].NewStream(ProtocolDHT, hosts[0].ID())
120 121 122 123
		if err != nil {
			t.Fatal(err)
		}
		defer s.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124

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

128 129 130
		if err := pbw.WriteMsg(&req); err != nil {
			t.Fatal(err)
		}
131

132 133 134 135 136 137 138 139 140 141 142
		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
143
}
Jeromy's avatar
Jeromy committed
144 145

func TestNotFound(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146
	// t.Skip("skipping test to debug another")
147 148 149
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
150

151
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
152
	mn, err := mocknet.FullMeshConnected(ctx, 16)
153 154 155
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
156
	hosts := mn.Hosts()
157
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
158
	d := NewDHT(ctx, hosts[0], tsds)
Jeromy's avatar
Jeromy committed
159

160 161
	for _, p := range hosts {
		d.Update(ctx, p.ID())
Jeromy's avatar
Jeromy committed
162 163 164
	}

	// Reply with random peers to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165 166 167
	for _, host := range hosts {
		host := host // shadow loop var
		host.SetStreamHandler(ProtocolDHT, func(s inet.Stream) {
168
			defer s.Close()
Jeromy's avatar
Jeromy committed
169

170 171
			pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
			pbw := ggio.NewDelimitedWriter(s)
Jeromy's avatar
Jeromy committed
172

173 174 175
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
Jeromy's avatar
Jeromy committed
176 177
			}

178 179 180 181
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
				resp := &pb.Message{Type: pmes.Type}

182
				ps := []peer.PeerInfo{}
183
				for i := 0; i < 7; i++ {
184
					p := hosts[rand.Intn(len(hosts))].ID()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
185
					pi := host.Peerstore().PeerInfo(p)
186
					ps = append(ps, pi)
187 188
				}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
189
				resp.CloserPeers = pb.PeerInfosToPBPeers(d.host.Network(), ps)
190 191 192 193 194 195 196 197 198
				if err := pbw.WriteMsg(resp); err != nil {
					panic(err)
				}

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

200 201
	// 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
202
	v, err := d.GetValue(ctx, u.Key("hello"))
203
	log.Debugf("get value got %v", v)
Jeromy's avatar
Jeromy committed
204 205
	if err != nil {
		switch err {
206
		case routing.ErrNotFound:
Jeromy's avatar
Jeromy committed
207 208 209 210 211 212 213 214 215 216
			//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.")
}
217 218 219 220

// 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
221
	// t.Skip("skipping test to debug another")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
222 223
	// t.Skip("skipping test because it makes a lot of output")

224
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
225
	mn, err := mocknet.FullMeshConnected(ctx, 6)
226 227 228
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229
	hosts := mn.Hosts()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
230

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

234
	for i := 1; i < 5; i++ {
235
		d.Update(ctx, hosts[i].ID())
236 237 238
	}

	// Reply with random peers to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
239 240 241
	for _, host := range hosts {
		host := host // shadow loop var
		host.SetStreamHandler(ProtocolDHT, func(s inet.Stream) {
242 243 244 245
			defer s.Close()

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

247 248 249
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
250 251
			}

252 253
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
254
				pi := host.Peerstore().PeerInfo(hosts[1].ID())
255 256
				resp := &pb.Message{
					Type:        pmes.Type,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
257
					CloserPeers: pb.PeerInfosToPBPeers(d.host.Network(), []peer.PeerInfo{pi}),
258 259 260 261 262 263 264
				}

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

267 268
		})
	}
269

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
270
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
271
	if _, err := d.GetValue(ctx, u.Key("hello")); err != nil {
272
		switch err {
273
		case routing.ErrNotFound:
274 275 276 277 278 279 280 281 282 283
			//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.")
}