ServerStatManTest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "ServerStatMan.h"
  2. #include <iostream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "ServerStat.h"
  5. #include "Exception.h"
  6. #include "util.h"
  7. #include "BufferedFile.h"
  8. #include "TestUtil.h"
  9. namespace aria2 {
  10. class ServerStatManTest:public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(ServerStatManTest);
  12. CPPUNIT_TEST(testAddAndFind);
  13. CPPUNIT_TEST(testSave);
  14. CPPUNIT_TEST(testLoad);
  15. CPPUNIT_TEST(testRemoveStaleServerStat);
  16. CPPUNIT_TEST_SUITE_END();
  17. public:
  18. void setUp() {}
  19. void tearDown() {}
  20. void testAddAndFind();
  21. void testSave();
  22. void testLoad();
  23. void testRemoveStaleServerStat();
  24. };
  25. CPPUNIT_TEST_SUITE_REGISTRATION(ServerStatManTest);
  26. void ServerStatManTest::testAddAndFind()
  27. {
  28. std::shared_ptr<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  29. std::shared_ptr<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  30. std::shared_ptr<ServerStat> mirror(new ServerStat("mirror", "http"));
  31. ServerStatMan ssm;
  32. CPPUNIT_ASSERT(ssm.add(localhost_http));
  33. CPPUNIT_ASSERT(!ssm.add(localhost_http));
  34. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  35. CPPUNIT_ASSERT(ssm.add(mirror));
  36. {
  37. std::shared_ptr<ServerStat> r = ssm.find("localhost", "http");
  38. CPPUNIT_ASSERT(r);
  39. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), r->getHostname());
  40. CPPUNIT_ASSERT_EQUAL(std::string("http"), r->getProtocol());
  41. }
  42. {
  43. std::shared_ptr<ServerStat> r = ssm.find("mirror", "ftp");
  44. CPPUNIT_ASSERT(!r);
  45. }
  46. }
  47. void ServerStatManTest::testSave()
  48. {
  49. std::shared_ptr<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  50. localhost_http->setDownloadSpeed(25000);
  51. localhost_http->setSingleConnectionAvgSpeed(100);
  52. localhost_http->setMultiConnectionAvgSpeed(101);
  53. localhost_http->setCounter(5);
  54. localhost_http->setLastUpdated(Time(1210000000));
  55. std::shared_ptr<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  56. localhost_ftp->setDownloadSpeed(30000);
  57. localhost_ftp->setLastUpdated(Time(1210000001));
  58. std::shared_ptr<ServerStat> mirror(new ServerStat("mirror", "http"));
  59. mirror->setDownloadSpeed(0);
  60. mirror->setStatus(ServerStat::A2_ERROR);
  61. mirror->setLastUpdated(Time(1210000002));
  62. ServerStatMan ssm;
  63. CPPUNIT_ASSERT(ssm.add(localhost_http));
  64. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  65. CPPUNIT_ASSERT(ssm.add(mirror));
  66. const char* filename = A2_TEST_OUT_DIR"/aria2_ServerStatManTest_testSave";
  67. CPPUNIT_ASSERT(ssm.save(filename));
  68. CPPUNIT_ASSERT_EQUAL
  69. (std::string
  70. ("host=localhost, protocol=ftp,"
  71. " dl_speed=30000,"
  72. " sc_avg_speed=0,"
  73. " mc_avg_speed=0,"
  74. " last_updated=1210000001,"
  75. " counter=0,"
  76. " status=OK\n"
  77. "host=localhost, protocol=http,"
  78. " dl_speed=25000,"
  79. " sc_avg_speed=100,"
  80. " mc_avg_speed=101,"
  81. " last_updated=1210000000,"
  82. " counter=5,"
  83. " status=OK\n"
  84. "host=mirror, protocol=http,"
  85. " dl_speed=0,"
  86. " sc_avg_speed=0,"
  87. " mc_avg_speed=0,"
  88. " last_updated=1210000002,"
  89. " counter=0,"
  90. " status=ERROR\n"),
  91. readFile(filename));
  92. }
  93. void ServerStatManTest::testLoad()
  94. {
  95. const char* filename = A2_TEST_OUT_DIR"/aria2_ServerStatManTest_testLoad";
  96. std::string in =
  97. "host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
  98. "host=localhost, protocol=http, dl_speed=25000, sc_avg_speed=101, mc_avg_speed=102, last_updated=1210000000, counter=6, status=OK\n"
  99. "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n";
  100. BufferedFile fp(filename, BufferedFile::WRITE);
  101. CPPUNIT_ASSERT_EQUAL((size_t)in.size(), fp.write(in.data(), in.size()));
  102. CPPUNIT_ASSERT(fp.close() != EOF);
  103. ServerStatMan ssm;
  104. CPPUNIT_ASSERT(ssm.load(filename));
  105. std::shared_ptr<ServerStat> localhost_http = ssm.find("localhost", "http");
  106. CPPUNIT_ASSERT(localhost_http);
  107. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost_http->getHostname());
  108. CPPUNIT_ASSERT_EQUAL(std::string("http"), localhost_http->getProtocol());
  109. CPPUNIT_ASSERT_EQUAL(25000, localhost_http->getDownloadSpeed());
  110. CPPUNIT_ASSERT_EQUAL(101, localhost_http->getSingleConnectionAvgSpeed());
  111. CPPUNIT_ASSERT_EQUAL(102, localhost_http->getMultiConnectionAvgSpeed());
  112. CPPUNIT_ASSERT_EQUAL(6, localhost_http->getCounter());
  113. CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(1210000000),
  114. localhost_http->getLastUpdated().getTime());
  115. CPPUNIT_ASSERT_EQUAL(ServerStat::OK, localhost_http->getStatus());
  116. std::shared_ptr<ServerStat> mirror = ssm.find("mirror", "http");
  117. CPPUNIT_ASSERT(mirror);
  118. CPPUNIT_ASSERT_EQUAL(ServerStat::A2_ERROR, mirror->getStatus());
  119. }
  120. void ServerStatManTest::testRemoveStaleServerStat()
  121. {
  122. Time now;
  123. std::shared_ptr<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  124. localhost_http->setDownloadSpeed(25000);
  125. localhost_http->setLastUpdated(now);
  126. std::shared_ptr<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  127. localhost_ftp->setDownloadSpeed(30000);
  128. localhost_ftp->setLastUpdated(Time(1210000001));
  129. std::shared_ptr<ServerStat> mirror(new ServerStat("mirror", "http"));
  130. mirror->setDownloadSpeed(0);
  131. mirror->setStatus(ServerStat::A2_ERROR);
  132. mirror->setLastUpdated(Time(1210000002));
  133. ServerStatMan ssm;
  134. CPPUNIT_ASSERT(ssm.add(localhost_http));
  135. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  136. CPPUNIT_ASSERT(ssm.add(mirror));
  137. ssm.removeStaleServerStat(24*60*60);
  138. CPPUNIT_ASSERT(ssm.find("localhost", "http"));
  139. CPPUNIT_ASSERT(!ssm.find("localhost", "ftp"));
  140. CPPUNIT_ASSERT(!ssm.find("mirror", "http"));
  141. }
  142. } // namespace aria2