diff --git a/src/torrent.cpp b/src/torrent.cpp index 98ecafb3f..a7d7136e3 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1168,7 +1168,8 @@ namespace libtorrent pause(); } - void torrent::on_disk_read_complete(int ret, disk_io_job const& j, peer_request r, read_piece_struct* rp) + void torrent::on_disk_read_complete(int ret, disk_io_job const& j + , peer_request r, read_piece_struct* rp) { TORRENT_ASSERT(m_ses.is_network_thread()); diff --git a/test/Jamfile b/test/Jamfile index a9ff18bf8..be73dee21 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -126,6 +126,7 @@ test-suite libtorrent : [ run test_torrent_parse.cpp ] [ run test_session.cpp ] [ run test_upnp.cpp ] + [ run test_read_piece.cpp ] [ run test_ssl.cpp ] [ run test_tracker.cpp ] diff --git a/test/Makefile.am b/test/Makefile.am index f9ab02b81..c7cb514dc 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,6 +1,7 @@ test_programs = \ test_file \ test_file_storage \ + test_privacy \ test_auto_unchoke \ test_bandwidth_limiter \ test_bdecode_performance \ @@ -25,12 +26,15 @@ test_programs = \ test_http_parser \ test_magnet \ test_packet_buffer \ + test_read_piece \ test_rss \ + test_ssl \ test_storage \ test_swarm \ test_threads \ test_torrent \ test_torrent_parse \ + test_tracker \ test_trackers_extension \ test_transfer \ test_upnp \ diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index b6d316cbe..69ec9d1c6 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -92,15 +92,20 @@ int print_failures() std::auto_ptr wait_for_alert(session& ses, int type, char const* name) { std::auto_ptr ret; + ptime end = time_now() + seconds(10); while (!ret.get()) { - ses.wait_for_alert(milliseconds(5000)); + ptime now = time_now(); + if (now > end) return std::auto_ptr(); + + ses.wait_for_alert(end - now); std::deque alerts; ses.pop_alerts(&alerts); for (std::deque::iterator i = alerts.begin() , end(alerts.end()); i != end; ++i) { - fprintf(stderr, "%s: %s: [%s] %s\n", time_now_string(), name, (*i)->what(), (*i)->message().c_str()); + fprintf(stderr, "%s: %s: [%s] %s\n", time_now_string(), name + , (*i)->what(), (*i)->message().c_str()); if (!ret.get() && (*i)->type() == type) { ret = std::auto_ptr(*i); diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index 6af5c4cfe..720cc5784 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -51,7 +51,8 @@ void EXPORT save_file(char const* filename, char const* data, int size); void EXPORT report_failure(char const* err, char const* file, int line); -std::auto_ptr EXPORT wait_for_alert(libtorrent::session& ses, int type, char const* name = ""); +std::auto_ptr EXPORT wait_for_alert( + libtorrent::session& ses, int type, char const* name = ""); void EXPORT print_ses_rate(float time , libtorrent::torrent_status const* st1 diff --git a/test/test_checking.cpp b/test/test_checking.cpp index 3a32d66a6..4031703b2 100644 --- a/test/test_checking.cpp +++ b/test/test_checking.cpp @@ -35,8 +35,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "setup_transfer.hpp" #include "libtorrent/create_torrent.hpp" #include // for chmod -#include - static const int file_sizes[] = { 5, 16 - 5, 16000, 17, 10, 8000, 8000, 1,1,1,1,1,100,1,1,1,1,100,1,1,1,1,1,1 ,1,1,1,1,1,1,13,65000,34,75,2,30,400,500,23000,900,43000,400,4300,6, 4}; diff --git a/test/test_read_piece.cpp b/test/test_read_piece.cpp new file mode 100644 index 000000000..2dbaf65a1 --- /dev/null +++ b/test/test_read_piece.cpp @@ -0,0 +1,140 @@ +/* + +Copyright (c) 2013, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "libtorrent/session.hpp" +#include "test.hpp" +#include "setup_transfer.hpp" +#include "libtorrent/create_torrent.hpp" +#include "libtorrent/alert_types.hpp" + +enum flags_t +{ + seed_mode = 1, + time_critical = 2 +}; + +void test_read_piece(int flags) +{ + using namespace libtorrent; + + fprintf(stderr, "==== TEST READ PIECE =====\n"); + + // in case the previous run was terminated + error_code ec; + remove_all("tmp1_read_piece", ec); + if (ec) fprintf(stderr, "ERROR: removing tmp1_read_piece: (%d) %s\n" + , ec.value(), ec.message().c_str()); + + create_directory("tmp1_read_piece", ec); + if (ec) fprintf(stderr, "ERROR: creating directory tmp1_read_piece: (%d) %s\n" + , ec.value(), ec.message().c_str()); + + create_directory(combine_path("tmp1_read_piece", "test_torrent"), ec); + if (ec) fprintf(stderr, "ERROR: creating directory test_torrent: (%d) %s\n" + , ec.value(), ec.message().c_str()); + + file_storage fs; + std::srand(10); + int piece_size = 0x4000; + + static const int file_sizes[] ={ 100000, 10000 }; + + create_random_files(combine_path("tmp1_read_piece", "test_torrent") + , file_sizes, 2); + + add_files(fs, combine_path("tmp1_read_piece", "test_torrent")); + libtorrent::create_torrent t(fs, piece_size, 0x4000); + + // calculate the hash for all pieces + set_piece_hashes(t, "tmp1_read_piece", ec); + if (ec) fprintf(stderr, "ERROR: set_piece_hashes: (%d) %s\n" + , ec.value(), ec.message().c_str()); + + std::vector buf; + bencode(std::back_inserter(buf), t.generate()); + boost::intrusive_ptr ti(new torrent_info(&buf[0], buf.size(), ec)); + + fprintf(stderr, "generated torrent: %s tmp1_read_piece/test_torrent\n" + , to_hex(ti->info_hash().to_string()).c_str()); + + session ses(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48000, 49000), "0.0.0.0", 0); + ses.set_alert_mask(alert::all_categories); + + add_torrent_params p; + p.save_path = "tmp1_read_piece"; + p.ti = ti; + if (flags & seed_mode) + p.flags |= add_torrent_params::flag_seed_mode; + torrent_handle tor1 = ses.add_torrent(p, ec); + TEST_CHECK(!ec); + TEST_CHECK(tor1.is_valid()); + + std::auto_ptr a = wait_for_alert(ses, torrent_finished_alert::alert_type, "ses"); + TEST_CHECK(a.get()); + + TEST_CHECK(tor1.status().is_seeding); + + if (flags & time_critical) + { + tor1.set_piece_deadline(1, 0, torrent_handle::alert_when_available); + } + else + { + tor1.read_piece(1); + } + + a = wait_for_alert(ses, read_piece_alert::alert_type, "ses"); + + TEST_CHECK(a.get()); + if (a.get()) + { + read_piece_alert* rp = alert_cast(a.get()); + TEST_CHECK(rp); + if (rp) + { + TEST_EQUAL(rp->piece, 1); + } + } + + remove_all("tmp1_read_piece", ec); + if (ec) fprintf(stderr, "ERROR: removing tmp1_read_piece: (%d) %s\n" + , ec.value(), ec.message().c_str()); +} + +int test_main() +{ + test_read_piece(0); + test_read_piece(seed_mode); + test_read_piece(time_critical); + return 0; +} +