Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Farber98 committed Jan 6, 2025
1 parent 2826ad1 commit 71662e1
Showing 1 changed file with 74 additions and 69 deletions.
143 changes: 74 additions & 69 deletions pkg/solana/txm/txm_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,74 +191,9 @@ func createTransaction(ctx context.Context, t *testing.T, client *solanaClient.C

func TestTxm_Integration_Reorg(t *testing.T) {
t.Parallel()

// startValidator is a helper func that starts a local solana-test-validator and return the cmd to control it.
startValidator := func(t *testing.T, ledgerDir, port, faucetPort string, reset bool) (*exec.Cmd, string) {
t.Helper()

args := []string{
"--rpc-port", port,
"--faucet-port", faucetPort,
"--ledger", ledgerDir,
}
if reset {
args = append([]string{"--reset"}, args...)
}

cmd := exec.Command("solana-test-validator", args...)

var stdErr, stdOut bytes.Buffer
cmd.Stderr = &stdErr
cmd.Stdout = &stdOut

require.NoError(t, cmd.Start(), "failed to start solana-test-validator")

// The RPC URL
url := "http://127.0.0.1:" + port

// Ensure validator is killed after the test finishes
t.Cleanup(func() {
_ = cmd.Process.Kill()
_ = cmd.Wait()
})

// Wait until it's healthy
client := rpc.New(url)
require.Eventually(t, func() bool {
out, err := client.GetHealth(context.Background())
return err == nil && out == rpc.HealthOk
}, 30*time.Second, 1*time.Second, "Validator should become healthy")

return cmd, url
}

// copyDir is a helper func that copies the directory tree.
copyDir := func(src, dst string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
dstPath := filepath.Join(dst, relPath)

if info.IsDir() {
return os.MkdirAll(dstPath, info.Mode())
}

data, err := os.ReadFile(path)
if err != nil {
return err
}

return os.WriteFile(dstPath, data, info.Mode())
})
}

t.Run("no reorg", func(t *testing.T) {
// Setup live validator and test environment
t.Parallel()
url := solanaClient.SetupLocalSolNode(t)
ctx, client, txmInstance, senderPubKey, receiverPubKey, observer := setup(t, url, true)

Expand Down Expand Up @@ -290,8 +225,9 @@ func TestTxm_Integration_Reorg(t *testing.T) {
require.Equal(t, amount, finalReceiverBalance, "Receiver should receive the transferred amount")
})

t.Run("confirmed reorg", func(t *testing.T) {
t.Run("confirmed reorg: previous tx is replaced and new one is finalized", func(t *testing.T) {
// Start live validator and setup test environment
t.Parallel()
ledgerDir := t.TempDir()
port := utils.MustRandomPort(t)
faucetPort := utils.MustRandomPort(t)
Expand All @@ -306,7 +242,7 @@ func TestTxm_Integration_Reorg(t *testing.T) {

// Create TX and wait for it to be confirmed
const amount = 1 * solana.LAMPORTS_PER_SOL
txID := "reorg"
txID := "reorg-test-tx"
tx, lastValidBlockHeight := createTransaction(ctx, t, cl, senderPubKey, receiverPubKey, amount, true)
require.NoError(t, txmInstance.Enqueue(ctx, "", tx, &txID, lastValidBlockHeight))
require.Eventually(t, func() bool {
Expand Down Expand Up @@ -344,7 +280,7 @@ func TestTxm_Integration_Reorg(t *testing.T) {
reorgLogs := obs.FilterMessageSnippet("re-org detected for transaction").Len()
require.Equal(t, reorgLogs, 1, "Re-org should be detected")
rebroadcastReorgLogs := obs.FilterMessageSnippet("re-orged tx was rebroadcasted successfully").Len()
require.Equal(t, rebroadcastReorgLogs, 1, "re-org tx should be rebroadcasted successfully")
require.Equal(t, rebroadcastReorgLogs, 1, "re-org tx should be rebroadcasted with new blockhash")

// Wait rebroadcasted tx to be finalized and check final balances
require.Eventually(t, func() bool {
Expand All @@ -365,3 +301,72 @@ func TestTxm_Integration_Reorg(t *testing.T) {
require.Equal(t, types.Finalized, status, "tx should be finalized after reorg")
})
}

// startValidator starts a local solana-test-validator and return the cmd to control it.
func startValidator(
t *testing.T,
ledgerDir, port, faucetPort string,
reset bool,
) (*exec.Cmd, string) {
t.Helper()

args := []string{
"--rpc-port", port,
"--faucet-port", faucetPort,
"--ledger", ledgerDir,
}
if reset {
args = append([]string{"--reset"}, args...)
}

cmd := exec.Command("solana-test-validator", args...)

var stdErr, stdOut bytes.Buffer
cmd.Stderr = &stdErr
cmd.Stdout = &stdOut

require.NoError(t, cmd.Start(), "failed to start solana-test-validator")

// The RPC URL
url := "http://127.0.0.1:" + port

// Ensure validator is killed after the test finishes
t.Cleanup(func() {
_ = cmd.Process.Kill()
_ = cmd.Wait()
})

// Wait until it's healthy
client := rpc.New(url)
require.Eventually(t, func() bool {
out, err := client.GetHealth(context.Background())
return err == nil && out == rpc.HealthOk
}, 30*time.Second, 1*time.Second, "Validator should become healthy")

return cmd, url
}

// copyDir copies the directory tree.
func copyDir(src, dst string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
dstPath := filepath.Join(dst, relPath)

if info.IsDir() {
return os.MkdirAll(dstPath, info.Mode())
}

data, err := os.ReadFile(path)
if err != nil {
return err
}

return os.WriteFile(dstPath, data, info.Mode())
})
}

0 comments on commit 71662e1

Please sign in to comment.