MultiFileAllocationIteratorTest.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = "/tmp/aria2_MultiFileAllocationIteratorTest"
  28. "_testMakeDiskWriterEntries";
  29. SharedHandle<FileEntry> fs[] = {
  30. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file1", 1536, 0)),
  31. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file2", 2048, 1536)),// req no
  32. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file3", 1024, 3584)),
  33. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file4", 1024, 4608)),// req no
  34. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file5", 1024, 5632)),// req no
  35. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file6", 1024, 6656)),// req no
  36. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file7", 256, 7680)),// req no
  37. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file8", 255, 7936)),
  38. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file9", 1025, 8191)),// req no
  39. SharedHandle<FileEntry>(new FileEntry(storeDir+"/fileA", 1024, 9216)),// req no
  40. SharedHandle<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. SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
  52. diskAdaptor->setFileEntries
  53. (std::deque<SharedHandle<FileEntry> >(&fs[0], &fs[arrayLength(fs)]));
  54. diskAdaptor->setPieceLength(1024);
  55. diskAdaptor->setStoreDir(storeDir);
  56. diskAdaptor->openFile();
  57. SharedHandle<MultiFileAllocationIterator> itr
  58. (dynamic_pointer_cast<MultiFileAllocationIterator>
  59. (diskAdaptor->fileAllocationIterator()));
  60. DiskWriterEntries 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().isNull());
  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().isNull());
  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().isNull());
  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().isNull());
  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().isNull());
  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().isNull());
  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().isNull());
  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().isNull());
  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().isNull());
  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().isNull());
  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().isNull());
  117. }
  118. void MultiFileAllocationIteratorTest::testAllocate()
  119. {
  120. std::string storeDir =
  121. "/tmp/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. SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
  136. diskAdaptor->setStoreDir(storeDir);
  137. diskAdaptor->setPieceLength(1);
  138. int64_t offset = 0;
  139. SharedHandle<FileEntry> fileEntry1(new FileEntry(storeDir+"/"+fname1,
  140. length1,
  141. offset));
  142. offset += length1;
  143. SharedHandle<FileEntry> fileEntry2(new FileEntry(storeDir+"/"+fname2,
  144. length2,
  145. offset));
  146. offset += length2;
  147. SharedHandle<FileEntry> fileEntry3(new FileEntry(storeDir+"/"+fname3,
  148. length3,
  149. offset));
  150. offset += length3;
  151. SharedHandle<FileEntry> fileEntry4(new FileEntry(storeDir+"/"+fname4,
  152. length4,
  153. offset));
  154. fileEntry4->setRequested(false);
  155. offset += length4;
  156. SharedHandle<FileEntry> fileEntry5(new FileEntry(storeDir+"/"+fname5,
  157. length5,
  158. offset));
  159. offset += length5;
  160. SharedHandle<FileEntry> fileEntry6(new FileEntry(storeDir+"/"+fname6,
  161. length6,
  162. offset));
  163. fileEntry6->setRequested(false);
  164. std::deque<SharedHandle<FileEntry> > fs;
  165. fs.push_back(fileEntry1);
  166. fs.push_back(fileEntry2);
  167. fs.push_back(fileEntry3);
  168. fs.push_back(fileEntry4);
  169. fs.push_back(fileEntry5);
  170. fs.push_back(fileEntry6);
  171. diskAdaptor->setFileEntries(fs);
  172. for(std::deque<SharedHandle<FileEntry> >::const_iterator i = fs.begin();
  173. i != fs.end(); ++i) {
  174. File((*i)->getPath()).remove();
  175. }
  176. // we have to open file first.
  177. diskAdaptor->initAndOpenFile();
  178. SharedHandle<MultiFileAllocationIterator> itr
  179. (dynamic_pointer_cast<MultiFileAllocationIterator>
  180. (diskAdaptor->fileAllocationIterator()));
  181. while(!itr->finished()) {
  182. itr->allocateChunk();
  183. }
  184. CPPUNIT_ASSERT_EQUAL((uint64_t)length1, File(fileEntry1->getPath()).size());
  185. CPPUNIT_ASSERT_EQUAL((uint64_t)length2, File(fileEntry2->getPath()).size());
  186. CPPUNIT_ASSERT_EQUAL((uint64_t)length3, File(fileEntry3->getPath()).size());
  187. CPPUNIT_ASSERT(!File(fileEntry4->getPath()).isFile());
  188. CPPUNIT_ASSERT_EQUAL((uint64_t)length5, File(fileEntry5->getPath()).size());
  189. CPPUNIT_ASSERT(!File(fileEntry6->getPath()).isFile());
  190. } catch(Exception& e) {
  191. CPPUNIT_FAIL(e.stackTrace());
  192. }
  193. }
  194. } // namespace aria2