ServerStatManTest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "ServerStatMan.h"
  2. #include <iostream>
  3. #include <sstream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. #include "ServerStat.h"
  6. #include "Exception.h"
  7. #include "util.h"
  8. namespace aria2 {
  9. class ServerStatManTest:public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(ServerStatManTest);
  11. CPPUNIT_TEST(testAddAndFind);
  12. CPPUNIT_TEST(testSave);
  13. CPPUNIT_TEST(testLoad);
  14. CPPUNIT_TEST(testRemoveStaleServerStat);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void setUp() {}
  18. void tearDown() {}
  19. void testAddAndFind();
  20. void testSave();
  21. void testLoad();
  22. void testRemoveStaleServerStat();
  23. };
  24. CPPUNIT_TEST_SUITE_REGISTRATION(ServerStatManTest);
  25. void ServerStatManTest::testAddAndFind()
  26. {
  27. SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  28. SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  29. SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
  30. ServerStatMan ssm;
  31. CPPUNIT_ASSERT(ssm.add(localhost_http));
  32. CPPUNIT_ASSERT(!ssm.add(localhost_http));
  33. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  34. CPPUNIT_ASSERT(ssm.add(mirror));
  35. {
  36. SharedHandle<ServerStat> r = ssm.find("localhost", "http");
  37. CPPUNIT_ASSERT(r);
  38. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), r->getHostname());
  39. CPPUNIT_ASSERT_EQUAL(std::string("http"), r->getProtocol());
  40. }
  41. {
  42. SharedHandle<ServerStat> r = ssm.find("mirror", "ftp");
  43. CPPUNIT_ASSERT(!r);
  44. }
  45. }
  46. void ServerStatManTest::testSave()
  47. {
  48. SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  49. localhost_http->setDownloadSpeed(25000);
  50. localhost_http->setSingleConnectionAvgSpeed(100);
  51. localhost_http->setMultiConnectionAvgSpeed(101);
  52. localhost_http->setCounter(5);
  53. localhost_http->setLastUpdated(Time(1210000000));
  54. SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  55. localhost_ftp->setDownloadSpeed(30000);
  56. localhost_ftp->setLastUpdated(Time(1210000001));
  57. SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
  58. mirror->setDownloadSpeed(0);
  59. mirror->setStatus(ServerStat::ERROR);
  60. mirror->setLastUpdated(Time(1210000002));
  61. ServerStatMan ssm;
  62. CPPUNIT_ASSERT(ssm.add(localhost_http));
  63. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  64. CPPUNIT_ASSERT(ssm.add(mirror));
  65. std::stringstream ss;
  66. CPPUNIT_ASSERT(ssm.save(ss));
  67. std::string out = ss.str();
  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. out);
  92. }
  93. void ServerStatManTest::testLoad()
  94. {
  95. std::string in =
  96. "host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
  97. "host=localhost, protocol=http, dl_speed=25000, sc_avg_speed=101, mc_avg_speed=102, last_updated=1210000000, counter=6, status=OK\n"
  98. "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n";
  99. std::stringstream ss(in);
  100. ServerStatMan ssm;
  101. CPPUNIT_ASSERT(ssm.load(ss));
  102. SharedHandle<ServerStat> localhost_http = ssm.find("localhost", "http");
  103. CPPUNIT_ASSERT(localhost_http);
  104. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost_http->getHostname());
  105. CPPUNIT_ASSERT_EQUAL(std::string("http"), localhost_http->getProtocol());
  106. CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(25000),
  107. localhost_http->getDownloadSpeed());
  108. CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(101),
  109. localhost_http->getSingleConnectionAvgSpeed());
  110. CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(102),
  111. localhost_http->getMultiConnectionAvgSpeed());
  112. CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(6),
  113. localhost_http->getCounter());
  114. CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(1210000000),
  115. localhost_http->getLastUpdated().getTime());
  116. CPPUNIT_ASSERT_EQUAL(ServerStat::OK, localhost_http->getStatus());
  117. SharedHandle<ServerStat> mirror = ssm.find("mirror", "http");
  118. CPPUNIT_ASSERT(mirror);
  119. CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, mirror->getStatus());
  120. }
  121. void ServerStatManTest::testRemoveStaleServerStat()
  122. {
  123. Time now;
  124. SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  125. localhost_http->setDownloadSpeed(25000);
  126. localhost_http->setLastUpdated(now);
  127. SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  128. localhost_ftp->setDownloadSpeed(30000);
  129. localhost_ftp->setLastUpdated(Time(1210000001));
  130. SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
  131. mirror->setDownloadSpeed(0);
  132. mirror->setStatus(ServerStat::ERROR);
  133. mirror->setLastUpdated(Time(1210000002));
  134. ServerStatMan ssm;
  135. CPPUNIT_ASSERT(ssm.add(localhost_http));
  136. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  137. CPPUNIT_ASSERT(ssm.add(mirror));
  138. ssm.removeStaleServerStat(24*60*60);
  139. CPPUNIT_ASSERT(ssm.find("localhost", "http"));
  140. CPPUNIT_ASSERT(!ssm.find("localhost", "ftp"));
  141. CPPUNIT_ASSERT(!ssm.find("mirror", "http"));
  142. }
  143. } // namespace aria2