rpc: decouple test fixtures from node implementation (#6533)

This commit is contained in:
Sam Kleinman
2021-06-04 09:10:38 -04:00
committed by GitHub
parent 618c945d54
commit 663c0bba9c
15 changed files with 366 additions and 229 deletions

View File

@@ -21,10 +21,17 @@ import (
// Automatically getting new headers and verifying them.
func ExampleClient_Update() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conf := rpctest.CreateConfig()
// Start a test application
app := kvstore.NewApplication()
n := rpctest.StartTendermint(app, rpctest.SuppressStdout)
defer func() { rpctest.StopTendermint(n) }()
_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
if err != nil {
stdlog.Fatal(err)
}
defer func() { _ = closer(ctx) }()
// give Tendermint time to generate some blocks
time.Sleep(5 * time.Second)
@@ -35,17 +42,14 @@ func ExampleClient_Update() {
}
defer os.RemoveAll(dbDir)
var (
config = n.Config()
chainID = config.ChainID()
)
chainID := conf.ChainID()
primary, err := httpp.New(chainID, config.RPC.ListenAddress)
primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
if err != nil {
stdlog.Fatal(err)
}
block, err := primary.LightBlock(context.Background(), 2)
block, err := primary.LightBlock(ctx, 2)
if err != nil {
stdlog.Fatal(err)
}
@@ -56,7 +60,7 @@ func ExampleClient_Update() {
}
c, err := light.NewClient(
context.Background(),
ctx,
chainID,
light.TrustOptions{
Period: 504 * time.Hour, // 21 days
@@ -79,7 +83,7 @@ func ExampleClient_Update() {
time.Sleep(2 * time.Second)
h, err := c.Update(context.Background(), time.Now())
h, err := c.Update(ctx, time.Now())
if err != nil {
stdlog.Fatal(err)
}
@@ -94,10 +98,18 @@ func ExampleClient_Update() {
// Manually getting light blocks and verifying them.
func ExampleClient_VerifyLightBlockAtHeight() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conf := rpctest.CreateConfig()
// Start a test application
app := kvstore.NewApplication()
n := rpctest.StartTendermint(app, rpctest.SuppressStdout)
defer func() { rpctest.StopTendermint(n) }()
_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
if err != nil {
stdlog.Fatal(err)
}
defer func() { _ = closer(ctx) }()
// give Tendermint time to generate some blocks
time.Sleep(5 * time.Second)
@@ -108,17 +120,14 @@ func ExampleClient_VerifyLightBlockAtHeight() {
}
defer os.RemoveAll(dbDir)
var (
config = n.Config()
chainID = config.ChainID()
)
chainID := conf.ChainID()
primary, err := httpp.New(chainID, config.RPC.ListenAddress)
primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
if err != nil {
stdlog.Fatal(err)
}
block, err := primary.LightBlock(context.Background(), 2)
block, err := primary.LightBlock(ctx, 2)
if err != nil {
stdlog.Fatal(err)
}
@@ -128,8 +137,7 @@ func ExampleClient_VerifyLightBlockAtHeight() {
stdlog.Fatal(err)
}
c, err := light.NewClient(
context.Background(),
c, err := light.NewClient(ctx,
chainID,
light.TrustOptions{
Period: 504 * time.Hour, // 21 days

View File

@@ -3,16 +3,16 @@ package http_test
import (
"context"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/abci/example/kvstore"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/service"
"github.com/tendermint/tendermint/light/provider"
lighthttp "github.com/tendermint/tendermint/light/provider/http"
"github.com/tendermint/tendermint/node"
rpcclient "github.com/tendermint/tendermint/rpc/client"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
rpctest "github.com/tendermint/tendermint/rpc/test"
@@ -33,23 +33,30 @@ func TestNewProvider(t *testing.T) {
require.Equal(t, fmt.Sprintf("%s", c), "http{http://153.200.0.1}")
}
func NodeSuite(t *testing.T) *node.Node {
// NodeSuite initiates and runs a full node instance in the
// background, stopping it once the test is completed
func NodeSuite(t *testing.T) (service.Service, *config.Config) {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
conf := rpctest.CreateConfig()
// start a tendermint node in the background to test against
app := kvstore.NewApplication()
app.RetainBlocks = 9
node := rpctest.StartTendermint(app)
node, closer, err := rpctest.StartTendermint(ctx, conf, app)
require.NoError(t, err)
t.Cleanup(func() {
rpctest.StopTendermint(node)
os.RemoveAll(node.Config().RootDir)
_ = closer(ctx)
cancel()
})
return node
return node, conf
}
func TestProvider(t *testing.T) {
n := NodeSuite(t)
cfg := n.Config()
_, cfg := NodeSuite(t)
rpcAddr := cfg.RPC.ListenAddress
genDoc, err := types.GenesisDocFromFile(cfg.GenesisFile())
require.NoError(t, err)