mock.go 1.23 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
package mocknet

import (
Jeromy's avatar
WIP    
Jeromy committed
4
	logging "github.com/ipfs/go-log"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5

Jeromy's avatar
WIP    
Jeromy committed
6
	context "golang.org/x/net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
8
)

Jeromy's avatar
Jeromy committed
9
var log = logging.Logger("mocknet")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// WithNPeers constructs a Mocknet with N peers.
func WithNPeers(ctx context.Context, n int) (Mocknet, error) {
	m := New(ctx)
	for i := 0; i < n; i++ {
		if _, err := m.GenPeer(); err != nil {
			return nil, err
		}
	}
	return m, nil
}

// FullMeshLinked constructs a Mocknet with full mesh of Links.
// This means that all the peers **can** connect to each other
// (not that they already are connected. you can use m.ConnectAll())
func FullMeshLinked(ctx context.Context, n int) (Mocknet, error) {
	m, err := WithNPeers(ctx, n)
	if err != nil {
		return nil, err
	}

rht's avatar
rht committed
31
32
	if err := m.LinkAll(); err != nil {
		return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
	}

	return m, nil
}

// FullMeshConnected constructs a Mocknet with full mesh of Connections.
// This means that all the peers have dialed and are ready to talk to
// each other.
func FullMeshConnected(ctx context.Context, n int) (Mocknet, error) {
	m, err := FullMeshLinked(ctx, n)
	if err != nil {
		return nil, err
	}

	nets := m.Nets()
	for _, n1 := range nets {
		for _, n2 := range nets {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50
			if _, err := m.ConnectNets(n1, n2); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
52
53
54
55
56
57
				return nil, err
			}
		}
	}

	return m, nil
}