context: cleaning up context dead ends (#7963)

This commit is contained in:
Sam Kleinman
2022-02-22 15:04:16 -05:00
committed by GitHub
parent 912751cf93
commit 2ffb262600
9 changed files with 15 additions and 17 deletions

View File

@@ -1,7 +1,6 @@
package commands
import (
"context"
"errors"
"fmt"
"net/http"
@@ -149,7 +148,7 @@ for applications built w/ Cosmos SDK).
// Initiate the light client. If the trusted store already has blocks in it, this
// will be used else we use the trusted options.
c, err := light.NewHTTPClient(
context.Background(),
cmd.Context(),
chainID,
light.TrustOptions{
Period: trustingPeriod,

View File

@@ -57,7 +57,7 @@ func (env *Environment) Subscribe(ctx context.Context, query string) (*coretypes
// Capture the current ID, since it can change in the future.
subscriptionID := callInfo.RPCRequest.ID
go func() {
opctx, opcancel := context.WithCancel(context.Background())
opctx, opcancel := context.WithCancel(context.TODO())
defer opcancel()
for {

View File

@@ -51,7 +51,7 @@ func testTxEventsSent(ctx context.Context, t *testing.T, broadcastMethod string,
}()
// and wait for confirmation
evt, err := client.WaitForOneEvent(c, types.EventTxValue, waitForEventTimeout)
evt, err := client.WaitForOneEvent(ctx, c, types.EventTxValue, waitForEventTimeout)
require.NoError(t, err)
// and make sure it has the proper info

View File

@@ -57,9 +57,9 @@ func WaitForHeight(ctx context.Context, c StatusClient, h int64, waiter Waiter)
// when the timeout duration has expired.
//
// This handles subscribing and unsubscribing under the hood
func WaitForOneEvent(c EventsClient, eventValue string, timeout time.Duration) (types.EventData, error) {
func WaitForOneEvent(ctx context.Context, c EventsClient, eventValue string, timeout time.Duration) (types.EventData, error) {
const subscriber = "helpers"
ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
// register for the next event of this type

View File

@@ -140,7 +140,7 @@ func (w *wsEvents) UnsubscribeAll(ctx context.Context, subscriber string) error
func (w *wsEvents) redoSubscriptionsAfter(d time.Duration) {
time.Sleep(d)
ctx := context.Background()
ctx := context.TODO()
w.mtx.Lock()
defer w.mtx.Unlock()

View File

@@ -15,10 +15,10 @@ import (
rpctest "github.com/tendermint/tendermint/rpc/test"
)
func NodeSuite(t *testing.T, logger log.Logger) (service.Service, *config.Config) {
func NodeSuite(ctx context.Context, t *testing.T, logger log.Logger) (service.Service, *config.Config) {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx)
conf, err := rpctest.CreateConfig(t, t.Name())
require.NoError(t, err)

View File

@@ -95,7 +95,7 @@ func TestClientOperations(t *testing.T) {
logger := log.NewTestingLogger(t)
_, conf := NodeSuite(t, logger)
_, conf := NodeSuite(ctx, t, logger)
t.Run("NilCustomHTTPClient", func(t *testing.T) {
_, err := rpchttp.NewWithClient("http://example.com", nil)
@@ -193,7 +193,7 @@ func TestClientMethodCalls(t *testing.T) {
defer cancel()
logger := log.NewTestingLogger(t)
n, conf := NodeSuite(t, logger)
n, conf := NodeSuite(ctx, t, logger)
// for broadcast tx tests
pool := getMempool(t, n)
@@ -471,7 +471,7 @@ func TestClientMethodCalls(t *testing.T) {
})
t.Run("Events", func(t *testing.T) {
t.Run("Header", func(t *testing.T) {
evt, err := client.WaitForOneEvent(c, types.EventNewBlockHeaderValue, waitForEventTimeout)
evt, err := client.WaitForOneEvent(ctx, c, types.EventNewBlockHeaderValue, waitForEventTimeout)
require.NoError(t, err, "%d: %+v", i, err)
_, ok := evt.(types.EventDataNewBlockHeader)
require.True(t, ok, "%d: %#v", i, evt)
@@ -583,7 +583,7 @@ func TestClientMethodCallsAdvanced(t *testing.T) {
logger := log.NewTestingLogger(t)
n, conf := NodeSuite(t, logger)
n, conf := NodeSuite(ctx, t, logger)
pool := getMempool(t, n)
t.Run("UnconfirmedTxs", func(t *testing.T) {

View File

@@ -175,7 +175,7 @@ func startLightNode(ctx context.Context, cfg *Config) error {
providers := rpcEndpoints(tmcfg.P2P.PersistentPeers)
c, err := light.NewHTTPClient(
context.Background(),
ctx,
cfg.ChainID,
light.TrustOptions{
Period: tmcfg.StateSync.TrustPeriod,

View File

@@ -43,7 +43,7 @@ func Benchmark(ctx context.Context, testnet *e2e.Testnet, benchmarkLength int64)
logger.Info("Ending benchmark period", "height", block.Height)
// fetch a sample of blocks
blocks, err := fetchBlockChainSample(testnet, benchmarkLength)
blocks, err := fetchBlockChainSample(ctx, testnet, benchmarkLength)
if err != nil {
return err
}
@@ -128,7 +128,7 @@ func (t *testnetStats) String() string {
// fetchBlockChainSample waits for `benchmarkLength` amount of blocks to pass, fetching
// all of the headers for these blocks from an archive node and returning it.
func fetchBlockChainSample(testnet *e2e.Testnet, benchmarkLength int64) ([]*types.BlockMeta, error) {
func fetchBlockChainSample(ctx context.Context, testnet *e2e.Testnet, benchmarkLength int64) ([]*types.BlockMeta, error) {
var blocks []*types.BlockMeta
// Find the first archive node
@@ -139,7 +139,6 @@ func fetchBlockChainSample(testnet *e2e.Testnet, benchmarkLength int64) ([]*type
}
// find the latest height
ctx := context.Background()
s, err := c.Status(ctx)
if err != nil {
return nil, err