File indexing completed on 2024-11-24 04:31:13

0001 /*
0002     SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QLocale>
0008 #include <QtTest>
0009 #include <diskio/multifilecache.h>
0010 #include <diskio/preallocationthread.h>
0011 #include <diskio/singlefilecache.h>
0012 #include <testlib/dummytorrentcreator.h>
0013 #include <testlib/utils.h>
0014 #include <torrent/torrent.h>
0015 #include <util/error.h>
0016 #include <util/fileops.h>
0017 #include <util/functions.h>
0018 #include <util/log.h>
0019 
0020 const bt::Uint64 TEST_FILE_SIZE = 15 * 1024 * 1024;
0021 
0022 using namespace bt;
0023 
0024 class PreallocationTest : public QObject
0025 {
0026     Q_OBJECT
0027 
0028 private Q_SLOTS:
0029     void initTestCase()
0030     {
0031         QLocale::setDefault(QLocale("main"));
0032         bt::InitLibKTorrent();
0033         bt::InitLog("preallocationtest.log", false, true);
0034         QMap<QString, bt::Uint64> files;
0035 
0036         files["aaa.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0037         files["bbb.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0038         files["ccc.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0039 
0040         try {
0041             QVERIFY(multi_creator.createMultiFileTorrent(files, "movies"));
0042             Out(SYS_GEN | LOG_DEBUG) << "Created " << multi_creator.torrentPath() << endl;
0043             multi_tor.load(bt::LoadFile(multi_creator.torrentPath()), false);
0044 
0045             // Truncate the files so we can preallocate them again
0046             for (QMap<QString, bt::Uint64>::const_iterator i = files.cbegin(); i != files.cend(); i++) {
0047                 bt::TruncateFile(multi_creator.dataPath() + i.key(), 0);
0048             }
0049         } catch (bt::Error &err) {
0050             Out(SYS_GEN | LOG_DEBUG) << "Failed to load torrent: " << multi_creator.torrentPath() << endl;
0051             QFAIL("Torrent load failure");
0052         }
0053 
0054         try {
0055             QVERIFY(single_creator.createSingleFileTorrent(RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE), "bla.avi"));
0056             Out(SYS_GEN | LOG_DEBUG) << "Created " << single_creator.torrentPath() << endl;
0057             single_tor.load(bt::LoadFile(single_creator.torrentPath()), false);
0058 
0059             // Truncate the file so we can preallocate them again
0060             bt::TruncateFile(single_creator.dataPath(), 0);
0061         } catch (bt::Error &err) {
0062             Out(SYS_GEN | LOG_DEBUG) << "Failed to load torrent: " << single_creator.torrentPath() << endl;
0063             QFAIL("Torrent load failure");
0064         }
0065     }
0066 
0067     void cleanupTestCase()
0068     {
0069     }
0070 
0071     void testPreallocationMultiFileCache()
0072     {
0073         bt::MultiFileCache cache(multi_tor, multi_creator.tempPath(), multi_creator.dataPath(), true);
0074         cache.loadFileMap();
0075         cache.setPreallocateFully(true);
0076         cache.open();
0077 
0078         PreallocationThread prealloc;
0079         cache.preparePreallocation(&prealloc);
0080         prealloc.run();
0081 
0082         if (!prealloc.errorMessage().isEmpty())
0083             Out(SYS_GEN | LOG_DEBUG) << "Preallocation failed: " << prealloc.errorMessage() << endl;
0084 
0085         Out(SYS_GEN | LOG_DEBUG) << "bw: " << prealloc.bytesWritten() << ", ts: " << multi_tor.getTotalSize() << endl;
0086         QVERIFY(prealloc.errorHappened() == false);
0087         QVERIFY(prealloc.bytesWritten() == multi_tor.getTotalSize());
0088 
0089         for (bt::Uint32 i = 0; i < multi_tor.getNumFiles(); i++) {
0090             QVERIFY(bt::FileSize(multi_tor.getFile(i).getPathOnDisk()) == multi_tor.getFile(i).getSize());
0091         }
0092     }
0093 
0094     void testPreallocationSingleFileCache()
0095     {
0096         QFileInfo info(single_creator.dataPath());
0097         bt::SingleFileCache cache(single_tor, single_creator.tempPath(), info.absoluteDir().absolutePath() + bt::DirSeparator());
0098         cache.loadFileMap();
0099         cache.setPreallocateFully(true);
0100         cache.open();
0101 
0102         PreallocationThread prealloc;
0103         cache.preparePreallocation(&prealloc);
0104         prealloc.run();
0105 
0106         if (!prealloc.errorMessage().isEmpty())
0107             Out(SYS_GEN | LOG_DEBUG) << "Preallocation failed: " << prealloc.errorMessage() << endl;
0108 
0109         Out(SYS_GEN | LOG_DEBUG) << "bw: " << prealloc.bytesWritten() << ", ts: " << single_tor.getTotalSize() << endl;
0110         QVERIFY(prealloc.errorHappened() == false);
0111         QVERIFY(prealloc.bytesWritten() == single_tor.getTotalSize());
0112         QVERIFY(bt::FileSize(single_creator.dataPath()) == single_tor.getTotalSize());
0113     }
0114 
0115 private:
0116     DummyTorrentCreator multi_creator;
0117     bt::Torrent multi_tor;
0118     DummyTorrentCreator single_creator;
0119     bt::Torrent single_tor;
0120 };
0121 
0122 QTEST_MAIN(PreallocationTest)
0123 
0124 #include "preallocationtest.moc"