run-test.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. ################################################################################
  3. #
  4. # A script to run the example as an integration test. It starts up a localnet
  5. # and executes the current directory's rust binary.
  6. #
  7. # Usage:
  8. #
  9. # ./run.sh
  10. #
  11. # Run this script from within the `example/` directory in which it is located.
  12. # The anchor cli must be installed.
  13. #
  14. # cargo install --git https://github.com/coral-xyz/anchor anchor-cli --locked
  15. #
  16. ################################################################################
  17. set -euox pipefail
  18. main() {
  19. #
  20. # Build programs.
  21. #
  22. local composite_pid="EHthziFziNoac9LBGxEaVN47Y3uUiRoXvqAiR6oes4iU"
  23. local basic_2_pid="Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
  24. local basic_4_pid="CwrqeMj2U8tFr1Rhkgwc84tpAsqbt9pTt2a4taoTADPr"
  25. local events_pid="2dhGsWUzy5YKUsjZdLHLmkNpUDAXkNa9MYWsPc4Ziqzy"
  26. local optional_pid="FNqz6pqLAwvMSds2FYjR4nKV3moVpPNtvkfGFrqLKrgG"
  27. cd ../../tests/composite && anchor build && cd -
  28. [ $? -ne 0 ] && exit 1
  29. cd ../../examples/tutorial/basic-2 && anchor build && cd -
  30. [ $? -ne 0 ] && exit 1
  31. cd ../../examples/tutorial/basic-4 && anchor build && cd -
  32. [ $? -ne 0 ] && exit 1
  33. cd ../../tests/events && anchor build && cd -
  34. [ $? -ne 0 ] && exit 1
  35. cd ../../tests/optional && anchor build && cd -
  36. [ $? -ne 0 ] && exit 1
  37. #
  38. # Bootup validator.
  39. #
  40. solana-test-validator -r \
  41. --bpf-program $composite_pid ../../tests/composite/target/deploy/composite.so \
  42. --bpf-program $basic_2_pid ../../examples/tutorial/basic-2/target/deploy/basic_2.so \
  43. --bpf-program $basic_4_pid ../../examples/tutorial/basic-4/target/deploy/basic_4.so \
  44. --bpf-program $events_pid ../../tests/events/target/deploy/events.so \
  45. --bpf-program $optional_pid ../../tests/optional/target/deploy/optional.so \
  46. > test-validator.log &
  47. test_validator_pid=$!
  48. sleep 5
  49. check_solana_validator_running $test_validator_pid
  50. #
  51. # Run single threaded test.
  52. #
  53. cargo run -- \
  54. --composite-pid $composite_pid \
  55. --basic-2-pid $basic_2_pid \
  56. --basic-4-pid $basic_4_pid \
  57. --events-pid $events_pid \
  58. --optional-pid $optional_pid
  59. #
  60. # Restart validator for multithreaded test
  61. #
  62. cleanup
  63. solana-test-validator -r \
  64. --bpf-program $composite_pid ../../tests/composite/target/deploy/composite.so \
  65. --bpf-program $basic_2_pid ../../examples/tutorial/basic-2/target/deploy/basic_2.so \
  66. --bpf-program $basic_4_pid ../../examples/tutorial/basic-4/target/deploy/basic_4.so \
  67. --bpf-program $events_pid ../../tests/events/target/deploy/events.so \
  68. --bpf-program $optional_pid ../../tests/optional/target/deploy/optional.so \
  69. > test-validator.log &
  70. test_validator_pid=$!
  71. sleep 5
  72. check_solana_validator_running $test_validator_pid
  73. #
  74. # Run multi threaded test.
  75. #
  76. cargo run -- \
  77. --composite-pid $composite_pid \
  78. --basic-2-pid $basic_2_pid \
  79. --basic-4-pid $basic_4_pid \
  80. --events-pid $events_pid \
  81. --optional-pid $optional_pid \
  82. --multithreaded
  83. #
  84. # Restart validator for async test
  85. #
  86. cleanup
  87. solana-test-validator -r \
  88. --bpf-program $composite_pid ../../tests/composite/target/deploy/composite.so \
  89. --bpf-program $basic_2_pid ../../examples/tutorial/basic-2/target/deploy/basic_2.so \
  90. --bpf-program $basic_4_pid ../../examples/tutorial/basic-4/target/deploy/basic_4.so \
  91. --bpf-program $events_pid ../../tests/events/target/deploy/events.so \
  92. --bpf-program $optional_pid ../../tests/optional/target/deploy/optional.so \
  93. > test-validator.log &
  94. test_validator_pid=$!
  95. sleep 5
  96. check_solana_validator_running $test_validator_pid
  97. #
  98. # Run async test.
  99. #
  100. cargo run --features async -- \
  101. --composite-pid $composite_pid \
  102. --basic-2-pid $basic_2_pid \
  103. --basic-4-pid $basic_4_pid \
  104. --events-pid $events_pid \
  105. --optional-pid $optional_pid \
  106. --multithreaded
  107. }
  108. cleanup() {
  109. pkill -P $$ || true
  110. wait || true
  111. }
  112. trap_add() {
  113. trap_add_cmd=$1; shift || fatal "${FUNCNAME} usage error"
  114. for trap_add_name in "$@"; do
  115. trap -- "$(
  116. extract_trap_cmd() { printf '%s\n' "${3:-}"; }
  117. eval "extract_trap_cmd $(trap -p "${trap_add_name}")"
  118. printf '%s\n' "${trap_add_cmd}"
  119. )" "${trap_add_name}" \
  120. || fatal "unable to add to trap ${trap_add_name}"
  121. done
  122. }
  123. check_solana_validator_running() {
  124. local pid=$1
  125. exit_state=$(kill -0 "$pid" && echo 'living' || echo 'exited')
  126. if [ "$exit_state" == 'exited' ]; then
  127. echo "Cannot start test validator, see ./test-validator.log"
  128. exit 1
  129. fi
  130. }
  131. declare -f -t trap_add
  132. trap_add 'cleanup' EXIT
  133. main