test_transactor.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <pqxx/transaction>
  2. #include <pqxx/transactor>
  3. #include "../test_helpers.hxx"
  4. namespace
  5. {
  6. void test_transactor_newstyle_executes_simple_query()
  7. {
  8. pqxx::connection conn;
  9. auto const r{pqxx::perform([&conn] {
  10. return pqxx::work{conn}.exec("SELECT generate_series(1, 4)");
  11. })};
  12. PQXX_CHECK_EQUAL(std::size(r), 4, "Unexpected result size.");
  13. PQXX_CHECK_EQUAL(r.columns(), 1, "Unexpected number of columns.");
  14. PQXX_CHECK_EQUAL(r[0][0].as<int>(), 1, "Unexpected first row.");
  15. PQXX_CHECK_EQUAL(r[3][0].as<int>(), 4, "Unexpected last row.");
  16. }
  17. void test_transactor_newstyle_can_return_void()
  18. {
  19. bool done{false};
  20. pqxx::perform([&done]() noexcept { done = true; });
  21. PQXX_CHECK(done, "Callback was not executed.");
  22. }
  23. void test_transactor_newstyle_completes_upon_success()
  24. {
  25. int attempts{0};
  26. pqxx::perform([&attempts]() noexcept { attempts++; });
  27. PQXX_CHECK_EQUAL(attempts, 1, "Successful transactor didn't run 1 time.");
  28. }
  29. void test_transactor_newstyle_retries_broken_connection()
  30. {
  31. int counter{0};
  32. auto const &callback{[&counter] {
  33. ++counter;
  34. if (counter == 1)
  35. throw pqxx::broken_connection();
  36. return counter;
  37. }};
  38. int const result{pqxx::perform(callback)};
  39. PQXX_CHECK_EQUAL(result, 2, "Transactor run returned wrong result.");
  40. PQXX_CHECK_EQUAL(counter, result, "Number of retries does not match.");
  41. }
  42. void test_transactor_newstyle_retries_rollback()
  43. {
  44. int counter{0};
  45. auto const &callback{[&counter] {
  46. ++counter;
  47. if (counter == 1)
  48. throw pqxx::transaction_rollback("Simulated error");
  49. return counter;
  50. }};
  51. int const result{pqxx::perform(callback)};
  52. PQXX_CHECK_EQUAL(result, 2, "Transactor run returned wrong result.");
  53. PQXX_CHECK_EQUAL(counter, result, "Number of retries does not match.");
  54. }
  55. void test_transactor_newstyle_does_not_retry_in_doubt_error()
  56. {
  57. int counter{0};
  58. auto const &callback{[&counter] {
  59. ++counter;
  60. throw pqxx::in_doubt_error("Simulated error");
  61. }};
  62. PQXX_CHECK_THROWS(
  63. pqxx::perform(callback), pqxx::in_doubt_error,
  64. "Transactor did not propagate in_doubt_error.");
  65. PQXX_CHECK_EQUAL(counter, 1, "Transactor retried after in_doubt_error.");
  66. }
  67. void test_transactor_newstyle_does_not_retry_other_error()
  68. {
  69. int counter{0};
  70. auto const &callback{[&counter] {
  71. ++counter;
  72. throw std::runtime_error("Simulated error");
  73. }};
  74. PQXX_CHECK_THROWS(
  75. pqxx::perform(callback), std::runtime_error,
  76. "Transactor did not propagate std exception.");
  77. PQXX_CHECK_EQUAL(counter, 1, "Transactor retried after std exception.");
  78. }
  79. void test_transactor_newstyle_repeats_up_to_given_number_of_attempts()
  80. {
  81. int const attempts{5};
  82. int counter{0};
  83. auto const &callback{[&counter] {
  84. ++counter;
  85. throw pqxx::transaction_rollback("Simulated error");
  86. }};
  87. PQXX_CHECK_THROWS(
  88. pqxx::perform(callback, attempts), pqxx::transaction_rollback,
  89. "Not propagating original exception.");
  90. PQXX_CHECK_EQUAL(counter, attempts, "Number of retries does not match.");
  91. }
  92. void test_transactor()
  93. {
  94. test_transactor_newstyle_executes_simple_query();
  95. test_transactor_newstyle_can_return_void();
  96. test_transactor_newstyle_completes_upon_success();
  97. test_transactor_newstyle_retries_broken_connection();
  98. test_transactor_newstyle_retries_rollback();
  99. test_transactor_newstyle_does_not_retry_in_doubt_error();
  100. test_transactor_newstyle_does_not_retry_other_error();
  101. test_transactor_newstyle_repeats_up_to_given_number_of_attempts();
  102. }
  103. PQXX_REGISTER_TEST(test_transactor);
  104. } // namespace