FeedbackURISelectorTest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "FeedbackURISelector.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "Exception.h"
  4. #include "util.h"
  5. #include "array_fun.h"
  6. #include "ServerStatMan.h"
  7. #include "ServerStat.h"
  8. #include "FileEntry.h"
  9. namespace aria2 {
  10. class FeedbackURISelectorTest:public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(FeedbackURISelectorTest);
  12. CPPUNIT_TEST(testSelect_withoutServerStat);
  13. CPPUNIT_TEST(testSelect);
  14. CPPUNIT_TEST(testSelect_skipErrorHost);
  15. CPPUNIT_TEST_SUITE_END();
  16. private:
  17. FileEntry _fileEntry;
  18. SharedHandle<ServerStatMan> ssm;
  19. SharedHandle<FeedbackURISelector> sel;
  20. public:
  21. void setUp()
  22. {
  23. static const char* urisSrc[] = {
  24. "http://alpha/file",
  25. "ftp://alpha/file",
  26. "http://bravo/file"
  27. };
  28. std::vector<std::string> uris;
  29. uris.assign(vbegin(urisSrc), vend(urisSrc));
  30. _fileEntry.setUris(uris);
  31. ssm.reset(new ServerStatMan());
  32. sel.reset(new FeedbackURISelector(ssm));
  33. }
  34. void tearDown() {}
  35. void testSelect_withoutServerStat();
  36. void testSelect();
  37. void testSelect_skipErrorHost();
  38. };
  39. CPPUNIT_TEST_SUITE_REGISTRATION(FeedbackURISelectorTest);
  40. void FeedbackURISelectorTest::testSelect_withoutServerStat()
  41. {
  42. // Without ServerStat, selector returns first URI
  43. std::string uri = sel->select(&_fileEntry);
  44. CPPUNIT_ASSERT_EQUAL(std::string("http://alpha/file"), uri);
  45. CPPUNIT_ASSERT_EQUAL((size_t)2, _fileEntry.getRemainingUris().size());
  46. }
  47. void FeedbackURISelectorTest::testSelect()
  48. {
  49. SharedHandle<ServerStat> bravo(new ServerStat("bravo", "http"));
  50. bravo->updateDownloadSpeed(100000);
  51. SharedHandle<ServerStat> alphaFTP(new ServerStat("alpha", "ftp"));
  52. alphaFTP->updateDownloadSpeed(80000);
  53. SharedHandle<ServerStat> alphaHTTP(new ServerStat("alpha", "http"));
  54. alphaHTTP->updateDownloadSpeed(180000);
  55. alphaHTTP->setError();
  56. ssm->add(bravo);
  57. ssm->add(alphaFTP);
  58. ssm->add(alphaHTTP);
  59. CPPUNIT_ASSERT_EQUAL(std::string("http://bravo/file"),
  60. sel->select(&_fileEntry));
  61. CPPUNIT_ASSERT_EQUAL((size_t)2, _fileEntry.getRemainingUris().size());
  62. CPPUNIT_ASSERT_EQUAL(std::string("ftp://alpha/file"),
  63. sel->select(&_fileEntry));
  64. CPPUNIT_ASSERT_EQUAL((size_t)1, _fileEntry.getRemainingUris().size());
  65. }
  66. void FeedbackURISelectorTest::testSelect_skipErrorHost()
  67. {
  68. SharedHandle<ServerStat> alphaHTTP(new ServerStat("alpha", "http"));
  69. alphaHTTP->setError();
  70. SharedHandle<ServerStat> alphaFTP(new ServerStat("alpha", "ftp"));
  71. alphaFTP->setError();
  72. ssm->add(alphaHTTP);
  73. ssm->add(alphaFTP);
  74. // See error URIs are removed from URI List.
  75. CPPUNIT_ASSERT_EQUAL(std::string("http://bravo/file"),
  76. sel->select(&_fileEntry));
  77. CPPUNIT_ASSERT_EQUAL((size_t)0, _fileEntry.getRemainingUris().size());
  78. }
  79. } // namespace aria2