forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
81 lines (68 loc) · 2.19 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package core
import (
"net"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/require"
tmconfig "github.com/tendermint/tendermint/config"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/celestiaorg/celestia-app/test/util/testnode"
)
// DefaultTestConfig returns the default testing configuration for Tendermint + Celestia App tandem.
//
// It fetches free ports from OS and sets them into configs, s.t.
// user can make use of them(unlike 0 port) and allowing to run
// multiple tests nodes in parallel.
//
// Additionally, it instructs Tendermint + Celestia App tandem to setup 10 funded accounts.
func DefaultTestConfig() *testnode.Config {
cfg := testnode.DefaultConfig()
// instructs creating funded accounts
// 10 usually is enough for testing
accounts := make([]string, 10)
for i := range accounts {
accounts[i] = tmrand.Str(9)
}
cfg.TmConfig.Consensus.TimeoutCommit = time.Millisecond * 200
cfg = cfg.
WithAccounts(accounts).
WithSupressLogs(true)
return cfg
}
// StartTestNode simply starts Tendermint and Celestia App tandem with default testing
// configuration.
func StartTestNode(t *testing.T) testnode.Context {
return StartTestNodeWithConfig(t, DefaultTestConfig())
}
// StartTestNodeWithConfig starts Tendermint and Celestia App tandem with custom configuration.
func StartTestNodeWithConfig(t *testing.T, cfg *testnode.Config) testnode.Context {
cctx, _, _ := testnode.NewNetwork(t, cfg)
// we want to test over remote http client,
// so we are as close to the real environment as possible
// however, it might be useful to use local tendermint client
// if you need to debug something inside of it
ip, port, err := getEndpoint(cfg.TmConfig)
require.NoError(t, err)
client, err := NewRemote(ip, port)
require.NoError(t, err)
err = client.Start()
require.NoError(t, err)
t.Cleanup(func() {
err := client.Stop()
require.NoError(t, err)
})
cctx.WithClient(client)
return cctx
}
func getEndpoint(cfg *tmconfig.Config) (string, string, error) {
url, err := url.Parse(cfg.RPC.ListenAddress)
if err != nil {
return "", "", err
}
host, _, err := net.SplitHostPort(url.Host)
if err != nil {
return "", "", err
}
return host, url.Port(), nil
}