ServerStatManTest.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(
  29. new ServerStat("localhost", "http"));
  30. std::shared_ptr<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  31. std::shared_ptr<ServerStat> mirror(new ServerStat("mirror", "http"));
  32. ServerStatMan ssm;
  33. CPPUNIT_ASSERT(ssm.add(localhost_http));
  34. CPPUNIT_ASSERT(!ssm.add(localhost_http));
  35. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  36. CPPUNIT_ASSERT(ssm.add(mirror));
  37. {
  38. std::shared_ptr<ServerStat> r = ssm.find("localhost", "http");
  39. CPPUNIT_ASSERT(r);
  40. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), r->getHostname());
  41. CPPUNIT_ASSERT_EQUAL(std::string("http"), r->getProtocol());
  42. }
  43. {
  44. std::shared_ptr<ServerStat> r = ssm.find("mirror", "ftp");
  45. CPPUNIT_ASSERT(!r);
  46. }
  47. }
  48. void ServerStatManTest::testSave()
  49. {
  50. std::shared_ptr<ServerStat> localhost_http(
  51. new ServerStat("localhost", "http"));
  52. localhost_http->setDownloadSpeed(25000);
  53. localhost_http->setSingleConnectionAvgSpeed(100);
  54. localhost_http->setMultiConnectionAvgSpeed(101);
  55. localhost_http->setCounter(5);
  56. localhost_http->setLastUpdated(Time(1210000000));
  57. std::shared_ptr<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  58. localhost_ftp->setDownloadSpeed(30000);
  59. localhost_ftp->setLastUpdated(Time(1210000001));
  60. std::shared_ptr<ServerStat> mirror(new ServerStat("mirror", "http"));
  61. mirror->setDownloadSpeed(0);
  62. mirror->setStatus(ServerStat::A2_ERROR);
  63. mirror->setLastUpdated(Time(1210000002));
  64. ServerStatMan ssm;
  65. CPPUNIT_ASSERT(ssm.add(localhost_http));
  66. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  67. CPPUNIT_ASSERT(ssm.add(mirror));
  68. const char* filename = A2_TEST_OUT_DIR "/aria2_ServerStatManTest_testSave";
  69. CPPUNIT_ASSERT(ssm.save(filename));
  70. CPPUNIT_ASSERT_EQUAL(std::string("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, "
  98. "status=OK\n"
  99. "host=localhost, protocol=http, dl_speed=25000, sc_avg_speed=101, "
  100. "mc_avg_speed=102, last_updated=1210000000, counter=6, status=OK\n"
  101. "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, "
  102. "status=ERROR\n";
  103. BufferedFile fp(filename, BufferedFile::WRITE);
  104. CPPUNIT_ASSERT_EQUAL((size_t)in.size(), fp.write(in.data(), in.size()));
  105. CPPUNIT_ASSERT(fp.close() != EOF);
  106. ServerStatMan ssm;
  107. CPPUNIT_ASSERT(ssm.load(filename));
  108. std::shared_ptr<ServerStat> localhost_http = ssm.find("localhost", "http");
  109. CPPUNIT_ASSERT(localhost_http);
  110. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost_http->getHostname());
  111. CPPUNIT_ASSERT_EQUAL(std::string("http"), localhost_http->getProtocol());
  112. CPPUNIT_ASSERT_EQUAL(25000, localhost_http->getDownloadSpeed());
  113. CPPUNIT_ASSERT_EQUAL(101, localhost_http->getSingleConnectionAvgSpeed());
  114. CPPUNIT_ASSERT_EQUAL(102, localhost_http->getMultiConnectionAvgSpeed());
  115. CPPUNIT_ASSERT_EQUAL(6, localhost_http->getCounter());
  116. CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(1210000000),
  117. localhost_http->getLastUpdated().getTimeFromEpoch());
  118. CPPUNIT_ASSERT_EQUAL(ServerStat::OK, localhost_http->getStatus());
  119. std::shared_ptr<ServerStat> mirror = ssm.find("mirror", "http");
  120. CPPUNIT_ASSERT(mirror);
  121. CPPUNIT_ASSERT_EQUAL(ServerStat::A2_ERROR, mirror->getStatus());
  122. }
  123. void ServerStatManTest::testRemoveStaleServerStat()
  124. {
  125. Time now;
  126. std::shared_ptr<ServerStat> localhost_http(
  127. new ServerStat("localhost", "http"));
  128. localhost_http->setDownloadSpeed(25000);
  129. localhost_http->setLastUpdated(now);
  130. std::shared_ptr<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  131. localhost_ftp->setDownloadSpeed(30000);
  132. localhost_ftp->setLastUpdated(Time(1210000001));
  133. std::shared_ptr<ServerStat> mirror(new ServerStat("mirror", "http"));
  134. mirror->setDownloadSpeed(0);
  135. mirror->setStatus(ServerStat::A2_ERROR);
  136. mirror->setLastUpdated(Time(1210000002));
  137. ServerStatMan ssm;
  138. CPPUNIT_ASSERT(ssm.add(localhost_http));
  139. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  140. CPPUNIT_ASSERT(ssm.add(mirror));
  141. ssm.removeStaleServerStat(24_h);
  142. CPPUNIT_ASSERT(ssm.find("localhost", "http"));
  143. CPPUNIT_ASSERT(!ssm.find("localhost", "ftp"));
  144. CPPUNIT_ASSERT(!ssm.find("mirror", "http"));
  145. }
  146. } // namespace aria2