ext_test.go 6.97 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 54 55
		if merr, ok := err.(u.MultiErr); ok && len(merr) > 0 {
			err = merr[0]
		}

56 57
		if err != context.DeadlineExceeded {
			t.Fatal("Got different error than we expected", err)
58 59 60
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
61 62
	}

63
	t.Log("Timeout test passed.")
64

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

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

72
		pmes := new(pb.Message)
73 74
		if err := pbr.ReadMsg(pmes); err != nil {
			panic(err)
75 76
		}

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

85 86 87 88 89 90
	// 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
91
	_, err = d.GetValue(ctx2, u.Key("test"))
92
	if err != nil {
93 94 95
		if merr, ok := err.(u.MultiErr); ok && len(merr) > 0 {
			err = merr[0]
		}
96
		if err != routing.ErrNotFound {
97
			t.Fatalf("Expected ErrNotFound, got: %s", err)
98 99 100 101
		}
	} else {
		t.Fatal("expected error, got none.")
	}
102

103 104
	t.Log("ErrNotFound check passed!")

105
	// Now we test this DHT's handleGetValue failure
106 107 108
	{
		typ := pb.Message_GET_VALUE
		str := "hello"
Jeromy's avatar
Jeromy committed
109 110 111 112 113 114

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

Jeromy's avatar
Jeromy committed
115
		rec, err := record.MakePutRecord(sk, u.Key(str), []byte("blah"))
116 117 118 119 120 121 122 123
		if err != nil {
			t.Fatal(err)
		}
		req := pb.Message{
			Type:   &typ,
			Key:    &str,
			Record: rec,
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124

125
		// u.POut("handleGetValue Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126
		s, err := hosts[1].NewStream(ProtocolDHT, hosts[0].ID())
127 128 129 130
		if err != nil {
			t.Fatal(err)
		}
		defer s.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
131

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

135 136 137
		if err := pbw.WriteMsg(&req); err != nil {
			t.Fatal(err)
		}
138

139 140 141 142 143 144 145 146 147 148 149
		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
150
}
Jeromy's avatar
Jeromy committed
151 152

func TestNotFound(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
153
	// t.Skip("skipping test to debug another")
154 155 156
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
157

158
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
159
	mn, err := mocknet.FullMeshConnected(ctx, 16)
160 161 162
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
163
	hosts := mn.Hosts()
164
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165
	d := NewDHT(ctx, hosts[0], tsds)
Jeromy's avatar
Jeromy committed
166

167 168
	for _, p := range hosts {
		d.Update(ctx, p.ID())
Jeromy's avatar
Jeromy committed
169 170 171
	}

	// Reply with random peers to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172 173 174
	for _, host := range hosts {
		host := host // shadow loop var
		host.SetStreamHandler(ProtocolDHT, func(s inet.Stream) {
175
			defer s.Close()
Jeromy's avatar
Jeromy committed
176

177 178
			pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
			pbw := ggio.NewDelimitedWriter(s)
Jeromy's avatar
Jeromy committed
179

180 181 182
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
Jeromy's avatar
Jeromy committed
183 184
			}

185 186 187 188
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
				resp := &pb.Message{Type: pmes.Type}

189
				ps := []peer.PeerInfo{}
190
				for i := 0; i < 7; i++ {
191
					p := hosts[rand.Intn(len(hosts))].ID()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
192
					pi := host.Peerstore().PeerInfo(p)
193
					ps = append(ps, pi)
194 195
				}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
196
				resp.CloserPeers = pb.PeerInfosToPBPeers(d.host.Network(), ps)
197 198 199 200 201 202 203 204 205
				if err := pbw.WriteMsg(resp); err != nil {
					panic(err)
				}

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

207 208
	// 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
209
	v, err := d.GetValue(ctx, u.Key("hello"))
210
	log.Debugf("get value got %v", v)
Jeromy's avatar
Jeromy committed
211
	if err != nil {
212 213 214
		if merr, ok := err.(u.MultiErr); ok && len(merr) > 0 {
			err = merr[0]
		}
Jeromy's avatar
Jeromy committed
215
		switch err {
216
		case routing.ErrNotFound:
Jeromy's avatar
Jeromy committed
217 218 219 220 221 222 223 224 225 226
			//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.")
}
227 228 229 230

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

234
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
235
	mn, err := mocknet.FullMeshConnected(ctx, 6)
236 237 238
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
239
	hosts := mn.Hosts()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
240

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

244
	for i := 1; i < 5; i++ {
245
		d.Update(ctx, hosts[i].ID())
246 247 248
	}

	// Reply with random peers to every message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
249 250 251
	for _, host := range hosts {
		host := host // shadow loop var
		host.SetStreamHandler(ProtocolDHT, func(s inet.Stream) {
252 253 254 255
			defer s.Close()

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

257 258 259
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
260 261
			}

262 263
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
264
				pi := host.Peerstore().PeerInfo(hosts[1].ID())
265 266
				resp := &pb.Message{
					Type:        pmes.Type,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
267
					CloserPeers: pb.PeerInfosToPBPeers(d.host.Network(), []peer.PeerInfo{pi}),
268 269 270 271 272 273 274
				}

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

277 278
		})
	}
279

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
280
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
281
	if _, err := d.GetValue(ctx, u.Key("hello")); err != nil {
282
		switch err {
283
		case routing.ErrNotFound:
284 285 286 287 288 289 290 291 292 293
			//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.")
}