extend some unit tests. primarily test_alert_manager and fixed a bug in alert_manager

This commit is contained in:
Arvid Norberg 2015-04-11 18:44:43 +00:00
parent caf2b6ed07
commit baaeac7809
6 changed files with 150 additions and 18 deletions

View File

@ -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)

View File

@ -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

View File

@ -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();

View File

@ -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 <boost/bind.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
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_added_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<test_plugin>(0));
mgr.add_extension(boost::make_shared<test_plugin>(1));
mgr.add_extension(boost::make_shared<test_plugin>(2));
for (int i = 0; i < 53; ++i)
mgr.emplace_alert<torrent_added_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_added_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_added_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_added_alert>(torrent_handle());
std::vector<alert*> 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<save_resume_data_failed_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<torrent_added_alert>());
TEST_CHECK(mgr.should_post<torrent_paused_alert>());
mgr.set_alert_mask(0);
TEST_CHECK(!mgr.should_post<torrent_added_alert>());
TEST_CHECK(!mgr.should_post<torrent_paused_alert>());
}
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;
}

View File

@ -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;
}

View File

@ -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)