add rename file test

This commit is contained in:
arvidn 2016-11-05 01:38:55 -04:00 committed by Arvid Norberg
parent 19a5d9f989
commit 200b3c4965
3 changed files with 80 additions and 9 deletions

View File

@ -151,7 +151,7 @@ std::map<std::string, boost::int64_t> get_counters(libtorrent::session& s)
return ret;
}
alert const* wait_for_alert(lt::session& ses, int type, char const* name)
alert const* wait_for_alert(lt::session& ses, int type, char const* name, int num)
{
time_point end = libtorrent::clock_type::now() + seconds(10);
while (true)
@ -169,12 +169,13 @@ alert const* wait_for_alert(lt::session& ses, int type, char const* name)
{
fprintf(stdout, "%s: %s: [%s] %s\n", time_now_string(), name
, (*i)->what(), (*i)->message().c_str());
if ((*i)->type() == type && !ret)
if ((*i)->type() == type)
{
ret = *i;
--num;
}
}
if (ret) return ret;
if (num == 0) return ret;
}
return NULL;
}

View File

@ -64,7 +64,7 @@ EXPORT libtorrent::sha1_hash rand_hash();
EXPORT std::map<std::string, boost::int64_t> get_counters(libtorrent::session& s);
EXPORT libtorrent::alert const* wait_for_alert(
libtorrent::session& ses, int type, char const* name = "");
libtorrent::session& ses, int type, char const* name = "", int num = 1);
EXPORT void print_ses_rate(float time
, libtorrent::torrent_status const* st1

View File

@ -116,11 +116,8 @@ void run_until(io_service& ios, bool const& done)
void nop() {}
boost::shared_ptr<default_storage> setup_torrent(file_storage& fs
, file_pool& fp
, std::vector<char>& buf
, std::string const& test_path
, aux::session_settings& set)
boost::shared_ptr<torrent_info> setup_torrent_info(file_storage& fs
, std::vector<char>& buf)
{
fs.add_file(combine_path("temp_storage", "test1.tmp"), 8);
fs.add_file(combine_path("temp_storage", combine_path("folder1", "test2.tmp")), 8);
@ -145,6 +142,17 @@ boost::shared_ptr<default_storage> setup_torrent(file_storage& fs
, ec.message().c_str());
}
return info;
}
boost::shared_ptr<default_storage> setup_torrent(file_storage& fs
, file_pool& fp
, std::vector<char>& buf
, std::string const& test_path
, aux::session_settings& set)
{
boost::shared_ptr<torrent_info> info = setup_torrent_info(fs, buf);
storage_params p;
p.files = &fs;
p.pool = &fp;
@ -731,6 +739,68 @@ bool got_file_rename_alert(alert const* a)
|| alert_cast<libtorrent::file_rename_failed_alert>(a);
}
TORRENT_TEST(rename_file)
{
std::vector<char> buf;
file_storage fs;
boost::shared_ptr<torrent_info> info = setup_torrent_info(fs, buf);
const int mask = alert::all_categories
& ~(alert::performance_warning
| alert::stats_notification);
settings_pack pack;
pack.set_bool(settings_pack::enable_lsd, false);
pack.set_bool(settings_pack::enable_natpmp, false);
pack.set_bool(settings_pack::enable_upnp, false);
pack.set_bool(settings_pack::enable_dht, false);
pack.set_int(settings_pack::alert_mask, mask);
pack.set_bool(settings_pack::disable_hash_checks, true);
lt::session ses(pack);
add_torrent_params p;
p.ti = info;
p.save_path = ".";
error_code ec;
torrent_handle h = ses.add_torrent(p, ec);
// make it a seed
std::vector<char> tmp(info->piece_length());
for (int i = 0; i < info->num_pieces(); ++i)
h.add_piece(i, &tmp[0]);
// wait for the files to have been written
alert const* pf = wait_for_alert(ses, piece_finished_alert::alert_type, "ses", info->num_pieces());
TEST_CHECK(pf);
// now rename them. This is the test
for (int i = 0; i < info->num_files(); ++i)
{
std::string name = fs.file_path(i);
h.rename_file(i, "__" + name);
}
// wait fir the files to have been renamed
alert const* fra = wait_for_alert(ses, file_renamed_alert::alert_type, "ses", info->num_files());
TEST_CHECK(fra);
TEST_CHECK(exists("__" + info->name()));
h.save_resume_data();
alert const* ra = wait_for_alert(ses, save_resume_data_alert::alert_type);
TEST_CHECK(ra);
if (!ra) return;
entry resume = *alert_cast<save_resume_data_alert>(ra)->resume_data;
std::cerr << resume.to_string() << "\n";
entry::list_type files = resume.dict().find("mapped_files")->second.list();
for (entry::list_type::iterator i = files.begin(); i != files.end(); ++i)
{
TEST_CHECK(i->string().substr(0, 2) == "__");
}
}
TORRENT_TEST(rename_file_fastresume)
{
std::string test_path = current_working_directory();