@cryptotaxi247 / kubo / commits / 57510d2fe

dht/dht_test: bootstrap synchronously. fares better.

Juan Batiz-Benet committed Dec 23, 2014 at 23:12 UTC 57510d2fec021bfcd3b8f806f8fe82efb0d3f0b5
2 files changed +37 -34
routing/dht/dht.go
+13 -19
@@ -365,26 +365,20 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) {
365 }
366
367 // Bootstrap builds up list of peers by requesting random peer IDs
368 -func (dht *IpfsDHT) Bootstrap(ctx context.Context) {
368 +func (dht *IpfsDHT) Bootstrap(ctx context.Context, queries int) {
369
370 - var wg sync.WaitGroup
370 + // bootstrap sequentially, as results will compound
371 for i := 0; i < NumBootstrapQueries; i++ {
372 - wg.Add(1)
373 - go func() {
374 - defer wg.Done()
375 -
376 - id := make([]byte, 16)
377 - rand.Read(id)
378 - pi, err := dht.FindPeer(ctx, peer.ID(id))
379 - if err == routing.ErrNotFound {
380 - // this isn't an error. this is precisely what we expect.
381 - } else if err != nil {
382 - log.Errorf("Bootstrap peer error: %s", err)
383 - } else {
384 - // woah, we got a peer under a random id? it _cannot_ be valid.
385 - log.Errorf("dht seemingly found a peer at a random bootstrap id (%s)...", pi)
386 - }
387 - }()
372 + id := make([]byte, 16)
373 + rand.Read(id)
374 + pi, err := dht.FindPeer(ctx, peer.ID(id))
375 + if err == routing.ErrNotFound {
376 + // this isn't an error. this is precisely what we expect.
377 + } else if err != nil {
378 + log.Errorf("Bootstrap peer error: %s", err)
379 + } else {
380 + // woah, we got a peer under a random id? it _cannot_ be valid.
381 + log.Errorf("dht seemingly found a peer at a random bootstrap id (%s)...", pi)
382 + }
383 }
389 - wg.Wait()
384 }
routing/dht/dht_test.go
+24 -15
@@ -93,24 +93,23 @@ func connect(t *testing.T, ctx context.Context, a, b *IpfsDHT) {
93
94 func bootstrap(t *testing.T, ctx context.Context, dhts []*IpfsDHT) {
95
96 - // try multiple rounds...
96 + ctx, cancel := context.WithCancel(ctx)
97 +
98 rounds := 1
99 for i := 0; i < rounds; i++ {
100 fmt.Printf("bootstrapping round %d/%d\n", i, rounds)
101
101 - var wg sync.WaitGroup
102 + // tried async. sequential fares much better. compare:
103 + // 100 async https://gist.github.com/jbenet/56d12f0578d5f34810b2
104 + // 100 sync https://gist.github.com/jbenet/6c59e7c15426e48aaedd
105 + // probably because results compound
106 for _, dht := range dhts {
103 - wg.Add(1)
104 - go func(i int) {
105 - defer wg.Done()
106 - <-time.After(time.Duration(i) * time.Millisecond) // stagger them to avoid overwhelming
107 - fmt.Printf("bootstrapping round %d/%d -- %s\n", i, rounds, dht.self)
108 - dht.Bootstrap(ctx)
109 - }(i)
107 + fmt.Printf("bootstrapping round %d/%d -- %s\n", i, rounds, dht.self)
108 + dht.Bootstrap(ctx, 3)
109 }
111 - wg.Wait()
112 -
110 }
111 +
112 + cancel()
113 }
114
115 func TestPing(t *testing.T) {
@@ -260,7 +259,7 @@ func TestProvides(t *testing.T) {
259 func TestBootstrap(t *testing.T) {
260 ctx := context.Background()
261
263 - nDHTs := 40
262 + nDHTs := 10
263 _, _, dhts := setupDHTS(ctx, nDHTs, t)
264 defer func() {
265 for i := 0; i < nDHTs; i++ {
@@ -275,7 +274,6 @@ func TestBootstrap(t *testing.T) {
274 }
275
276 <-time.After(100 * time.Millisecond)
278 -
277 t.Logf("bootstrapping them so they find each other", nDHTs)
278 ctxT, _ := context.WithTimeout(ctx, 5*time.Second)
279 bootstrap(t, ctxT, dhts)
@@ -308,8 +306,19 @@ func TestProvidesMany(t *testing.T) {
306 connect(t, ctx, dhts[i], dhts[(i+1)%len(dhts)])
307 }
308
309 + <-time.After(100 * time.Millisecond)
310 t.Logf("bootstrapping them so they find each other", nDHTs)
312 - bootstrap(t, ctx, dhts)
311 + ctxT, _ := context.WithTimeout(ctx, 5*time.Second)
312 + bootstrap(t, ctxT, dhts)
313 +
314 + <-time.After(5 * time.Second)
315 + // the routing tables should be full now. let's inspect them.
316 + t.Logf("checking routing table of %d", nDHTs)
317 + for _, dht := range dhts {
318 + fmt.Printf("checking routing table of %s\n", dht.self)
319 + dht.routingTable.Print()
320 + fmt.Println("")
321 + }
322
323 d := 0
324 for k, v := range testCaseValues {
@@ -341,7 +350,7 @@ func TestProvidesMany(t *testing.T) {
350
351 errchan := make(chan error)
352
344 - ctxT, _ := context.WithTimeout(ctx, 5*time.Second)
353 + ctxT, _ = context.WithTimeout(ctx, 5*time.Second)
354
355 var wg sync.WaitGroup
356 getProvider := func(dht *IpfsDHT, k u.Key) {