ext_test.go 6.68 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
	{
		typ := pb.Message_GET_VALUE
		str := "hello"
Jeromy's avatar
Jeromy committed
101 102 103 104 105 106 107

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

266 267
		})
	}
268

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