2006-09-23 11:58:23 +00:00
|
|
|
#include "SegmentMan.h"
|
2009-01-31 14:34:29 +00:00
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "UnknownLengthPieceStorage.h"
|
2008-01-06 16:37:25 +00:00
|
|
|
#include "DefaultPieceStorage.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "Segment.h"
|
|
|
|
#include "Option.h"
|
2009-01-31 14:34:29 +00:00
|
|
|
#include "PieceSelector.h"
|
2006-09-23 11:58:23 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-09-23 11:58:23 +00:00
|
|
|
|
|
|
|
class SegmentManTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(SegmentManTest);
|
|
|
|
CPPUNIT_TEST(testNullBitfield);
|
2008-01-06 16:37:25 +00:00
|
|
|
CPPUNIT_TEST(testCompleteSegment);
|
2009-06-30 17:03:57 +00:00
|
|
|
CPPUNIT_TEST(testGetSegment_sameFileEntry);
|
2009-07-02 16:26:04 +00:00
|
|
|
CPPUNIT_TEST(testRegisterPeerStat);
|
2006-09-23 11:58:23 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNullBitfield();
|
2008-01-06 16:37:25 +00:00
|
|
|
void testCompleteSegment();
|
2008-07-20 07:55:10 +00:00
|
|
|
void testGetPeerStat();
|
2009-06-30 17:03:57 +00:00
|
|
|
void testGetSegment_sameFileEntry();
|
2009-07-02 16:26:04 +00:00
|
|
|
void testRegisterPeerStat();
|
2006-09-23 11:58:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( SegmentManTest );
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void SegmentManTest::testNullBitfield()
|
|
|
|
{
|
2006-09-23 11:58:23 +00:00
|
|
|
Option op;
|
2009-06-28 10:37:15 +00:00
|
|
|
SharedHandle<DownloadContext> dctx
|
|
|
|
(new DownloadContext(0, 0, "aria2.tar.bz2"));
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<UnknownLengthPieceStorage> ps
|
|
|
|
(new UnknownLengthPieceStorage(dctx, &op));
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentMan segmentMan(&op, dctx, ps);
|
2006-09-23 11:58:23 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<Segment> segment = segmentMan.getSegment(1);
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
CPPUNIT_ASSERT(!segment.isNull());
|
2008-03-08 08:04:28 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, segment->getIndex());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, segment->getLength());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, segment->getSegmentLength());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, segment->getWrittenLength());
|
2006-09-23 11:58:23 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<Segment> segment2 = segmentMan.getSegment(2);
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
CPPUNIT_ASSERT(segment2.isNull());
|
2006-09-23 11:58:23 +00:00
|
|
|
|
2006-10-01 11:29:14 +00:00
|
|
|
segmentMan.cancelSegment(1);
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_ASSERT(!segmentMan.getSegment(2).isNull());
|
2007-03-24 14:32:49 +00:00
|
|
|
}
|
|
|
|
|
2008-01-06 16:37:25 +00:00
|
|
|
void SegmentManTest::testCompleteSegment()
|
|
|
|
{
|
|
|
|
Option op;
|
2008-03-08 08:04:28 +00:00
|
|
|
size_t pieceLength = 1024*1024;
|
|
|
|
uint64_t totalLength = 64*1024*1024;
|
2009-06-28 10:37:15 +00:00
|
|
|
SharedHandle<DownloadContext> dctx
|
|
|
|
(new DownloadContext(pieceLength, totalLength, "aria2.tar.bz2"));
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &op));
|
2008-01-06 16:37:25 +00:00
|
|
|
|
|
|
|
SegmentMan segmentMan(&op, dctx, ps);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!segmentMan.getSegment(1, 0).isNull());
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<Segment> seg = segmentMan.getSegment(1, 1);
|
2008-01-06 16:37:25 +00:00
|
|
|
CPPUNIT_ASSERT(!seg.isNull());
|
|
|
|
CPPUNIT_ASSERT(!segmentMan.getSegment(1, 2).isNull());
|
|
|
|
|
|
|
|
seg->updateWrittenLength(pieceLength);
|
|
|
|
segmentMan.completeSegment(1, seg);
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<SharedHandle<Segment> > segments;
|
2008-05-11 10:46:52 +00:00
|
|
|
segmentMan.getInFlightSegment(segments, 1);
|
2008-01-06 16:37:25 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, segments.size());
|
2008-03-08 08:04:28 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, segments[0]->getIndex());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, segments[1]->getIndex());
|
2008-01-06 16:37:25 +00:00
|
|
|
}
|
|
|
|
|
2009-06-30 17:03:57 +00:00
|
|
|
void SegmentManTest::testGetSegment_sameFileEntry()
|
|
|
|
{
|
|
|
|
Option op;
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
dctx->setPieceLength(2);
|
|
|
|
SharedHandle<FileEntry> fileEntries[] = {
|
|
|
|
SharedHandle<FileEntry>(new FileEntry("file1", 3, 0)),
|
|
|
|
SharedHandle<FileEntry>(new FileEntry("file2", 6, 3)),
|
|
|
|
SharedHandle<FileEntry>(new FileEntry("file3", 1, 9))
|
|
|
|
};
|
|
|
|
dctx->setFileEntries(&fileEntries[0], &fileEntries[3]);
|
|
|
|
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &op));
|
|
|
|
SegmentMan segman(&op, dctx, ps);
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<SharedHandle<Segment> > segments;
|
2009-06-30 17:03:57 +00:00
|
|
|
segman.getSegment(segments, 1, fileEntries[1], 4);
|
|
|
|
// See 3 segments are returned, not 4 because the part of file1 is
|
|
|
|
// not filled in segment#1
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, segments.size());
|
|
|
|
|
|
|
|
SharedHandle<Segment> segmentNo1 = segman.getSegment(2, 1);
|
|
|
|
// Fill the part of file1 in segment#1
|
|
|
|
segmentNo1->updateWrittenLength(1);
|
|
|
|
segman.cancelSegment(2);
|
|
|
|
|
|
|
|
segman.cancelSegment(1);
|
|
|
|
segments.clear();
|
|
|
|
segman.getSegment(segments, 1, fileEntries[1], 4);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, segments.size());
|
|
|
|
|
|
|
|
segman.cancelSegment(1);
|
|
|
|
SharedHandle<Segment> segmentNo4 = segman.getSegment(1, 4);
|
|
|
|
// Fill the part of file2 in segment#4
|
|
|
|
segmentNo4->updateWrittenLength(1);
|
|
|
|
segman.cancelSegment(1);
|
|
|
|
|
|
|
|
segments.clear();
|
|
|
|
segman.getSegment(segments, 1, fileEntries[1], 4);
|
|
|
|
// segment#4 is not returned because the part of file2 is filled.
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, segments.size());
|
|
|
|
}
|
|
|
|
|
2009-07-02 16:26:04 +00:00
|
|
|
void SegmentManTest::testRegisterPeerStat()
|
|
|
|
{
|
|
|
|
Option op;
|
|
|
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
|
|
|
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &op));
|
|
|
|
SegmentMan segman(&op, dctx, ps);
|
|
|
|
|
|
|
|
SharedHandle<PeerStat> p1(new PeerStat(0, "host1", "http"));
|
|
|
|
segman.registerPeerStat(p1);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, segman.getPeerStats().size());
|
|
|
|
SharedHandle<PeerStat> p2(new PeerStat(0, "host2", "http"));
|
|
|
|
segman.registerPeerStat(p2);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, segman.getPeerStats().size());
|
|
|
|
p2->downloadStart();
|
|
|
|
segman.registerPeerStat(p1);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, segman.getPeerStats().size());
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|