From baaeac780983d51ce03c94e0fc92cfdc25aa9221 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 11 Apr 2015 18:44:43 +0000 Subject: [PATCH] extend some unit tests. primarily test_alert_manager and fixed a bug in alert_manager --- include/libtorrent/alert_manager.hpp | 9 -- include/libtorrent/extensions.hpp | 4 +- src/alert_manager.cpp | 4 + test/test_alert_manager.cpp | 123 ++++++++++++++++++++++++++- test/test_bdecode.cpp | 24 +++++- test/test_storage.cpp | 4 +- 6 files changed, 150 insertions(+), 18 deletions(-) diff --git a/include/libtorrent/alert_manager.hpp b/include/libtorrent/alert_manager.hpp index a6481f75c..033eda39c 100644 --- a/include/libtorrent/alert_manager.hpp +++ b/include/libtorrent/alert_manager.hpp @@ -113,15 +113,6 @@ namespace libtorrent { return (m_alert_mask & T::static_category) != 0; } - bool should_post(alert const* a) const - { - mutex::scoped_lock lock(m_mutex); - boost::uint32_t mask = m_alert_mask; - lock.unlock(); - - return (mask & a->category()) != 0; - } - alert* wait_for_alert(time_duration max_wait); void set_alert_mask(boost::uint32_t m) diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 6063ead40..089e56e8e 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -222,9 +222,7 @@ namespace libtorrent // called when plugin is added to a session virtual void added(aux::session_impl*) {} - // called when an alert is posted - // alerts that are filtered are not - // posted + // called when an alert is posted alerts that are filtered are not posted virtual void on_alert(alert const*) {} // return true if the add_torrent_params should be added diff --git a/src/alert_manager.cpp b/src/alert_manager.cpp index 43606fe1a..85ce2dfed 100644 --- a/src/alert_manager.cpp +++ b/src/alert_manager.cpp @@ -80,6 +80,10 @@ namespace libtorrent (*i)->on_alert(a); } #endif + if (a->type() == save_resume_data_failed_alert::alert_type + || a->type() == save_resume_data_failed_alert::alert_type) + ++m_num_queued_resume; + if (m_alerts[m_generation].size() == 1) { lock.unlock(); diff --git a/test/test_alert_manager.cpp b/test/test_alert_manager.cpp index 9906e03af..ef82e6171 100644 --- a/test/test_alert_manager.cpp +++ b/test/test_alert_manager.cpp @@ -34,8 +34,11 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alert_manager.hpp" #include "libtorrent/torrent_handle.hpp" #include "libtorrent/alert_types.hpp" +#include "libtorrent/extensions.hpp" #include +#include +#include using namespace libtorrent; @@ -61,6 +64,19 @@ void test_limit() TEST_EQUAL(alerts.size(), 500); TEST_EQUAL(mgr.pending(), false); + + // now, try lowering the limit and do the same thing again + mgr.set_alert_queue_size_limit(200); + + for (int i = 0; i < 600; ++i) + mgr.emplace_alert(torrent_handle()); + + TEST_EQUAL(mgr.pending(), true); + + mgr.get_all(alerts, num_resume); + + // even though we posted 600, the limit was 200 + TEST_EQUAL(alerts.size(), 200); } void test_priority_limit() @@ -167,16 +183,115 @@ void test_notify_function() TEST_EQUAL(cnt, 2); } +#ifndef TORRENT_DISABLE_EXTENSIONS +int plugin_alerts[3] = { 0, 0, 0 }; + +struct test_plugin : libtorrent::plugin +{ + test_plugin(int index) : m_index(index) {} + virtual void on_alert(alert const* a) + { + ++plugin_alerts[m_index]; + } + int m_index; +}; + +#endif + +void test_extensions() +{ +#ifndef TORRENT_DISABLE_EXTENSIONS + alert_manager mgr(100, 0xffffffff); + + mgr.add_extension(boost::make_shared(0)); + mgr.add_extension(boost::make_shared(1)); + mgr.add_extension(boost::make_shared(2)); + + for (int i = 0; i < 53; ++i) + mgr.emplace_alert(torrent_handle()); + + TEST_EQUAL(plugin_alerts[0], 53); + TEST_EQUAL(plugin_alerts[1], 53); + TEST_EQUAL(plugin_alerts[2], 53); + + for (int i = 0; i < 17; ++i) + mgr.emplace_alert(torrent_handle()); + + TEST_EQUAL(plugin_alerts[0], 70); + TEST_EQUAL(plugin_alerts[1], 70); + TEST_EQUAL(plugin_alerts[2], 70); +#endif +} + +void test_wait_for_alert() +{ + alert_manager mgr(100, 0xffffffff); + + time_point start = clock_type::now(); + + alert* a = mgr.wait_for_alert(seconds(1)); + + time_point end = clock_type::now(); + TEST_EQUAL(a, NULL); + TEST_CHECK(end - start > milliseconds(900)); + TEST_CHECK(end - start < milliseconds(1100)); + + mgr.emplace_alert(torrent_handle()); + + start = clock_type::now(); + a = mgr.wait_for_alert(seconds(1)); + end = clock_type::now(); + TEST_CHECK(end - start < milliseconds(1)); + TEST_CHECK(a->type() == torrent_added_alert::alert_type); +} + +void test_queued_resume() +{ + alert_manager mgr(100, 0xffffffff); + + for (int i = 0; i < 17; ++i) + mgr.emplace_alert(torrent_handle()); + + std::vector alerts; + int num_resume = 0; + mgr.get_all(alerts, num_resume); + TEST_EQUAL(num_resume, 0); + TEST_EQUAL(alerts.size(), 17); + + error_code ec(boost::system::errc::no_such_file_or_directory + , generic_category()); + + for (int i = 0; i < 2; ++i) + mgr.emplace_alert(torrent_handle(), ec); + + mgr.get_all(alerts, num_resume); + TEST_EQUAL(num_resume, 2); + TEST_EQUAL(alerts.size(), 2); +} + +void test_alert_mask() +{ + alert_manager mgr(100, 0xffffffff); + + TEST_CHECK(mgr.should_post()); + TEST_CHECK(mgr.should_post()); + + mgr.set_alert_mask(0); + + TEST_CHECK(!mgr.should_post()); + TEST_CHECK(!mgr.should_post()); +} + int test_main() { test_limit(); test_priority_limit(); test_dispatch_function(); test_notify_function(); - - // TODO: test wait_for_alert - // TODO: test num_queued_resume - // TODO: test alert_mask + test_extensions(); + test_wait_for_alert(); + test_queued_resume(); + test_alert_mask(); return 0; } diff --git a/test/test_bdecode.cpp b/test/test_bdecode.cpp index d38e572db..dd4b6f7c0 100644 --- a/test/test_bdecode.cpp +++ b/test/test_bdecode.cpp @@ -1162,7 +1162,29 @@ int test_main() TEST_EQUAL(print_entry(e), "{ 'a': 1, 'b': 'foo', 'c': [ 1 ] }"); } - // TODO: test switch_underlying_buffer + // test switch_underlying_buffer + { + char b1[] = "d1:ai1e1:b3:foo1:cli1e-i2ee1:dd1:xi1eee"; + char b2[] = "d1:ai1e1:b3:foo1:cli1e-i2ee1:dd1:xi1eee"; + + bdecode_node e; + error_code ec; + int pos; + int ret = bdecode(b1, b1 + sizeof(b1)-1, e, ec, &pos); + TEST_EQUAL(ret, -1); + TEST_EQUAL(pos, 22); + TEST_EQUAL(e.type(), bdecode_node::dict_t); + + std::string string1 = print_entry(e); + printf("%s\n", string1.c_str()); + + e.switch_underlying_buffer(b2); + + std::string string2 = print_entry(e); + printf("%s\n", string2.c_str()); + + TEST_EQUAL(string1, string2); + } return 0; } diff --git a/test/test_storage.cpp b/test/test_storage.cpp index bc7330402..411ac8779 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -532,7 +532,9 @@ void run_test(std::string const& test_path, bool unbuffered) TEST_EQUAL(file_size(combine_path(base, "test5.tmp")), 3253); TEST_EQUAL(file_size(combine_path(base, "test6.tmp")), 841); - printf("file: %d expected: %d last_file_size: %d, piece_size: %d\n", int(file_size(combine_path(base, "test7.tmp"))), int(last_file_size - piece_size), last_file_size, piece_size); + printf("file: %d expected: %d last_file_size: %d, piece_size: %d\n" + , int(file_size(combine_path(base, "test7.tmp"))) + , int(last_file_size - piece_size), last_file_size, piece_size); TEST_EQUAL(file_size(combine_path(base, "test7.tmp")), last_file_size - piece_size); remove_all(combine_path(test_path, "temp_storage"), ec); if (ec && ec != boost::system::errc::no_such_file_or_directory)