simul_test.go 1.56 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
package swarm

import (
Jeromy's avatar
Jeromy committed
4
	"runtime"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
6
7
8
	"sync"
	"testing"
	"time"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	peer "github.com/ipfs/go-libp2p/p2p/peer"
Jeromy's avatar
Jeromy committed
10
	ci "util/testutil/ci"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11

Jeromy's avatar
Jeromy committed
12
	ma "QmaA6aDzeHjZiuqBtgYRz8ZXb1qMCoyMHgyDjBEYQniUKF/go-multiaddr"
Jeromy's avatar
Jeromy committed
13
	context "golang.org/x/net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
15
16
17
)

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

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

	// connect everyone
	{
		var wg sync.WaitGroup
		connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
			// copy for other peer
28
			log.Debugf("TestSimultOpen: connecting: %s --> %s (%s)", s.local, dst, addr)
29
			s.peers.AddAddr(dst, addr, peer.PermanentAddrTTL)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
31
32
33
34
35
36
37
			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)
38
39
		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
40
41
42
43
44
45
46
47
48
49
50
51
		wg.Wait()
	}

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

func TestSimultOpenMany(t *testing.T) {
	// t.Skip("very very slow")

	addrs := 20
52
	rounds := 10
Jeromy's avatar
Jeromy committed
53
54
	if ci.IsRunning() || runtime.GOOS == "darwin" {
		// osx has a limit of 256 file descriptors
55
56
57
58
		addrs = 10
		rounds = 5
	}
	SubtestSwarm(t, addrs, rounds)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
60
61
62
63
64
65
}

func TestSimultOpenFewStress(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	// t.Skip("skipping for another test")
66
	t.Parallel()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
68
69
70
71
72
73
74
75
76
77

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

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