123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #include "ServerStatMan.h"
- #include "ServerStat.h"
- #include "Exception.h"
- #include "Util.h"
- #include <iostream>
- #include <sstream>
- #include <cppunit/extensions/HelperMacros.h>
- namespace aria2 {
- class ServerStatManTest:public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(ServerStatManTest);
- CPPUNIT_TEST(testAddAndFind);
- CPPUNIT_TEST(testSave);
- CPPUNIT_TEST(testLoad);
- CPPUNIT_TEST(testRemoveStaleServerStat);
- CPPUNIT_TEST_SUITE_END();
- public:
- void setUp() {}
- void tearDown() {}
- void testAddAndFind();
- void testSave();
- void testLoad();
- void testRemoveStaleServerStat();
- };
- CPPUNIT_TEST_SUITE_REGISTRATION(ServerStatManTest);
- void ServerStatManTest::testAddAndFind()
- {
- SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
- SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
- SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
- ServerStatMan ssm;
- CPPUNIT_ASSERT(ssm.add(localhost_http));
- CPPUNIT_ASSERT(!ssm.add(localhost_http));
- CPPUNIT_ASSERT(ssm.add(localhost_ftp));
- CPPUNIT_ASSERT(ssm.add(mirror));
- {
- SharedHandle<ServerStat> r = ssm.find("localhost", "http");
- CPPUNIT_ASSERT(!r.isNull());
- CPPUNIT_ASSERT_EQUAL(std::string("localhost"), r->getHostname());
- CPPUNIT_ASSERT_EQUAL(std::string("http"), r->getProtocol());
- }
- {
- SharedHandle<ServerStat> r = ssm.find("mirror", "ftp");
- CPPUNIT_ASSERT(r.isNull());
- }
- }
- void ServerStatManTest::testSave()
- {
- SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
- localhost_http->setDownloadSpeed(25000);
- localhost_http->setLastUpdated(Time(1210000000));
- SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
- localhost_ftp->setDownloadSpeed(30000);
- localhost_ftp->setLastUpdated(Time(1210000001));
- SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
- mirror->setDownloadSpeed(0);
- mirror->setStatus(ServerStat::ERROR);
- mirror->setLastUpdated(Time(1210000002));
- ServerStatMan ssm;
- CPPUNIT_ASSERT(ssm.add(localhost_http));
- CPPUNIT_ASSERT(ssm.add(localhost_ftp));
- CPPUNIT_ASSERT(ssm.add(mirror));
- std::stringstream ss;
- CPPUNIT_ASSERT(ssm.save(ss));
- std::string out = ss.str();
- CPPUNIT_ASSERT_EQUAL
- (std::string
- ("host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
- "host=localhost, protocol=http, dl_speed=25000, last_updated=1210000000, status=OK\n"
- "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n"),
- out);
- }
- void ServerStatManTest::testLoad()
- {
- std::string in =
- "host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
- "host=localhost, protocol=http, dl_speed=25000, last_updated=1210000000, status=OK\n"
- "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n";
- std::stringstream ss(in);
- ServerStatMan ssm;
- CPPUNIT_ASSERT(ssm.load(ss));
- SharedHandle<ServerStat> localhost_http = ssm.find("localhost", "http");
- CPPUNIT_ASSERT(!localhost_http.isNull());
- CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost_http->getHostname());
- CPPUNIT_ASSERT_EQUAL(std::string("http"), localhost_http->getProtocol());
- CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(25000),
- localhost_http->getDownloadSpeed());
- CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(1210000000),
- localhost_http->getLastUpdated().getTime());
- CPPUNIT_ASSERT_EQUAL(ServerStat::OK, localhost_http->getStatus());
- SharedHandle<ServerStat> mirror = ssm.find("mirror", "http");
- CPPUNIT_ASSERT(!mirror.isNull());
- CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, mirror->getStatus());
- }
- void ServerStatManTest::testRemoveStaleServerStat()
- {
- Time now;
- SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
- localhost_http->setDownloadSpeed(25000);
- localhost_http->setLastUpdated(now);
- SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
- localhost_ftp->setDownloadSpeed(30000);
- localhost_ftp->setLastUpdated(Time(1210000001));
- SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
- mirror->setDownloadSpeed(0);
- mirror->setStatus(ServerStat::ERROR);
- mirror->setLastUpdated(Time(1210000002));
- ServerStatMan ssm;
- CPPUNIT_ASSERT(ssm.add(localhost_http));
- CPPUNIT_ASSERT(ssm.add(localhost_ftp));
- CPPUNIT_ASSERT(ssm.add(mirror));
- ssm.removeStaleServerStat(24*60*60);
- CPPUNIT_ASSERT(!ssm.find("localhost", "http").isNull());
- CPPUNIT_ASSERT(ssm.find("localhost", "ftp").isNull());
- CPPUNIT_ASSERT(ssm.find("mirror", "http").isNull());
- }
- } // namespace aria2
|