simul_test.go 1.46 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package swarm

import (
	"sync"
	"testing"
	"time"

	peer "github.com/jbenet/go-ipfs/p2p/peer"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)

func TestSimultOpen(t *testing.T) {
	// t.Skip("skipping for another test")
16
	t.Parallel()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
18

	ctx := context.Background()
19
	swarms := makeSwarms(ctx, t, 2)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
21
22
23
24
25

	// connect everyone
	{
		var wg sync.WaitGroup
		connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
			// copy for other peer
26
			log.Debugf("TestSimultOpen: connecting: %s --> %s (%s)", s.local, dst, addr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27
28
29
30
31
32
33
34
35
			s.peers.AddAddress(dst, addr)
			if _, err := s.Dial(ctx, dst); err != nil {
				t.Fatal("error swarm dialing to peer", err)
			}
			wg.Done()
		}

		log.Info("Connecting swarms simultaneously.")
		wg.Add(2)
36
37
		go connect(swarms[0], swarms[1].local, swarms[1].ListenAddresses()[0])
		go connect(swarms[1], swarms[0].local, swarms[0].ListenAddresses()[0])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
39
40
41
42
43
44
45
46
47
		wg.Wait()
	}

	for _, s := range swarms {
		s.Close()
	}
}

func TestSimultOpenMany(t *testing.T) {
	// t.Skip("very very slow")
48
	t.Parallel()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
50
51
52
53
54
55
56
57
58

	addrs := 20
	SubtestSwarm(t, addrs, 10)
}

func TestSimultOpenFewStress(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	// t.Skip("skipping for another test")
59
	t.Parallel()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
61
62
63
64
65
66
67
68
69
70

	msgs := 40
	swarms := 2
	rounds := 10
	// rounds := 100

	for i := 0; i < rounds; i++ {
		SubtestSwarm(t, swarms, msgs)
		<-time.After(10 * time.Millisecond)
	}
}