File: ServerStatManTest.cc

package info (click to toggle)
aria2 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 14,748 kB
  • ctags: 13,441
  • sloc: cpp: 86,740; ansic: 16,496; sh: 4,916; makefile: 1,312; ruby: 397; yacc: 291; xml: 170; sed: 16
file content (171 lines) | stat: -rw-r--r-- 5,563 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "ServerStatMan.h"

#include <iostream>
#include <sstream>

#include <cppunit/extensions/HelperMacros.h>

#include "ServerStat.h"
#include "Exception.h"
#include "util.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->setSingleConnectionAvgSpeed(100);
  localhost_http->setMultiConnectionAvgSpeed(101);
  localhost_http->setCounter(5);
  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,"
      " sc_avg_speed=0,"
      " mc_avg_speed=0,"
      " last_updated=1210000001,"
      " counter=0,"
      " status=OK\n"

      "host=localhost, protocol=http,"
      " dl_speed=25000,"
      " sc_avg_speed=100,"
      " mc_avg_speed=101,"
      " last_updated=1210000000,"
      " counter=5,"
      " status=OK\n"

      "host=mirror, protocol=http,"
      " dl_speed=0,"
      " sc_avg_speed=0,"
      " mc_avg_speed=0,"
      " last_updated=1210000002,"
      " counter=0,"
      " 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, sc_avg_speed=101, mc_avg_speed=102, last_updated=1210000000, counter=6, 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<unsigned int>(101),
                       localhost_http->getSingleConnectionAvgSpeed());
  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(102),
                       localhost_http->getMultiConnectionAvgSpeed());
  CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(6),
                       localhost_http->getCounter());
  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