ServerStatManTest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_SUITE_END();
  15. public:
  16. void setUp() {}
  17. void tearDown() {}
  18. void testAddAndFind();
  19. void testSave();
  20. void testLoad();
  21. };
  22. CPPUNIT_TEST_SUITE_REGISTRATION(ServerStatManTest);
  23. void ServerStatManTest::testAddAndFind()
  24. {
  25. SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  26. SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  27. SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
  28. ServerStatMan ssm;
  29. CPPUNIT_ASSERT(ssm.add(localhost_http));
  30. CPPUNIT_ASSERT(!ssm.add(localhost_http));
  31. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  32. CPPUNIT_ASSERT(ssm.add(mirror));
  33. {
  34. SharedHandle<ServerStat> r = ssm.find("localhost", "http");
  35. CPPUNIT_ASSERT(!r.isNull());
  36. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), r->getHostname());
  37. CPPUNIT_ASSERT_EQUAL(std::string("http"), r->getProtocol());
  38. }
  39. {
  40. SharedHandle<ServerStat> r = ssm.find("mirror", "ftp");
  41. CPPUNIT_ASSERT(r.isNull());
  42. }
  43. }
  44. void ServerStatManTest::testSave()
  45. {
  46. SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  47. localhost_http->setDownloadSpeed(25000);
  48. localhost_http->setLastUpdated(Time(1210000000));
  49. SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  50. localhost_ftp->setDownloadSpeed(30000);
  51. localhost_ftp->setLastUpdated(Time(1210000001));
  52. SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
  53. mirror->setDownloadSpeed(0);
  54. mirror->setError();
  55. mirror->setLastUpdated(Time(1210000002));
  56. ServerStatMan ssm;
  57. CPPUNIT_ASSERT(ssm.add(localhost_http));
  58. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  59. CPPUNIT_ASSERT(ssm.add(mirror));
  60. std::stringstream ss;
  61. ssm.save(ss);
  62. std::string out = ss.str();
  63. CPPUNIT_ASSERT_EQUAL
  64. (std::string
  65. ("host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
  66. "host=localhost, protocol=http, dl_speed=25000, last_updated=1210000000, status=OK\n"
  67. "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n"),
  68. out);
  69. }
  70. void ServerStatManTest::testLoad()
  71. {
  72. std::string in =
  73. "host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
  74. "host=localhost, protocol=http, dl_speed=25000, last_updated=1210000000, status=OK\n"
  75. "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n";
  76. std::stringstream ss(in);
  77. ServerStatMan ssm;
  78. ssm.load(ss);
  79. SharedHandle<ServerStat> localhost_http = ssm.find("localhost", "http");
  80. CPPUNIT_ASSERT(!localhost_http.isNull());
  81. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost_http->getHostname());
  82. CPPUNIT_ASSERT_EQUAL(std::string("http"), localhost_http->getProtocol());
  83. CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(25000),
  84. localhost_http->getDownloadSpeed());
  85. CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(1210000000),
  86. localhost_http->getLastUpdated().getTime());
  87. CPPUNIT_ASSERT_EQUAL(ServerStat::OK, localhost_http->getStatus());
  88. SharedHandle<ServerStat> mirror = ssm.find("mirror", "http");
  89. CPPUNIT_ASSERT(!mirror.isNull());
  90. CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, mirror->getStatus());
  91. }
  92. } // namespace aria2