Skip to content

Commit

Permalink
Add EXPECT_TIMELY test macro (#3940)
Browse files Browse the repository at this point in the history
* Add `EXPECT_TIMELY` test macro

* Compare error value != 0
  • Loading branch information
pwojcikdev authored Sep 5, 2022
1 parent a98449e commit cf25128
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
13 changes: 2 additions & 11 deletions nano/slow_test/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ nano::keypair setup_rep (nano::test::system & system, nano::node & node, nano::u

EXPECT_TRUE (nano::test::process (node, { send, open }));
EXPECT_TRUE (nano::test::confirm (node, { send, open }));
// TODO: Create `EXPECT_TIMELY` macro to remove this boilerplate
system.poll_until_true (3s, [&node, &send, &open] () {
return nano::test::confirmed (node, { send, open });
});
EXPECT_TRUE (nano::test::confirmed (node, { send, open }));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { send, open }));

return key;
}
Expand Down Expand Up @@ -106,12 +102,7 @@ std::vector<std::shared_ptr<nano::block>> setup_blocks (nano::test::system & sys

// Confirm whole genesis chain at once
EXPECT_TRUE (nano::test::confirm (node, { sends.back () }));

// TODO: Create `EXPECT_TIMELY` macro to remove this boilerplate
system.poll_until_true (60s, [&node, &sends] () {
return nano::test::confirmed (node, { sends });
});
EXPECT_TRUE (nano::test::confirmed (node, { sends }));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { sends }));

return receives;
}
Expand Down
27 changes: 24 additions & 3 deletions nano/test_common/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <nano/lib/locks.hpp>
#include <nano/lib/timer.hpp>

#include <gtest/gtest.h>

#include <boost/iostreams/concepts.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/setup/console.hpp>
Expand All @@ -29,11 +31,21 @@
GTEST_TEST_ERROR_CODE (!(condition), #condition, condition.message ().c_str (), "", \
GTEST_FATAL_FAILURE_)

/** Extends gtest with a std::error_code assert that prints the error code message when non-zero */
#define EXPECT_NO_ERROR(condition) \
GTEST_TEST_ERROR_CODE (!(condition), #condition, condition.message ().c_str (), "", \
GTEST_NONFATAL_FAILURE_)

/** Extends gtest with a std::error_code assert that expects an error */
#define ASSERT_IS_ERROR(condition) \
GTEST_TEST_ERROR_CODE ((condition.value () > 0), #condition, "An error was expected", "", \
#define ASSERT_IS_ERROR(condition) \
GTEST_TEST_ERROR_CODE ((condition.value () != 0), #condition, "An error was expected", "", \
GTEST_FATAL_FAILURE_)

/** Extends gtest with a std::error_code assert that expects an error */
#define EXPECT_IS_ERROR(condition) \
GTEST_TEST_ERROR_CODE ((condition.value () != 0), #condition, "An error was expected", "", \
GTEST_NONFATAL_FAILURE_)

/** Asserts that the condition becomes true within the deadline */
#define ASSERT_TIMELY(time, condition) \
system.deadline_set (time); \
Expand All @@ -42,6 +54,15 @@
ASSERT_NO_ERROR (system.poll ()); \
}

/** Expects that the condition becomes true within the deadline */
#define EXPECT_TIMELY(time, condition) \
system.deadline_set (time); \
std::error_code ec; \
while (!(condition) && !(ec = system.poll ())) \
{ \
} \
EXPECT_NO_ERROR (ec);

/*
* Waits specified number of time while keeping system running.
* Useful for asserting conditions that should still hold after some delay of time
Expand Down Expand Up @@ -295,4 +316,4 @@ namespace test
*/
std::vector<nano::block_hash> blocks_to_hashes (std::vector<std::shared_ptr<nano::block>> blocks);
}
}
}

0 comments on commit cf25128

Please sign in to comment.