util_trace_tests.cpp raw

   1  // Copyright (c) 2023-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <test/util/setup_common.h>
   6  
   7  #include <boost/test/unit_test.hpp>
   8  
   9  #include <util/trace.h>
  10  
  11  TRACEPOINT_SEMAPHORE(test, zero_args);
  12  TRACEPOINT_SEMAPHORE(test, one_arg);
  13  TRACEPOINT_SEMAPHORE(test, six_args);
  14  TRACEPOINT_SEMAPHORE(test, twelve_args);
  15  TRACEPOINT_SEMAPHORE(test, check_if_attached);
  16  TRACEPOINT_SEMAPHORE(test, expensive_section);
  17  
  18  BOOST_FIXTURE_TEST_SUITE(util_trace_tests, BasicTestingSetup)
  19  
  20  // Tests the TRACEPOINT macro and that we can compile tracepoints with 0 to 12 args.
  21  BOOST_AUTO_TEST_CASE(test_tracepoints)
  22  {
  23      TRACEPOINT(test, zero_args);
  24      TRACEPOINT(test, one_arg, 1);
  25      TRACEPOINT(test, six_args, 1, 2, 3, 4, 5, 6);
  26      TRACEPOINT(test, twelve_args, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
  27      BOOST_CHECK(true);
  28  }
  29  
  30  int fail_test_if_executed()
  31  {
  32      BOOST_CHECK(false);
  33      return 0;
  34  }
  35  
  36  BOOST_AUTO_TEST_CASE(test_tracepoint_check_if_attached)
  37  {
  38      // TRACEPOINT should check if we are attaching to the tracepoint and only then
  39      // process arguments. This means, only if we are attached to the
  40      // `test:check_if_attached` tracepoint, fail_test_if_executed() is executed.
  41      // Since we don't attach to the tracepoint when running the test, it succeeds.
  42      TRACEPOINT(test, check_if_attached, fail_test_if_executed());
  43      BOOST_CHECK(true);
  44  }
  45  
  46  BOOST_AUTO_TEST_CASE(test_tracepoint_manual_tracepoint_active_check)
  47  {
  48      // We should be able to use the TRACEPOINT_ACTIVE() macro to only
  49      // execute an 'expensive' code section if we are attached to the
  50      // tracepoint.
  51      if (TRACEPOINT_ACTIVE(test, expensive_section)) {
  52          BOOST_CHECK(false); // expensive_function()
  53          TRACEPOINT(test, expensive_section);
  54      }
  55      BOOST_CHECK(true);
  56  }
  57  
  58  BOOST_AUTO_TEST_SUITE_END()
  59