MultiFileAllocationIteratorTest.cc 8.2 KB

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