MultiFileAllocationIteratorTest.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "MultiFileAllocationIterator.h"
  2. #include "File.h"
  3. #include "MultiDiskAdaptor.h"
  4. #include "FileEntry.h"
  5. #include "Exception.h"
  6. #include "array_fun.h"
  7. #include "TestUtil.h"
  8. #include "DiskWriter.h"
  9. #include <algorithm>
  10. #include <iostream>
  11. #include <cppunit/extensions/HelperMacros.h>
  12. namespace aria2 {
  13. class MultiFileAllocationIteratorTest:public CppUnit::TestFixture {
  14. CPPUNIT_TEST_SUITE(MultiFileAllocationIteratorTest);
  15. CPPUNIT_TEST(testAllocate);
  16. CPPUNIT_TEST(testMakeDiskWriterEntries);
  17. CPPUNIT_TEST_SUITE_END();
  18. private:
  19. public:
  20. void setUp() {}
  21. void testAllocate();
  22. void testMakeDiskWriterEntries();
  23. };
  24. CPPUNIT_TEST_SUITE_REGISTRATION( MultiFileAllocationIteratorTest );
  25. void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries()
  26. {
  27. SharedHandle<FileEntry> fs[] = {
  28. SharedHandle<FileEntry>(new FileEntry("file1", 1536, 0)),
  29. SharedHandle<FileEntry>(new FileEntry("file2", 2048, 1536)),// req no
  30. SharedHandle<FileEntry>(new FileEntry("file3", 1024, 3584)),
  31. SharedHandle<FileEntry>(new FileEntry("file4", 1024, 4608)),// req no
  32. SharedHandle<FileEntry>(new FileEntry("file5", 1024, 5632)),// req no
  33. SharedHandle<FileEntry>(new FileEntry("file6", 1024, 6656)),// req no
  34. SharedHandle<FileEntry>(new FileEntry("file7", 256, 7680)),// req no
  35. SharedHandle<FileEntry>(new FileEntry("file8", 255, 7936)),
  36. SharedHandle<FileEntry>(new FileEntry("file9", 1025, 8191)),// req no
  37. SharedHandle<FileEntry>(new FileEntry("fileA", 1024, 9216)),// req no
  38. SharedHandle<FileEntry>(new FileEntry("fileB", 1024, 10240)),
  39. };
  40. fs[1]->setRequested(false); // file2
  41. fs[3]->setRequested(false); // file4
  42. fs[4]->setRequested(false); // file5
  43. fs[5]->setRequested(false); // file6
  44. fs[6]->setRequested(false); // file7
  45. fs[8]->setRequested(false); // file9
  46. fs[9]->setRequested(false); // fileA
  47. std::string topDir = "top";
  48. std::string storeDir = "/tmp/aria2_MultiFileAllocationIteratorTest"
  49. "_testMakeDiskWriterEntries";
  50. std::string prefix = storeDir+"/"+topDir;
  51. // create empty file4
  52. createFile(prefix+std::string("/file4"), 0);
  53. SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
  54. diskAdaptor->setFileEntries
  55. (std::deque<SharedHandle<FileEntry> >(&fs[0], &fs[arrayLength(fs)]));
  56. diskAdaptor->setPieceLength(1024);
  57. diskAdaptor->setStoreDir(storeDir);
  58. diskAdaptor->setTopDir(topDir);
  59. diskAdaptor->openFile();
  60. SharedHandle<MultiFileAllocationIterator> itr
  61. (dynamic_pointer_cast<MultiFileAllocationIterator>
  62. (diskAdaptor->fileAllocationIterator()));
  63. DiskWriterEntries entries = itr->getDiskWriterEntries();
  64. CPPUNIT_ASSERT_EQUAL((size_t)11, entries.size());
  65. // file1
  66. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/file1"),
  67. entries[0]->getFilePath(prefix));
  68. CPPUNIT_ASSERT(entries[0]->needsFileAllocation());
  69. CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull());
  70. // file2
  71. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/file2"),
  72. entries[1]->getFilePath(prefix));
  73. CPPUNIT_ASSERT(entries[1]->needsFileAllocation());
  74. CPPUNIT_ASSERT(!entries[1]->getDiskWriter().isNull());
  75. // file3
  76. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/file3"),
  77. entries[2]->getFilePath(prefix));
  78. CPPUNIT_ASSERT(entries[2]->needsFileAllocation());
  79. CPPUNIT_ASSERT(!entries[2]->getDiskWriter().isNull());
  80. // file4, diskWriter is not null, because file exists.
  81. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/file4"),
  82. entries[3]->getFilePath(prefix));
  83. CPPUNIT_ASSERT(!entries[3]->needsFileAllocation());
  84. CPPUNIT_ASSERT(!entries[3]->getDiskWriter().isNull());
  85. // file5
  86. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/file5"),
  87. entries[4]->getFilePath(prefix));
  88. CPPUNIT_ASSERT(!entries[4]->needsFileAllocation());
  89. CPPUNIT_ASSERT(entries[4]->getDiskWriter().isNull());
  90. // file6
  91. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/file6"),
  92. entries[5]->getFilePath(prefix));
  93. CPPUNIT_ASSERT(entries[5]->needsFileAllocation());
  94. CPPUNIT_ASSERT(!entries[5]->getDiskWriter().isNull());
  95. // file7
  96. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/file7"),
  97. entries[6]->getFilePath(prefix));
  98. CPPUNIT_ASSERT(entries[6]->needsFileAllocation());
  99. CPPUNIT_ASSERT(!entries[6]->getDiskWriter().isNull());
  100. // file8
  101. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/file8"),
  102. entries[7]->getFilePath(prefix));
  103. CPPUNIT_ASSERT(entries[7]->needsFileAllocation());
  104. CPPUNIT_ASSERT(!entries[7]->getDiskWriter().isNull());
  105. // file9
  106. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/file9"),
  107. entries[8]->getFilePath(prefix));
  108. CPPUNIT_ASSERT(entries[8]->needsFileAllocation());
  109. CPPUNIT_ASSERT(!entries[8]->getDiskWriter().isNull());
  110. // fileA
  111. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/fileA"),
  112. entries[9]->getFilePath(prefix));
  113. CPPUNIT_ASSERT(!entries[9]->needsFileAllocation());
  114. CPPUNIT_ASSERT(entries[9]->getDiskWriter().isNull());
  115. // fileB
  116. CPPUNIT_ASSERT_EQUAL(prefix+std::string("/fileB"),
  117. entries[10]->getFilePath(prefix));
  118. CPPUNIT_ASSERT(entries[10]->needsFileAllocation());
  119. CPPUNIT_ASSERT(!entries[10]->getDiskWriter().isNull());
  120. }
  121. void MultiFileAllocationIteratorTest::testAllocate()
  122. {
  123. std::string dir = "/tmp";
  124. std::string topDir = "aria2_MultiFileAllocationIteratorTest_testAllocate";
  125. std::string prefix = dir+"/"+topDir;
  126. std::string fname1 = "file1";
  127. std::string fname2 = "file2";
  128. std::string fname3 = "file3";
  129. std::string fname4 = "file4";
  130. std::string fname5 = "file5";
  131. std::string fname6 = "file6";
  132. int64_t length1 = 32769;
  133. int64_t length2 = 0;
  134. int64_t length3 = 8;
  135. int64_t length4 = 10;
  136. int64_t length5 = 20;
  137. int64_t length6 = 30;
  138. try {
  139. SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
  140. diskAdaptor->setStoreDir(dir);
  141. diskAdaptor->setTopDir(topDir);
  142. diskAdaptor->setPieceLength(1);
  143. int64_t offset = 0;
  144. SharedHandle<FileEntry> fileEntry1(new FileEntry(fname1,
  145. length1,
  146. offset));
  147. offset += length1;
  148. SharedHandle<FileEntry> fileEntry2(new FileEntry(fname2,
  149. length2,
  150. offset));
  151. offset += length2;
  152. SharedHandle<FileEntry> fileEntry3(new FileEntry(fname3,
  153. length3,
  154. offset));
  155. offset += length3;
  156. SharedHandle<FileEntry> fileEntry4(new FileEntry(fname4,
  157. length4,
  158. offset));
  159. fileEntry4->setRequested(false);
  160. offset += length4;
  161. SharedHandle<FileEntry> fileEntry5(new FileEntry(fname5,
  162. length5,
  163. offset));
  164. offset += length5;
  165. SharedHandle<FileEntry> fileEntry6(new FileEntry(fname6,
  166. length6,
  167. offset));
  168. fileEntry6->setRequested(false);
  169. std::deque<SharedHandle<FileEntry> > fs;
  170. fs.push_back(fileEntry1);
  171. fs.push_back(fileEntry2);
  172. fs.push_back(fileEntry3);
  173. fs.push_back(fileEntry4);
  174. fs.push_back(fileEntry5);
  175. fs.push_back(fileEntry6);
  176. diskAdaptor->setFileEntries(fs);
  177. File(prefix+"/"+fname1).remove();
  178. File(prefix+"/"+fname2).remove();
  179. File(prefix+"/"+fname3).remove();
  180. File(prefix+"/"+fname4).remove();
  181. File(prefix+"/"+fname5).remove();
  182. File(prefix+"/"+fname6).remove();
  183. // we have to open file first.
  184. diskAdaptor->initAndOpenFile();
  185. SharedHandle<MultiFileAllocationIterator> itr
  186. (dynamic_pointer_cast<MultiFileAllocationIterator>
  187. (diskAdaptor->fileAllocationIterator()));
  188. while(!itr->finished()) {
  189. itr->allocateChunk();
  190. }
  191. CPPUNIT_ASSERT_EQUAL((uint64_t)length1, File(prefix+"/"+fname1).size());
  192. CPPUNIT_ASSERT_EQUAL((uint64_t)length2, File(prefix+"/"+fname2).size());
  193. CPPUNIT_ASSERT_EQUAL((uint64_t)length3, File(prefix+"/"+fname3).size());
  194. CPPUNIT_ASSERT(!File(prefix+"/"+fname4).isFile());
  195. CPPUNIT_ASSERT_EQUAL((uint64_t)length5, File(prefix+"/"+fname5).size());
  196. CPPUNIT_ASSERT(!File(prefix+"/"+fname6).isFile());
  197. } catch(Exception& e) {
  198. CPPUNIT_FAIL(e.stackTrace());
  199. }
  200. }
  201. } // namespace aria2