2007-10-11 16:58:24 +00:00
|
|
|
#include "MultiFileAllocationIterator.h"
|
2009-03-07 03:10:53 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "File.h"
|
|
|
|
#include "MultiDiskAdaptor.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
|
|
|
#include "Exception.h"
|
2008-06-15 16:19:06 +00:00
|
|
|
#include "array_fun.h"
|
2008-09-07 11:37:15 +00:00
|
|
|
#include "TestUtil.h"
|
|
|
|
#include "DiskWriter.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
class MultiFileAllocationIteratorTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(MultiFileAllocationIteratorTest);
|
|
|
|
CPPUNIT_TEST(testAllocate);
|
2007-11-28 14:22:28 +00:00
|
|
|
CPPUNIT_TEST(testMakeDiskWriterEntries);
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {}
|
|
|
|
|
|
|
|
void testAllocate();
|
2007-11-28 14:22:28 +00:00
|
|
|
void testMakeDiskWriterEntries();
|
2007-10-11 16:58:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( MultiFileAllocationIteratorTest );
|
|
|
|
|
2007-11-28 14:22:28 +00:00
|
|
|
void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries()
|
2007-11-21 16:14:40 +00:00
|
|
|
{
|
2009-03-09 15:10:26 +00:00
|
|
|
std::string storeDir = "/tmp/aria2_MultiFileAllocationIteratorTest"
|
|
|
|
"_testMakeDiskWriterEntries";
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<FileEntry> fs[] = {
|
2009-03-09 15:10:26 +00:00
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file1", 1536, 0)),
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file2", 2048, 1536)),// req no
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file3", 1024, 3584)),
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file4", 1024, 4608)),// req no
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file5", 1024, 5632)),// req no
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file6", 1024, 6656)),// req no
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file7", 256, 7680)),// req no
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file8", 255, 7936)),
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file9", 1025, 8191)),// req no
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/fileA", 1024, 9216)),// req no
|
|
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/fileB", 1024, 10240)),
|
2007-11-21 16:14:40 +00:00
|
|
|
};
|
2008-06-15 16:19:06 +00:00
|
|
|
fs[1]->setRequested(false); // file2
|
|
|
|
fs[3]->setRequested(false); // file4
|
|
|
|
fs[4]->setRequested(false); // file5
|
|
|
|
fs[5]->setRequested(false); // file6
|
|
|
|
fs[6]->setRequested(false); // file7
|
|
|
|
fs[8]->setRequested(false); // file9
|
|
|
|
fs[9]->setRequested(false); // fileA
|
2008-09-07 11:37:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// create empty file4
|
2009-03-07 03:10:53 +00:00
|
|
|
createFile(storeDir+std::string("/file4"), 0);
|
2007-11-21 16:14:40 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
|
2009-06-28 10:37:15 +00:00
|
|
|
diskAdaptor->setFileEntries(&fs[0], &fs[arrayLength(fs)]);
|
2007-11-21 16:14:40 +00:00
|
|
|
diskAdaptor->setPieceLength(1024);
|
2007-11-28 14:22:28 +00:00
|
|
|
diskAdaptor->openFile();
|
2007-11-21 16:14:40 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MultiFileAllocationIterator> itr
|
2008-09-07 11:37:15 +00:00
|
|
|
(dynamic_pointer_cast<MultiFileAllocationIterator>
|
|
|
|
(diskAdaptor->fileAllocationIterator()));
|
2007-11-21 16:14:40 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
const std::deque<SharedHandle<DiskWriterEntry> >& entries =
|
|
|
|
itr->getDiskWriterEntries();
|
2007-11-21 16:14:40 +00:00
|
|
|
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)11, entries.size());
|
|
|
|
|
|
|
|
// file1
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file1"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[0]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(entries[0]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull());
|
|
|
|
// file2
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file2"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[1]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(entries[1]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(!entries[1]->getDiskWriter().isNull());
|
|
|
|
// file3
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file3"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[2]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(entries[2]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(!entries[2]->getDiskWriter().isNull());
|
|
|
|
// file4, diskWriter is not null, because file exists.
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file4"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[3]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(!entries[3]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(!entries[3]->getDiskWriter().isNull());
|
|
|
|
// file5
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file5"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[4]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(!entries[4]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(entries[4]->getDiskWriter().isNull());
|
|
|
|
// file6
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file6"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[5]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(entries[5]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(!entries[5]->getDiskWriter().isNull());
|
|
|
|
// file7
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file7"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[6]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(entries[6]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(!entries[6]->getDiskWriter().isNull());
|
|
|
|
// file8
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file8"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[7]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(entries[7]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(!entries[7]->getDiskWriter().isNull());
|
|
|
|
// file9
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file9"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[8]->getFilePath());
|
2009-02-11 07:41:15 +00:00
|
|
|
CPPUNIT_ASSERT(!entries[8]->needsFileAllocation());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(!entries[8]->getDiskWriter().isNull());
|
|
|
|
// fileA
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/fileA"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[9]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(!entries[9]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(entries[9]->getDiskWriter().isNull());
|
|
|
|
// fileB
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/fileB"),
|
2009-03-09 15:10:26 +00:00
|
|
|
entries[10]->getFilePath());
|
2008-09-07 11:37:15 +00:00
|
|
|
CPPUNIT_ASSERT(entries[10]->needsFileAllocation());
|
|
|
|
CPPUNIT_ASSERT(!entries[10]->getDiskWriter().isNull());
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void MultiFileAllocationIteratorTest::testAllocate()
|
|
|
|
{
|
2009-03-07 03:10:53 +00:00
|
|
|
std::string storeDir =
|
|
|
|
"/tmp/aria2_MultiFileAllocationIteratorTest_testAllocate";
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string fname1 = "file1";
|
|
|
|
std::string fname2 = "file2";
|
|
|
|
std::string fname3 = "file3";
|
|
|
|
std::string fname4 = "file4";
|
|
|
|
std::string fname5 = "file5";
|
|
|
|
std::string fname6 = "file6";
|
2007-10-11 16:58:24 +00:00
|
|
|
int64_t length1 = 32769;
|
|
|
|
int64_t length2 = 0;
|
|
|
|
int64_t length3 = 8;
|
|
|
|
int64_t length4 = 10;
|
|
|
|
int64_t length5 = 20;
|
|
|
|
int64_t length6 = 30;
|
|
|
|
|
|
|
|
try {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
|
2008-09-07 11:37:15 +00:00
|
|
|
diskAdaptor->setPieceLength(1);
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
int64_t offset = 0;
|
2009-03-09 15:10:26 +00:00
|
|
|
SharedHandle<FileEntry> fileEntry1(new FileEntry(storeDir+"/"+fname1,
|
2008-04-20 00:50:22 +00:00
|
|
|
length1,
|
|
|
|
offset));
|
2008-02-08 15:53:45 +00:00
|
|
|
offset += length1;
|
2009-03-09 15:10:26 +00:00
|
|
|
SharedHandle<FileEntry> fileEntry2(new FileEntry(storeDir+"/"+fname2,
|
2008-04-20 00:50:22 +00:00
|
|
|
length2,
|
|
|
|
offset));
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
offset += length2;
|
2009-03-09 15:10:26 +00:00
|
|
|
SharedHandle<FileEntry> fileEntry3(new FileEntry(storeDir+"/"+fname3,
|
2008-04-20 00:50:22 +00:00
|
|
|
length3,
|
|
|
|
offset));
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
offset += length3;
|
2009-03-09 15:10:26 +00:00
|
|
|
SharedHandle<FileEntry> fileEntry4(new FileEntry(storeDir+"/"+fname4,
|
2008-04-20 00:50:22 +00:00
|
|
|
length4,
|
|
|
|
offset));
|
2008-02-08 15:53:45 +00:00
|
|
|
fileEntry4->setRequested(false);
|
|
|
|
|
|
|
|
offset += length4;
|
2009-03-09 15:10:26 +00:00
|
|
|
SharedHandle<FileEntry> fileEntry5(new FileEntry(storeDir+"/"+fname5,
|
2008-04-20 00:50:22 +00:00
|
|
|
length5,
|
|
|
|
offset));
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
offset += length5;
|
2009-03-09 15:10:26 +00:00
|
|
|
SharedHandle<FileEntry> fileEntry6(new FileEntry(storeDir+"/"+fname6,
|
2008-04-20 00:50:22 +00:00
|
|
|
length6,
|
|
|
|
offset));
|
2008-02-08 15:53:45 +00:00
|
|
|
fileEntry6->setRequested(false);
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
std::vector<SharedHandle<FileEntry> > fs;
|
2008-02-08 15:53:45 +00:00
|
|
|
fs.push_back(fileEntry1);
|
|
|
|
fs.push_back(fileEntry2);
|
|
|
|
fs.push_back(fileEntry3);
|
|
|
|
fs.push_back(fileEntry4);
|
|
|
|
fs.push_back(fileEntry5);
|
|
|
|
fs.push_back(fileEntry6);
|
2009-06-28 10:37:15 +00:00
|
|
|
diskAdaptor->setFileEntries(fs.begin(), fs.end());
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
for(std::vector<SharedHandle<FileEntry> >::const_iterator i = fs.begin();
|
2009-03-09 15:10:26 +00:00
|
|
|
i != fs.end(); ++i) {
|
|
|
|
File((*i)->getPath()).remove();
|
|
|
|
}
|
2008-09-07 11:37:15 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
// we have to open file first.
|
|
|
|
diskAdaptor->initAndOpenFile();
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MultiFileAllocationIterator> itr
|
2008-09-07 11:37:15 +00:00
|
|
|
(dynamic_pointer_cast<MultiFileAllocationIterator>
|
|
|
|
(diskAdaptor->fileAllocationIterator()));
|
2008-02-08 15:53:45 +00:00
|
|
|
while(!itr->finished()) {
|
|
|
|
itr->allocateChunk();
|
|
|
|
}
|
2009-03-09 15:10:26 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)length1, File(fileEntry1->getPath()).size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)length2, File(fileEntry2->getPath()).size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)length3, File(fileEntry3->getPath()).size());
|
|
|
|
CPPUNIT_ASSERT(!File(fileEntry4->getPath()).isFile());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)length5, File(fileEntry5->getPath()).size());
|
|
|
|
CPPUNIT_ASSERT(!File(fileEntry6->getPath()).isFile());
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
CPPUNIT_FAIL(e.stackTrace());
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|