RequestGroupTest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "RequestGroup.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "ServerHost.h"
  4. #include "Option.h"
  5. #include "DownloadContext.h"
  6. #include "FileEntry.h"
  7. #include "PieceStorage.h"
  8. #include "DownloadResult.h"
  9. namespace aria2 {
  10. class RequestGroupTest : public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(RequestGroupTest);
  12. CPPUNIT_TEST(testRegisterSearchRemove);
  13. CPPUNIT_TEST(testGetFirstFilePath);
  14. CPPUNIT_TEST(testCreateDownloadResult);
  15. CPPUNIT_TEST_SUITE_END();
  16. private:
  17. SharedHandle<Option> _option;
  18. public:
  19. void setUp()
  20. {
  21. _option.reset(new Option());
  22. }
  23. void testRegisterSearchRemove();
  24. void testGetFirstFilePath();
  25. void testCreateDownloadResult();
  26. };
  27. CPPUNIT_TEST_SUITE_REGISTRATION( RequestGroupTest );
  28. void RequestGroupTest::testRegisterSearchRemove()
  29. {
  30. RequestGroup rg(_option);
  31. SharedHandle<ServerHost> sv1(new ServerHost(1, "localhost1"));
  32. SharedHandle<ServerHost> sv2(new ServerHost(2, "localhost2"));
  33. SharedHandle<ServerHost> sv3(new ServerHost(3, "localhost3"));
  34. rg.registerServerHost(sv3);
  35. rg.registerServerHost(sv1);
  36. rg.registerServerHost(sv2);
  37. CPPUNIT_ASSERT(rg.searchServerHost(0).isNull());
  38. {
  39. SharedHandle<ServerHost> sv = rg.searchServerHost(1);
  40. CPPUNIT_ASSERT(!sv.isNull());
  41. CPPUNIT_ASSERT_EQUAL(std::string("localhost1"), sv->getHostname());
  42. }
  43. rg.removeServerHost(1);
  44. {
  45. SharedHandle<ServerHost> sv = rg.searchServerHost(1);
  46. CPPUNIT_ASSERT(sv.isNull());
  47. }
  48. {
  49. SharedHandle<ServerHost> sv = rg.searchServerHost(2);
  50. CPPUNIT_ASSERT(!sv.isNull());
  51. CPPUNIT_ASSERT_EQUAL(std::string("localhost2"), sv->getHostname());
  52. }
  53. }
  54. void RequestGroupTest::testGetFirstFilePath()
  55. {
  56. SharedHandle<DownloadContext> ctx
  57. (new DownloadContext(1024, 1024, "/tmp/myfile"));
  58. RequestGroup group(_option);
  59. group.setDownloadContext(ctx);
  60. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFirstFilePath());
  61. group.markInMemoryDownload();
  62. CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFirstFilePath());
  63. }
  64. void RequestGroupTest::testCreateDownloadResult()
  65. {
  66. SharedHandle<DownloadContext> ctx
  67. (new DownloadContext(1024, 1024*1024, "/tmp/myfile"));
  68. RequestGroup group(_option);
  69. group.setDownloadContext(ctx);
  70. group.initPieceStorage();
  71. {
  72. SharedHandle<DownloadResult> result = group.createDownloadResult();
  73. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"),
  74. result->fileEntries[0]->getPath());
  75. CPPUNIT_ASSERT_EQUAL((off_t)1024*1024,
  76. result->fileEntries.back()->getLastOffset());
  77. CPPUNIT_ASSERT_EQUAL((uint64_t)0, result->sessionDownloadLength);
  78. CPPUNIT_ASSERT_EQUAL((int64_t)0, result->sessionTime);
  79. // result is UNKNOWN_ERROR if download has not completed and no specific
  80. // error has been reported
  81. CPPUNIT_ASSERT_EQUAL(downloadresultcode::UNKNOWN_ERROR, result->result);
  82. // if haltReason is set to RequestGroup::USER_REQUEST, download
  83. // result becomes IN_PROGRESS
  84. group.setHaltRequested(true, RequestGroup::USER_REQUEST);
  85. result = group.createDownloadResult();
  86. CPPUNIT_ASSERT_EQUAL(downloadresultcode::IN_PROGRESS, result->result);
  87. }
  88. {
  89. group.setLastUriResult
  90. ("http://second/file",downloadresultcode::RESOURCE_NOT_FOUND);
  91. SharedHandle<DownloadResult> result = group.createDownloadResult();
  92. CPPUNIT_ASSERT_EQUAL(downloadresultcode::RESOURCE_NOT_FOUND, result->result);
  93. }
  94. {
  95. group.getPieceStorage()->markAllPiecesDone();
  96. SharedHandle<DownloadResult> result = group.createDownloadResult();
  97. CPPUNIT_ASSERT_EQUAL(downloadresultcode::FINISHED, result->result);
  98. }
  99. }
  100. } // namespace aria2