1 #include "ServerStatMan.h"
2 
3 #include <iostream>
4 
5 #include <cppunit/extensions/HelperMacros.h>
6 
7 #include "ServerStat.h"
8 #include "Exception.h"
9 #include "util.h"
10 #include "BufferedFile.h"
11 #include "TestUtil.h"
12 
13 namespace aria2 {
14 
15 class ServerStatManTest : public CppUnit::TestFixture {
16 
17   CPPUNIT_TEST_SUITE(ServerStatManTest);
18   CPPUNIT_TEST(testAddAndFind);
19   CPPUNIT_TEST(testSave);
20   CPPUNIT_TEST(testLoad);
21   CPPUNIT_TEST(testRemoveStaleServerStat);
22   CPPUNIT_TEST_SUITE_END();
23 
24 public:
setUp()25   void setUp() {}
26 
tearDown()27   void tearDown() {}
28 
29   void testAddAndFind();
30   void testSave();
31   void testLoad();
32   void testRemoveStaleServerStat();
33 };
34 
35 CPPUNIT_TEST_SUITE_REGISTRATION(ServerStatManTest);
36 
testAddAndFind()37 void ServerStatManTest::testAddAndFind()
38 {
39   std::shared_ptr<ServerStat> localhost_http(
40       new ServerStat("localhost", "http"));
41   std::shared_ptr<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
42   std::shared_ptr<ServerStat> mirror(new ServerStat("mirror", "http"));
43 
44   ServerStatMan ssm;
45   CPPUNIT_ASSERT(ssm.add(localhost_http));
46   CPPUNIT_ASSERT(!ssm.add(localhost_http));
47   CPPUNIT_ASSERT(ssm.add(localhost_ftp));
48   CPPUNIT_ASSERT(ssm.add(mirror));
49 
50   {
51     std::shared_ptr<ServerStat> r = ssm.find("localhost", "http");
52     CPPUNIT_ASSERT(r);
53     CPPUNIT_ASSERT_EQUAL(std::string("localhost"), r->getHostname());
54     CPPUNIT_ASSERT_EQUAL(std::string("http"), r->getProtocol());
55   }
56   {
57     std::shared_ptr<ServerStat> r = ssm.find("mirror", "ftp");
58     CPPUNIT_ASSERT(!r);
59   }
60 }
61 
testSave()62 void ServerStatManTest::testSave()
63 {
64   std::shared_ptr<ServerStat> localhost_http(
65       new ServerStat("localhost", "http"));
66   localhost_http->setDownloadSpeed(25000);
67   localhost_http->setSingleConnectionAvgSpeed(100);
68   localhost_http->setMultiConnectionAvgSpeed(101);
69   localhost_http->setCounter(5);
70   localhost_http->setLastUpdated(Time(1210000000));
71   std::shared_ptr<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
72   localhost_ftp->setDownloadSpeed(30000);
73   localhost_ftp->setLastUpdated(Time(1210000001));
74   std::shared_ptr<ServerStat> mirror(new ServerStat("mirror", "http"));
75   mirror->setDownloadSpeed(0);
76   mirror->setStatus(ServerStat::A2_ERROR);
77   mirror->setLastUpdated(Time(1210000002));
78 
79   ServerStatMan ssm;
80   CPPUNIT_ASSERT(ssm.add(localhost_http));
81   CPPUNIT_ASSERT(ssm.add(localhost_ftp));
82   CPPUNIT_ASSERT(ssm.add(mirror));
83 
84   const char* filename = A2_TEST_OUT_DIR "/aria2_ServerStatManTest_testSave";
85   CPPUNIT_ASSERT(ssm.save(filename));
86   CPPUNIT_ASSERT_EQUAL(std::string("host=localhost, protocol=ftp,"
87                                    " dl_speed=30000,"
88                                    " sc_avg_speed=0,"
89                                    " mc_avg_speed=0,"
90                                    " last_updated=1210000001,"
91                                    " counter=0,"
92                                    " status=OK\n"
93 
94                                    "host=localhost, protocol=http,"
95                                    " dl_speed=25000,"
96                                    " sc_avg_speed=100,"
97                                    " mc_avg_speed=101,"
98                                    " last_updated=1210000000,"
99                                    " counter=5,"
100                                    " status=OK\n"
101 
102                                    "host=mirror, protocol=http,"
103                                    " dl_speed=0,"
104                                    " sc_avg_speed=0,"
105                                    " mc_avg_speed=0,"
106                                    " last_updated=1210000002,"
107                                    " counter=0,"
108                                    " status=ERROR\n"),
109                        readFile(filename));
110 }
111 
testLoad()112 void ServerStatManTest::testLoad()
113 {
114   const char* filename = A2_TEST_OUT_DIR "/aria2_ServerStatManTest_testLoad";
115   std::string in =
116       "host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, "
117       "status=OK\n"
118       "host=localhost, protocol=http, dl_speed=25000, sc_avg_speed=101, "
119       "mc_avg_speed=102, last_updated=1210000000, counter=6, status=OK\n"
120       "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, "
121       "status=ERROR\n";
122   BufferedFile fp(filename, BufferedFile::WRITE);
123   CPPUNIT_ASSERT_EQUAL((size_t)in.size(), fp.write(in.data(), in.size()));
124   CPPUNIT_ASSERT(fp.close() != EOF);
125 
126   ServerStatMan ssm;
127   CPPUNIT_ASSERT(ssm.load(filename));
128 
129   std::shared_ptr<ServerStat> localhost_http = ssm.find("localhost", "http");
130   CPPUNIT_ASSERT(localhost_http);
131   CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost_http->getHostname());
132   CPPUNIT_ASSERT_EQUAL(std::string("http"), localhost_http->getProtocol());
133   CPPUNIT_ASSERT_EQUAL(25000, localhost_http->getDownloadSpeed());
134   CPPUNIT_ASSERT_EQUAL(101, localhost_http->getSingleConnectionAvgSpeed());
135   CPPUNIT_ASSERT_EQUAL(102, localhost_http->getMultiConnectionAvgSpeed());
136   CPPUNIT_ASSERT_EQUAL(6, localhost_http->getCounter());
137   CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(1210000000),
138                        localhost_http->getLastUpdated().getTimeFromEpoch());
139   CPPUNIT_ASSERT_EQUAL(ServerStat::OK, localhost_http->getStatus());
140 
141   std::shared_ptr<ServerStat> mirror = ssm.find("mirror", "http");
142   CPPUNIT_ASSERT(mirror);
143   CPPUNIT_ASSERT_EQUAL(ServerStat::A2_ERROR, mirror->getStatus());
144 }
145 
testRemoveStaleServerStat()146 void ServerStatManTest::testRemoveStaleServerStat()
147 {
148   Time now;
149   std::shared_ptr<ServerStat> localhost_http(
150       new ServerStat("localhost", "http"));
151   localhost_http->setDownloadSpeed(25000);
152   localhost_http->setLastUpdated(now);
153   std::shared_ptr<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
154   localhost_ftp->setDownloadSpeed(30000);
155   localhost_ftp->setLastUpdated(Time(1210000001));
156   std::shared_ptr<ServerStat> mirror(new ServerStat("mirror", "http"));
157   mirror->setDownloadSpeed(0);
158   mirror->setStatus(ServerStat::A2_ERROR);
159   mirror->setLastUpdated(Time(1210000002));
160 
161   ServerStatMan ssm;
162   CPPUNIT_ASSERT(ssm.add(localhost_http));
163   CPPUNIT_ASSERT(ssm.add(localhost_ftp));
164   CPPUNIT_ASSERT(ssm.add(mirror));
165 
166   ssm.removeStaleServerStat(24_h);
167 
168   CPPUNIT_ASSERT(ssm.find("localhost", "http"));
169   CPPUNIT_ASSERT(!ssm.find("localhost", "ftp"));
170   CPPUNIT_ASSERT(!ssm.find("mirror", "http"));
171 }
172 
173 } // namespace aria2
174