MultiFileAllocationIteratorTest.cc 7.5 KB

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