From 57510d2fec021bfcd3b8f806f8fe82efb0d3f0b5 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Tue, 23 Dec 2014 23:12:22 -0800 Subject: [PATCH] dht/dht_test: bootstrap synchronously. fares better. --- routing/dht/dht.go | 32 +++++++++++++------------------- routing/dht/dht_test.go | 39 ++++++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/routing/dht/dht.go b/routing/dht/dht.go index fdb5170f7..4cbf68e43 100644 --- a/routing/dht/dht.go +++ b/routing/dht/dht.go @@ -365,26 +365,20 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) { } // Bootstrap builds up list of peers by requesting random peer IDs -func (dht *IpfsDHT) Bootstrap(ctx context.Context) { +func (dht *IpfsDHT) Bootstrap(ctx context.Context, queries int) { - var wg sync.WaitGroup + // bootstrap sequentially, as results will compound for i := 0; i < NumBootstrapQueries; i++ { - wg.Add(1) - go func() { - defer wg.Done() - - id := make([]byte, 16) - rand.Read(id) - pi, err := dht.FindPeer(ctx, peer.ID(id)) - if err == routing.ErrNotFound { - // this isn't an error. this is precisely what we expect. - } else if err != nil { - log.Errorf("Bootstrap peer error: %s", err) - } else { - // woah, we got a peer under a random id? it _cannot_ be valid. - log.Errorf("dht seemingly found a peer at a random bootstrap id (%s)...", pi) - } - }() + id := make([]byte, 16) + rand.Read(id) + pi, err := dht.FindPeer(ctx, peer.ID(id)) + if err == routing.ErrNotFound { + // this isn't an error. this is precisely what we expect. + } else if err != nil { + log.Errorf("Bootstrap peer error: %s", err) + } else { + // woah, we got a peer under a random id? it _cannot_ be valid. + log.Errorf("dht seemingly found a peer at a random bootstrap id (%s)...", pi) + } } - wg.Wait() } diff --git a/routing/dht/dht_test.go b/routing/dht/dht_test.go index d3041f364..f37656c2e 100644 --- a/routing/dht/dht_test.go +++ b/routing/dht/dht_test.go @@ -93,24 +93,23 @@ func connect(t *testing.T, ctx context.Context, a, b *IpfsDHT) { func bootstrap(t *testing.T, ctx context.Context, dhts []*IpfsDHT) { - // try multiple rounds... + ctx, cancel := context.WithCancel(ctx) + rounds := 1 for i := 0; i < rounds; i++ { fmt.Printf("bootstrapping round %d/%d\n", i, rounds) - var wg sync.WaitGroup + // tried async. sequential fares much better. compare: + // 100 async https://gist.github.com/jbenet/56d12f0578d5f34810b2 + // 100 sync https://gist.github.com/jbenet/6c59e7c15426e48aaedd + // probably because results compound for _, dht := range dhts { - wg.Add(1) - go func(i int) { - defer wg.Done() - <-time.After(time.Duration(i) * time.Millisecond) // stagger them to avoid overwhelming - fmt.Printf("bootstrapping round %d/%d -- %s\n", i, rounds, dht.self) - dht.Bootstrap(ctx) - }(i) + fmt.Printf("bootstrapping round %d/%d -- %s\n", i, rounds, dht.self) + dht.Bootstrap(ctx, 3) } - wg.Wait() - } + + cancel() } func TestPing(t *testing.T) { @@ -260,7 +259,7 @@ func TestProvides(t *testing.T) { func TestBootstrap(t *testing.T) { ctx := context.Background() - nDHTs := 40 + nDHTs := 10 _, _, dhts := setupDHTS(ctx, nDHTs, t) defer func() { for i := 0; i < nDHTs; i++ { @@ -275,7 +274,6 @@ func TestBootstrap(t *testing.T) { } <-time.After(100 * time.Millisecond) - t.Logf("bootstrapping them so they find each other", nDHTs) ctxT, _ := context.WithTimeout(ctx, 5*time.Second) bootstrap(t, ctxT, dhts) @@ -308,8 +306,19 @@ func TestProvidesMany(t *testing.T) { connect(t, ctx, dhts[i], dhts[(i+1)%len(dhts)]) } + <-time.After(100 * time.Millisecond) t.Logf("bootstrapping them so they find each other", nDHTs) - bootstrap(t, ctx, dhts) + ctxT, _ := context.WithTimeout(ctx, 5*time.Second) + bootstrap(t, ctxT, dhts) + + <-time.After(5 * time.Second) + // the routing tables should be full now. let's inspect them. + t.Logf("checking routing table of %d", nDHTs) + for _, dht := range dhts { + fmt.Printf("checking routing table of %s\n", dht.self) + dht.routingTable.Print() + fmt.Println("") + } d := 0 for k, v := range testCaseValues { @@ -341,7 +350,7 @@ func TestProvidesMany(t *testing.T) { errchan := make(chan error) - ctxT, _ := context.WithTimeout(ctx, 5*time.Second) + ctxT, _ = context.WithTimeout(ctx, 5*time.Second) var wg sync.WaitGroup getProvider := func(dht *IpfsDHT, k u.Key) {