ServerStatManTest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "ServerStatMan.h"
  2. #include "ServerStat.h"
  3. #include "Exception.h"
  4. #include "Util.h"
  5. #include <iostream>
  6. #include <sstream>
  7. #include <cppunit/extensions/HelperMacros.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.isNull());
  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.isNull());
  44. }
  45. }
  46. void ServerStatManTest::testSave()
  47. {
  48. SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  49. localhost_http->setDownloadSpeed(25000);
  50. localhost_http->setLastUpdated(Time(1210000000));
  51. SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  52. localhost_ftp->setDownloadSpeed(30000);
  53. localhost_ftp->setLastUpdated(Time(1210000001));
  54. SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
  55. mirror->setDownloadSpeed(0);
  56. mirror->setStatus(ServerStat::ERROR);
  57. mirror->setLastUpdated(Time(1210000002));
  58. ServerStatMan ssm;
  59. CPPUNIT_ASSERT(ssm.add(localhost_http));
  60. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  61. CPPUNIT_ASSERT(ssm.add(mirror));
  62. std::stringstream ss;
  63. CPPUNIT_ASSERT(ssm.save(ss));
  64. std::string out = ss.str();
  65. CPPUNIT_ASSERT_EQUAL
  66. (std::string
  67. ("host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
  68. "host=localhost, protocol=http, dl_speed=25000, last_updated=1210000000, status=OK\n"
  69. "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n"),
  70. out);
  71. }
  72. void ServerStatManTest::testLoad()
  73. {
  74. std::string in =
  75. "host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
  76. "host=localhost, protocol=http, dl_speed=25000, last_updated=1210000000, status=OK\n"
  77. "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n";
  78. std::stringstream ss(in);
  79. ServerStatMan ssm;
  80. CPPUNIT_ASSERT(ssm.load(ss));
  81. SharedHandle<ServerStat> localhost_http = ssm.find("localhost", "http");
  82. CPPUNIT_ASSERT(!localhost_http.isNull());
  83. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost_http->getHostname());
  84. CPPUNIT_ASSERT_EQUAL(std::string("http"), localhost_http->getProtocol());
  85. CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(25000),
  86. localhost_http->getDownloadSpeed());
  87. CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(1210000000),
  88. localhost_http->getLastUpdated().getTime());
  89. CPPUNIT_ASSERT_EQUAL(ServerStat::OK, localhost_http->getStatus());
  90. SharedHandle<ServerStat> mirror = ssm.find("mirror", "http");
  91. CPPUNIT_ASSERT(!mirror.isNull());
  92. CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, mirror->getStatus());
  93. }
  94. void ServerStatManTest::testRemoveStaleServerStat()
  95. {
  96. Time now;
  97. SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  98. localhost_http->setDownloadSpeed(25000);
  99. localhost_http->setLastUpdated(now);
  100. SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  101. localhost_ftp->setDownloadSpeed(30000);
  102. localhost_ftp->setLastUpdated(Time(1210000001));
  103. SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
  104. mirror->setDownloadSpeed(0);
  105. mirror->setStatus(ServerStat::ERROR);
  106. mirror->setLastUpdated(Time(1210000002));
  107. ServerStatMan ssm;
  108. CPPUNIT_ASSERT(ssm.add(localhost_http));
  109. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  110. CPPUNIT_ASSERT(ssm.add(mirror));
  111. ssm.removeStaleServerStat(24*60*60);
  112. CPPUNIT_ASSERT(!ssm.find("localhost", "http").isNull());
  113. CPPUNIT_ASSERT(ssm.find("localhost", "ftp").isNull());
  114. CPPUNIT_ASSERT(ssm.find("mirror", "http").isNull());
  115. }
  116. } // namespace aria2