simul_test.go 1.54 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"

9
	ci "github.com/libp2p/go-libp2p/testutil/ci"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10

11
	"context"
Jeromy's avatar
Jeromy committed
12
13
	peer "github.com/ipfs/go-libp2p-peer"
	pstore "github.com/ipfs/go-libp2p-peerstore"
Jeromy's avatar
Jeromy committed
14
	ma "github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
16
17
)

func TestSimultOpen(t *testing.T) {
Jeromy's avatar
Jeromy committed
18

19
	t.Parallel()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
21

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

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

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

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

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

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

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

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