forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedbackURISelectorTest.cc
135 lines (104 loc) · 3.91 KB
/
FeedbackURISelectorTest.cc
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
#include "FeedbackURISelector.h"
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "util.h"
#include "array_fun.h"
#include "ServerStatMan.h"
#include "ServerStat.h"
#include "FileEntry.h"
namespace aria2 {
class FeedbackURISelectorTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(FeedbackURISelectorTest);
CPPUNIT_TEST(testSelect_withoutServerStat);
CPPUNIT_TEST(testSelect);
CPPUNIT_TEST(testSelect_withUsedHosts);
CPPUNIT_TEST(testSelect_skipErrorHost);
CPPUNIT_TEST_SUITE_END();
private:
FileEntry fileEntry_;
SharedHandle<ServerStatMan> ssm;
SharedHandle<FeedbackURISelector> sel;
public:
void setUp()
{
static const char* urisSrc[] = {
"http://alpha/file",
"ftp://alpha/file",
"http://bravo/file"
};
std::vector<std::string> uris;
uris.assign(vbegin(urisSrc), vend(urisSrc));
fileEntry_.setUris(uris);
ssm.reset(new ServerStatMan());
sel.reset(new FeedbackURISelector(ssm));
}
void tearDown() {}
void testSelect_withoutServerStat();
void testSelect();
void testSelect_withUsedHosts();
void testSelect_skipErrorHost();
};
CPPUNIT_TEST_SUITE_REGISTRATION(FeedbackURISelectorTest);
void FeedbackURISelectorTest::testSelect_withoutServerStat()
{
std::vector<std::string> usedHosts;
// Without ServerStat, selector returns first URI
std::string uri = sel->select(&fileEntry_, usedHosts);
CPPUNIT_ASSERT_EQUAL(std::string("http://alpha/file"), uri);
CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntry_.getRemainingUris().size());
}
void FeedbackURISelectorTest::testSelect()
{
SharedHandle<ServerStat> bravo(new ServerStat("bravo", "http"));
bravo->updateDownloadSpeed(100000);
SharedHandle<ServerStat> alphaFTP(new ServerStat("alpha", "ftp"));
alphaFTP->updateDownloadSpeed(80000);
SharedHandle<ServerStat> alphaHTTP(new ServerStat("alpha", "http"));
alphaHTTP->updateDownloadSpeed(180000);
alphaHTTP->setError();
std::vector<std::string> usedHosts;
ssm->add(bravo);
ssm->add(alphaFTP);
ssm->add(alphaHTTP);
CPPUNIT_ASSERT_EQUAL(std::string("http://bravo/file"),
sel->select(&fileEntry_, usedHosts));
CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntry_.getRemainingUris().size());
CPPUNIT_ASSERT_EQUAL(std::string("ftp://alpha/file"),
sel->select(&fileEntry_, usedHosts));
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntry_.getRemainingUris().size());
}
void FeedbackURISelectorTest::testSelect_withUsedHosts()
{
SharedHandle<ServerStat> bravo(new ServerStat("bravo", "http"));
bravo->updateDownloadSpeed(100000);
SharedHandle<ServerStat> alphaHTTP(new ServerStat("alpha", "http"));
alphaHTTP->updateDownloadSpeed(180000);
alphaHTTP->setError();
std::vector<std::string> usedHosts;
usedHosts.push_back("bravo");
ssm->add(bravo);
ssm->add(alphaHTTP);
CPPUNIT_ASSERT_EQUAL(std::string("ftp://alpha/file"),
sel->select(&fileEntry_, usedHosts));
CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntry_.getRemainingUris().size());
CPPUNIT_ASSERT_EQUAL(std::string("http://bravo/file"),
sel->select(&fileEntry_, usedHosts));
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntry_.getRemainingUris().size());
CPPUNIT_ASSERT_EQUAL(std::string("http://alpha/file"),
sel->select(&fileEntry_, usedHosts));
CPPUNIT_ASSERT_EQUAL((size_t)0, fileEntry_.getRemainingUris().size());
}
void FeedbackURISelectorTest::testSelect_skipErrorHost()
{
SharedHandle<ServerStat> alphaHTTP(new ServerStat("alpha", "http"));
alphaHTTP->setError();
SharedHandle<ServerStat> alphaFTP(new ServerStat("alpha", "ftp"));
alphaFTP->setError();
std::vector<std::string> usedHosts;
ssm->add(alphaHTTP);
ssm->add(alphaFTP);
CPPUNIT_ASSERT_EQUAL(std::string("http://bravo/file"),
sel->select(&fileEntry_, usedHosts));
CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntry_.getRemainingUris().size());
}
} // namespace aria2