1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-03 21:08:17 +08:00

dht/dht_test: bootstrap synchronously. fares better.

This commit is contained in:
Juan Batiz-Benet
2014-12-23 23:12:22 -08:00
parent 618097d80e
commit 57510d2fec
2 changed files with 37 additions and 34 deletions

View File

@ -365,14 +365,10 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) {
} }
// Bootstrap builds up list of peers by requesting random peer IDs // 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++ { for i := 0; i < NumBootstrapQueries; i++ {
wg.Add(1)
go func() {
defer wg.Done()
id := make([]byte, 16) id := make([]byte, 16)
rand.Read(id) rand.Read(id)
pi, err := dht.FindPeer(ctx, peer.ID(id)) pi, err := dht.FindPeer(ctx, peer.ID(id))
@ -384,7 +380,5 @@ func (dht *IpfsDHT) Bootstrap(ctx context.Context) {
// woah, we got a peer under a random id? it _cannot_ be valid. // 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) log.Errorf("dht seemingly found a peer at a random bootstrap id (%s)...", pi)
} }
}()
} }
wg.Wait()
} }

View File

@ -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) { func bootstrap(t *testing.T, ctx context.Context, dhts []*IpfsDHT) {
// try multiple rounds... ctx, cancel := context.WithCancel(ctx)
rounds := 1 rounds := 1
for i := 0; i < rounds; i++ { for i := 0; i < rounds; i++ {
fmt.Printf("bootstrapping round %d/%d\n", i, rounds) 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 { 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) fmt.Printf("bootstrapping round %d/%d -- %s\n", i, rounds, dht.self)
dht.Bootstrap(ctx) dht.Bootstrap(ctx, 3)
}(i) }
} }
wg.Wait()
} cancel()
} }
func TestPing(t *testing.T) { func TestPing(t *testing.T) {
@ -260,7 +259,7 @@ func TestProvides(t *testing.T) {
func TestBootstrap(t *testing.T) { func TestBootstrap(t *testing.T) {
ctx := context.Background() ctx := context.Background()
nDHTs := 40 nDHTs := 10
_, _, dhts := setupDHTS(ctx, nDHTs, t) _, _, dhts := setupDHTS(ctx, nDHTs, t)
defer func() { defer func() {
for i := 0; i < nDHTs; i++ { for i := 0; i < nDHTs; i++ {
@ -275,7 +274,6 @@ func TestBootstrap(t *testing.T) {
} }
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.Logf("bootstrapping them so they find each other", nDHTs) t.Logf("bootstrapping them so they find each other", nDHTs)
ctxT, _ := context.WithTimeout(ctx, 5*time.Second) ctxT, _ := context.WithTimeout(ctx, 5*time.Second)
bootstrap(t, ctxT, dhts) bootstrap(t, ctxT, dhts)
@ -308,8 +306,19 @@ func TestProvidesMany(t *testing.T) {
connect(t, ctx, dhts[i], dhts[(i+1)%len(dhts)]) 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) 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 d := 0
for k, v := range testCaseValues { for k, v := range testCaseValues {
@ -341,7 +350,7 @@ func TestProvidesMany(t *testing.T) {
errchan := make(chan error) errchan := make(chan error)
ctxT, _ := context.WithTimeout(ctx, 5*time.Second) ctxT, _ = context.WithTimeout(ctx, 5*time.Second)
var wg sync.WaitGroup var wg sync.WaitGroup
getProvider := func(dht *IpfsDHT, k u.Key) { getProvider := func(dht *IpfsDHT, k u.Key) {