ServerStatManTest.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_SUITE_END();
  14. public:
  15. void setUp() {}
  16. void tearDown() {}
  17. void testAddAndFind();
  18. void testSave();
  19. };
  20. CPPUNIT_TEST_SUITE_REGISTRATION(ServerStatManTest);
  21. void ServerStatManTest::testAddAndFind()
  22. {
  23. SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  24. SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  25. SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
  26. ServerStatMan ssm;
  27. CPPUNIT_ASSERT(ssm.add(localhost_http));
  28. CPPUNIT_ASSERT(!ssm.add(localhost_http));
  29. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  30. CPPUNIT_ASSERT(ssm.add(mirror));
  31. {
  32. SharedHandle<ServerStat> r = ssm.find("localhost", "http");
  33. CPPUNIT_ASSERT(!r.isNull());
  34. CPPUNIT_ASSERT_EQUAL(std::string("localhost"), r->getHostname());
  35. CPPUNIT_ASSERT_EQUAL(std::string("http"), r->getProtocol());
  36. }
  37. {
  38. SharedHandle<ServerStat> r = ssm.find("mirror", "ftp");
  39. CPPUNIT_ASSERT(r.isNull());
  40. }
  41. }
  42. void ServerStatManTest::testSave()
  43. {
  44. SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
  45. localhost_http->setDownloadSpeed(25000);
  46. localhost_http->setLastUpdated(Time(1210000000));
  47. SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
  48. localhost_ftp->setDownloadSpeed(30000);
  49. localhost_ftp->setLastUpdated(Time(1210000001));
  50. SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
  51. mirror->setDownloadSpeed(0);
  52. mirror->setError();
  53. mirror->setLastUpdated(Time(1210000002));
  54. ServerStatMan ssm;
  55. CPPUNIT_ASSERT(ssm.add(localhost_http));
  56. CPPUNIT_ASSERT(ssm.add(localhost_ftp));
  57. CPPUNIT_ASSERT(ssm.add(mirror));
  58. std::stringstream ss;
  59. ssm.save(ss);
  60. std::string out = ss.str();
  61. CPPUNIT_ASSERT_EQUAL
  62. (std::string
  63. ("host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
  64. "host=localhost, protocol=http, dl_speed=25000, last_updated=1210000000, status=OK\n"
  65. "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n"),
  66. out);
  67. }
  68. } // namespace aria2