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

import (
4
	"math/rand"
Jeromy's avatar
Jeromy committed
5 6
	"testing"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8
	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
9
	peer "github.com/jbenet/go-ipfs/p2p/peer"
10
	routing "github.com/jbenet/go-ipfs/routing"
11
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
12
	u "github.com/jbenet/go-ipfs/util"
Jeromy's avatar
Jeromy committed
13

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

Jeromy's avatar
Jeromy committed
19 20 21
	"time"
)

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

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

35 36
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
	d := NewDHT(ctx, peers[0], nets[0], tsds)
37
	d.Update(ctx, peers[1])
38 39

	// This one should time out
40
	// u.POut("Timout Test\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	ctx1, _ := context.WithTimeout(context.Background(), time.Second)
42
	if _, err := d.GetValue(ctx1, u.Key("test")); err != nil {
43 44
		if err != context.DeadlineExceeded {
			t.Fatal("Got different error than we expected", err)
45 46 47
		}
	} else {
		t.Fatal("Did not get expected error!")
Jeromy's avatar
Jeromy committed
48 49
	}

50
	t.Log("Timeout test passed.")
51

52
	// Reply with failures to every message
53 54 55 56 57 58
	nets[1].SetHandler(inet.ProtocolDHT, func(s inet.Stream) {
		defer s.Close()

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

59
		pmes := new(pb.Message)
60 61
		if err := pbr.ReadMsg(pmes); err != nil {
			panic(err)
62 63
		}

64
		resp := &pb.Message{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
			Type: pmes.Type,
66
		}
67 68 69
		if err := pbw.WriteMsg(resp); err != nil {
			panic(err)
		}
70 71 72
	})

	// This one should fail with NotFound
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
	ctx2, _ := context.WithTimeout(context.Background(), 3*time.Second)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
	_, err = d.GetValue(ctx2, u.Key("test"))
75
	if err != nil {
76
		if err != routing.ErrNotFound {
77
			t.Fatalf("Expected ErrNotFound, got: %s", err)
78 79 80 81
		}
	} else {
		t.Fatal("expected error, got none.")
	}
82

83 84
	t.Log("ErrNotFound check passed!")

85
	// Now we test this DHT's handleGetValue failure
86 87 88 89 90 91 92 93 94 95 96 97
	{
		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
98

99 100 101 102 103 104
		// u.POut("handleGetValue Test\n")
		s, err := nets[1].NewStream(inet.ProtocolDHT, peers[0])
		if err != nil {
			t.Fatal(err)
		}
		defer s.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105

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

109 110 111
		if err := pbw.WriteMsg(&req); err != nil {
			t.Fatal(err)
		}
112

113 114 115 116 117 118 119 120 121 122 123
		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
124
}
Jeromy's avatar
Jeromy committed
125 126

func TestNotFound(t *testing.T) {
127 128 129
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130

131
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132
	mn, err := mocknet.FullMeshConnected(ctx, 16)
133 134 135
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
136 137
	nets := mn.Nets()
	peers := mn.Peers()
138 139
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
	d := NewDHT(ctx, peers[0], nets[0], tsds)
Jeromy's avatar
Jeromy committed
140

141 142
	for _, p := range peers {
		d.Update(ctx, p)
Jeromy's avatar
Jeromy committed
143 144 145
	}

	// Reply with random peers to every message
146
	for _, neti := range nets {
147
		neti := neti // shadow loop var
148 149
		neti.SetHandler(inet.ProtocolDHT, func(s inet.Stream) {
			defer s.Close()
Jeromy's avatar
Jeromy committed
150

151 152
			pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
			pbw := ggio.NewDelimitedWriter(s)
Jeromy's avatar
Jeromy committed
153

154 155 156
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
Jeromy's avatar
Jeromy committed
157 158
			}

159 160 161 162
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
				resp := &pb.Message{Type: pmes.Type}

163
				ps := []peer.PeerInfo{}
164
				for i := 0; i < 7; i++ {
165 166 167
					p := peers[rand.Intn(len(peers))]
					pi := neti.Peerstore().PeerInfo(p)
					ps = append(ps, pi)
168 169
				}

170
				resp.CloserPeers = pb.PeerInfosToPBPeers(d.network, ps)
171 172 173 174 175 176 177 178 179
				if err := pbw.WriteMsg(resp); err != nil {
					panic(err)
				}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
181
	ctx, _ = context.WithTimeout(ctx, time.Second*5)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
182
	v, err := d.GetValue(ctx, u.Key("hello"))
183
	log.Debugf("get value got %v", v)
Jeromy's avatar
Jeromy committed
184 185
	if err != nil {
		switch err {
186
		case routing.ErrNotFound:
Jeromy's avatar
Jeromy committed
187 188 189 190 191 192 193 194 195 196
			//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.")
}
197 198 199 200

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

203
	ctx := context.Background()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
204
	mn, err := mocknet.FullMeshConnected(ctx, 6)
205 206 207
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
208 209
	nets := mn.Nets()
	peers := mn.Peers()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
210

211 212
	tsds := dssync.MutexWrap(ds.NewMapDatastore())
	d := NewDHT(ctx, peers[0], nets[0], tsds)
213

214 215
	for i := 1; i < 5; i++ {
		d.Update(ctx, peers[i])
216 217 218
	}

	// Reply with random peers to every message
219
	for _, neti := range nets {
220
		neti := neti // shadow loop var
221 222 223 224 225
		neti.SetHandler(inet.ProtocolDHT, func(s inet.Stream) {
			defer s.Close()

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

227 228 229
			pmes := new(pb.Message)
			if err := pbr.ReadMsg(pmes); err != nil {
				panic(err)
230 231
			}

232 233
			switch pmes.GetType() {
			case pb.Message_GET_VALUE:
234
				pi := neti.Peerstore().PeerInfo(peers[1])
235 236
				resp := &pb.Message{
					Type:        pmes.Type,
237
					CloserPeers: pb.PeerInfosToPBPeers(d.network, []peer.PeerInfo{pi}),
238 239 240 241 242 243 244
				}

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

247 248
		})
	}
249

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
250
	ctx, _ = context.WithTimeout(ctx, time.Second*30)
251
	if _, err := d.GetValue(ctx, u.Key("hello")); err != nil {
252
		switch err {
253
		case routing.ErrNotFound:
254 255 256 257 258 259 260 261 262 263
			//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.")
}