Skip to content

Commit

Permalink
Enabled Parallel runs for TestChainComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
silaslenihan committed Dec 23, 2024
1 parent ed1b2f6 commit 814f55b
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions integration-tests/relayinterface/chain_components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"
"path/filepath"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -36,9 +37,28 @@ import (

func TestChainComponents(t *testing.T) {
t.Parallel()
it := &SolanaChainComponentsInterfaceTester[*testing.T]{Helper: &helper{}}
it.Init(t)
helper := &helper{}
helper.Init(t)

t.Run("RunChainComponentsSolanaTests", func(t *testing.T) {
t.Parallel()
it := &SolanaChainComponentsInterfaceTester[*testing.T]{Helper: helper}
DisableTests(it)
it.Setup(t)
RunChainComponentsSolanaTests(t, it)
})

t.Run("RunChainComponentsInLoopSolanaTests", func(t *testing.T) {
t.Parallel()
it := &SolanaChainComponentsInterfaceTester[*testing.T]{Helper: helper}
DisableTests(it)
wrapped := commontestutils.WrapContractReaderTesterForLoop(it)
wrapped.Setup(t)
RunChainComponentsInLoopSolanaTests(t, wrapped)
})
}

func DisableTests(it *SolanaChainComponentsInterfaceTester[*testing.T]) {
it.DisableTests([]string{
// disable tests that set values
ContractReaderGetLatestValueBasedOnConfidenceLevel,
Expand Down Expand Up @@ -70,9 +90,6 @@ func TestChainComponents(t *testing.T) {
ContractReaderQueryKeysCanFilterWithValueComparator,
ContractReaderQueryKeysCanLimitResultsWithCursor,
})

RunChainComponentsSolanaTests(t, it)
RunChainComponentsInLoopSolanaTests(t, commontestutils.WrapContractReaderTesterForLoop(it))
}

func RunChainComponentsSolanaTests[T TestingT[T]](t T, it *SolanaChainComponentsInterfaceTester[T]) {
Expand All @@ -86,15 +103,15 @@ func RunChainComponentsInLoopSolanaTests[T TestingT[T]](t T, it ChainComponentsI
}

func RunContractReaderSolanaTests[T TestingT[T]](t T, it *SolanaChainComponentsInterfaceTester[T]) {
RunContractReaderInterfaceTests(t, it, false, false)
RunContractReaderInterfaceTests(t, it, false, true)

testCases := []Testcase[T]{}

RunTests(t, it, testCases)
}

func RunContractReaderInLoopTests[T TestingT[T]](t T, it ChainComponentsInterfaceTester[T]) {
RunContractReaderInterfaceTests(t, it, false, false)
RunContractReaderInterfaceTests(t, it, false, true)

testCases := []Testcase[T]{}

Expand All @@ -112,11 +129,9 @@ type SolanaChainComponentsInterfaceTesterHelper[T TestingT[T]] interface {

type SolanaChainComponentsInterfaceTester[T TestingT[T]] struct {
TestSelectionSupport
Helper SolanaChainComponentsInterfaceTesterHelper[T]
cr *chainreader.SolanaChainReaderService
chainReaderConfig config.ChainReader
accountPubKey solana.PublicKey
secondAccountPubKey solana.PublicKey
Helper SolanaChainComponentsInterfaceTesterHelper[T]
cr *chainreader.SolanaChainReaderService
chainReaderConfig config.ChainReader
}

func (it *SolanaChainComponentsInterfaceTester[T]) Setup(t T) {
Expand Down Expand Up @@ -164,9 +179,6 @@ func (it *SolanaChainComponentsInterfaceTester[T]) Setup(t T) {
},
},
}

it.accountPubKey = it.Helper.CreateAccount(t, AnyValueToReadWithoutAnArgument)
it.secondAccountPubKey = it.Helper.CreateAccount(t, AnyDifferentValueToReadWithoutAnArgument)
}

func (it *SolanaChainComponentsInterfaceTester[T]) Name() string {
Expand Down Expand Up @@ -202,10 +214,10 @@ func (it *SolanaChainComponentsInterfaceTester[T]) GetContractWriter(t T) types.
}

func (it *SolanaChainComponentsInterfaceTester[T]) GetBindings(t T) []types.BoundContract {
// at the moment, use only a single account address for everything
// Create a new account with fresh state for each test
return []types.BoundContract{
{Name: AnyContractName, Address: it.accountPubKey.String()},
{Name: AnySecondContractName, Address: it.secondAccountPubKey.String()},
{Name: AnyContractName, Address: it.Helper.CreateAccount(t, AnyValueToReadWithoutAnArgument).String()},
{Name: AnySecondContractName, Address: it.Helper.CreateAccount(t, AnyDifferentValueToReadWithoutAnArgument).String()},
}
}

Expand All @@ -219,10 +231,6 @@ func (it *SolanaChainComponentsInterfaceTester[T]) GenerateBlocksTillConfidenceL

}

func (it *SolanaChainComponentsInterfaceTester[T]) Init(t T) {
it.Helper.Init(t)
}

type helper struct {
programID solana.PublicKey
rpcURL string
Expand All @@ -231,6 +239,7 @@ type helper struct {
wsClient *ws.Client
idlBts []byte
nonce uint64
nonceMu sync.Mutex
}

func (h *helper) Init(t *testing.T) {
Expand Down Expand Up @@ -292,10 +301,14 @@ func (h *helper) GetJSONEncodedIDL(t *testing.T) []byte {
func (h *helper) CreateAccount(t *testing.T, value uint64) solana.PublicKey {
t.Helper()

// avoid collisions in parallel tests
h.nonceMu.Lock()
h.nonce++
nonce := h.nonce
h.nonceMu.Unlock()

bts := make([]byte, 8)
binary.LittleEndian.PutUint64(bts, h.nonce*value)
binary.LittleEndian.PutUint64(bts, nonce*value)

pubKey, _, err := solana.FindProgramAddress([][]byte{[]byte("data"), bts}, h.programID)
require.NoError(t, err)
Expand All @@ -304,7 +317,7 @@ func (h *helper) CreateAccount(t *testing.T, value uint64) solana.PublicKey {
privateKey, err := solana.PrivateKeyFromBase58(solclient.DefaultPrivateKeysSolValidator[1])
require.NoError(t, err)

h.runInitialize(t, value, pubKey, func(key solana.PublicKey) *solana.PrivateKey {
h.runInitialize(t, nonce, value, pubKey, func(key solana.PublicKey) *solana.PrivateKey {
return &privateKey
}, privateKey.PublicKey())

Expand All @@ -313,14 +326,15 @@ func (h *helper) CreateAccount(t *testing.T, value uint64) solana.PublicKey {

func (h *helper) runInitialize(
t *testing.T,
nonce uint64,
value uint64,
data solana.PublicKey,
signerFunc func(key solana.PublicKey) *solana.PrivateKey,
payer solana.PublicKey,
) {
t.Helper()

inst, err := contract.NewInitializeInstruction(h.nonce*value, value, data, payer, solana.SystemProgramID).ValidateAndBuild()
inst, err := contract.NewInitializeInstruction(nonce*value, value, data, payer, solana.SystemProgramID).ValidateAndBuild()
require.NoError(t, err)

h.sendInstruction(t, inst, signerFunc, payer)
Expand Down

0 comments on commit 814f55b

Please sign in to comment.