Skip to content

Commit

Permalink
Remove JSONRPC client leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Jun 13, 2024
1 parent 8908c62 commit 966ac43
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ func TestJRPCRequestDurationLimiter_Limiting(t *testing.T) {
logCounter.Entry()).Handle

ch := jhttp.NewChannel("http://"+addr+"/", nil)
defer ch.Close()
client := jrpc2.NewClient(ch, nil)
defer client.Close()

var res interface{}
req := struct {
Expand Down Expand Up @@ -250,7 +252,9 @@ func TestJRPCRequestDurationLimiter_NoLimiting(t *testing.T) {
logCounter.Entry()).Handle

ch := jhttp.NewChannel("http://"+addr+"/", nil)
defer ch.Close()
client := jrpc2.NewClient(ch, nil)
defer client.Close()

var res interface{}
req := struct {
Expand Down Expand Up @@ -291,7 +295,9 @@ func TestJRPCRequestDurationLimiter_NoLimiting_Warn(t *testing.T) {
logCounter.Entry()).Handle

ch := jhttp.NewChannel("http://"+addr+"/", nil)
defer ch.Close()
client := jrpc2.NewClient(ch, nil)
defer client.Close()

var res interface{}
req := struct {
Expand Down
6 changes: 1 addition & 5 deletions cmd/soroban-rpc/internal/test/get_fee_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"testing"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stellar/go/keypair"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
Expand All @@ -18,9 +16,7 @@ import (
func TestGetFeeStats(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)

client := test.GetRPCLient()
sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
account := txnbuild.NewSimpleAccount(address, 0)
Expand Down
10 changes: 3 additions & 7 deletions cmd/soroban-rpc/internal/test/get_ledger_entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -20,8 +19,7 @@ import (
func TestGetLedgerEntriesNotFound(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address()
contractID := getContractID(t, sourceAccount, testSalt, StandaloneNetworkPassphrase)
Expand Down Expand Up @@ -58,8 +56,7 @@ func TestGetLedgerEntriesNotFound(t *testing.T) {
func TestGetLedgerEntriesInvalidParams(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

var keys []string
keys = append(keys, "<>@@#$")
Expand All @@ -76,8 +73,7 @@ func TestGetLedgerEntriesInvalidParams(t *testing.T) {
func TestGetLedgerEntriesSucceeds(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
Expand Down
10 changes: 3 additions & 7 deletions cmd/soroban-rpc/internal/test/get_ledger_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stellar/go/txnbuild"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -20,8 +19,7 @@ import (
func TestGetLedgerEntryNotFound(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address()
contractID := getContractID(t, sourceAccount, testSalt, StandaloneNetworkPassphrase)
Expand Down Expand Up @@ -53,8 +51,7 @@ func TestGetLedgerEntryNotFound(t *testing.T) {
func TestGetLedgerEntryInvalidParams(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

request := methods.GetLedgerEntryRequest{
Key: "<>@@#$",
Expand All @@ -69,8 +66,7 @@ func TestGetLedgerEntryInvalidParams(t *testing.T) {
func TestGetLedgerEntrySucceeds(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

kp := keypair.Root(StandaloneNetworkPassphrase)
account := txnbuild.NewSimpleAccount(kp.Address(), 0)
Expand Down
5 changes: 1 addition & 4 deletions cmd/soroban-rpc/internal/test/get_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"testing"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stretchr/testify/assert"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/methods"
Expand All @@ -14,8 +12,7 @@ import (
func TestGetNetworkSucceeds(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

request := methods.GetNetworkRequest{}

Expand Down
4 changes: 1 addition & 3 deletions cmd/soroban-rpc/internal/test/get_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stellar/go/keypair"
"github.com/stellar/go/txnbuild"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -57,8 +56,7 @@ func sendTransactions(t *testing.T, client *jrpc2.Client) []uint32 {

func TestGetTransactions(t *testing.T) {
test := NewTest(t, nil)
ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

ledgers := sendTransactions(t, client)

Expand Down
8 changes: 3 additions & 5 deletions cmd/soroban-rpc/internal/test/get_version_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package test
import (
"context"
"fmt"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config"
"os/exec"
"testing"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config"

"github.com/stretchr/testify/assert"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/methods"
Expand All @@ -28,8 +27,7 @@ func TestGetVersionInfoSucceeds(t *testing.T) {
config.BuildTimestamp = buildTimeStamp
})

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

var result methods.GetVersionInfoResponse
err := client.CallResult(context.Background(), "getVersionInfo", nil, &result)
Expand Down
5 changes: 1 addition & 4 deletions cmd/soroban-rpc/internal/test/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"testing"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stretchr/testify/assert"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/ledgerbucketwindow"
Expand All @@ -15,8 +13,7 @@ import (
func TestHealth(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

var result methods.HealthCheckResult
if err := client.CallResult(context.Background(), "getHealth", nil, &result); err != nil {
Expand Down
16 changes: 11 additions & 5 deletions cmd/soroban-rpc/internal/test/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type Test struct {
masterAccount txnbuild.Account
shutdownOnce sync.Once
shutdownCalls []func()
rpcClient *jrpc2.Client
}

func NewTest(t *testing.T, cfg *TestConfig) *Test {
Expand Down Expand Up @@ -119,6 +120,9 @@ func NewTest(t *testing.T, cfg *TestConfig) *Test {
proxy.ServeHTTP(w, r)
}))

ch := jhttp.NewChannel(i.sorobanRPCURL(), nil)
t.Cleanup(func() { ch.Close() })
i.rpcClient = jrpc2.NewClient(ch, nil)
rpcCfg := i.getRPConfig(sqlLitePath)
if i.rpcContainerVersion != "" {
i.rpcContainerConfigMountDir = i.createRPCContainerMountDir(rpcCfg)
Expand All @@ -137,6 +141,9 @@ func NewTest(t *testing.T, cfg *TestConfig) *Test {
return i
}

func (i *Test) GetRPCLient() *jrpc2.Client {
return i.rpcClient
}
func (i *Test) MasterKey() *keypair.Full {
return keypair.Root(StandaloneNetworkPassphrase)
}
Expand Down Expand Up @@ -237,13 +244,9 @@ func (i *Test) getRPConfig(sqlitePath string) map[string]string {
func (i *Test) waitForRPC() {
i.t.Log("Waiting for RPC to be healthy...")

ch := jhttp.NewChannel(i.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
defer client.Close()

var result methods.HealthCheckResult
for t := 120; t >= 0; t-- {
err := client.CallResult(context.Background(), "getHealth", nil, &result)
err := i.rpcClient.CallResult(context.Background(), "getHealth", nil, &result)
if err == nil {
if result.Status == "healthy" {
i.t.Log("RPC is healthy")
Expand Down Expand Up @@ -349,6 +352,9 @@ func (i *Test) prepareShutdownHandlers() {
if i.historyArchiveProxy != nil {
i.historyArchiveProxy.Close()
}
if i.rpcClient != nil {
i.rpcClient.Close()
}
i.runComposeCommand("down", "-v")
},
)
Expand Down
5 changes: 1 addition & 4 deletions cmd/soroban-rpc/internal/test/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"strings"
"testing"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stellar/go/keypair"
"github.com/stellar/go/txnbuild"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -53,8 +51,7 @@ func testMigrateFromVersion(t *testing.T, version string) {
UseSQLitePath: sqliteFile,
})

ch := jhttp.NewChannel(it.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := it.GetRPCLient()

// Submit an event-logging transaction in the version to migrate from
kp := keypair.Root(StandaloneNetworkPassphrase)
Expand Down
32 changes: 10 additions & 22 deletions cmd/soroban-rpc/internal/test/simulate_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -191,9 +190,7 @@ func preflightTransactionParams(t *testing.T, client *jrpc2.Client, params txnbu
func TestSimulateTransactionSucceeds(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)

client := test.GetRPCLient()
sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address()
contractBinary := getHelloWorldContract(t)
params := txnbuild.TransactionParams{
Expand Down Expand Up @@ -323,8 +320,7 @@ func TestSimulateTransactionSucceeds(t *testing.T) {
func TestSimulateTransactionWithAuth(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
Expand Down Expand Up @@ -381,8 +377,7 @@ func TestSimulateTransactionWithAuth(t *testing.T) {
func TestSimulateInvokeContractTransactionSucceeds(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
Expand Down Expand Up @@ -565,8 +560,7 @@ func TestSimulateInvokeContractTransactionSucceeds(t *testing.T) {
func TestSimulateTransactionError(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address()
invokeHostOp := createInvokeHostOperation(sourceAccount, xdr.Hash{}, "noMethod")
Expand Down Expand Up @@ -605,8 +599,7 @@ func TestSimulateTransactionError(t *testing.T) {
func TestSimulateTransactionMultipleOperations(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address()
contractBinary := getHelloWorldContract(t)
Expand Down Expand Up @@ -640,8 +633,7 @@ func TestSimulateTransactionMultipleOperations(t *testing.T) {
func TestSimulateTransactionWithoutInvokeHostFunction(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

params := txnbuild.TransactionParams{
SourceAccount: &txnbuild.SimpleAccount{
Expand Down Expand Up @@ -671,8 +663,7 @@ func TestSimulateTransactionWithoutInvokeHostFunction(t *testing.T) {
func TestSimulateTransactionUnmarshalError(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

request := methods.SimulateTransactionRequest{Transaction: "invalid"}
var result methods.SimulateTransactionResponse
Expand All @@ -688,8 +679,7 @@ func TestSimulateTransactionUnmarshalError(t *testing.T) {
func TestSimulateTransactionExtendAndRestoreFootprint(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
Expand Down Expand Up @@ -921,8 +911,7 @@ func waitUntilLedgerEntryTTL(t *testing.T, client *jrpc2.Client, ledgerKey xdr.L
func TestSimulateInvokePrng_u64_in_range(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
Expand Down Expand Up @@ -1032,8 +1021,7 @@ func TestSimulateInvokePrng_u64_in_range(t *testing.T) {
func TestSimulateSystemEvent(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
Expand Down
Loading

0 comments on commit 966ac43

Please sign in to comment.