Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
test: add unit test case for PowerClient.downloadPiece
Browse files Browse the repository at this point in the history
  • Loading branch information
PWZER committed Dec 2, 2020
1 parent 91d2b64 commit 788e97a
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions dfget/core/downloader/p2p_downloader/power_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ func (s *PowerClientTestSuite) TearDownTest(c *check.C) {
}

func (s *PowerClientTestSuite) TestDownloadPiece(c *check.C) {
// dstIP != pc.node && CheckConnect Fail
// dstIP != pc.node && pc.pieceTask.Path != pc.cfg.URL && CheckConnect Fail
s.powerClient.pieceTask.PeerIP = "127.0.0.2"
content, err := s.powerClient.downloadPiece()
c.Check(content, check.IsNil)
c.Check(err, check.NotNil)
s.reset()

// dstIP != pc.node && CheckConnect Success && Download Fail
// dstIP != pc.node && pc.pieceTask.Path != pc.cfg.URL && CheckConnect Success && Download Fail
s.powerClient.node = "127.0.0.2"
downloadMock = func() (*http.Response, error) {
return nil, fmt.Errorf("error")
Expand All @@ -75,6 +75,17 @@ func (s *PowerClientTestSuite) TestDownloadPiece(c *check.C) {
c.Check(err, check.DeepEquals, fmt.Errorf("error"))
s.reset()

// dstIP != pc.node && pc.pieceTask.Path == pc.cfg.URL && Download Fail
s.powerClient.node = "127.0.0.2"
s.powerClient.pieceTask.Path = s.powerClient.cfg.URL
downloadMock = func() (*http.Response, error) {
return nil, fmt.Errorf("error")
}
content, err = s.powerClient.downloadPiece()
c.Check(content, check.IsNil)
c.Check(err, check.DeepEquals, fmt.Errorf("error"))
s.reset()

// dstIP == pc.node && Download Success && StatusCode == 416
body1 := ioutil.NopCloser(bytes.NewReader([]byte("RangeNotSatisfiable")))
defer func() {
Expand Down Expand Up @@ -162,7 +173,7 @@ func (s *PowerClientTestSuite) TestReadBody(c *check.C) {

func (s *PowerClientTestSuite) reset() {
s.powerClient = &PowerClient{
cfg: &config.Config{RV: config.RuntimeVariable{Cid: ""}},
cfg: &config.Config{RV: config.RuntimeVariable{Cid: ""}, URL: "http://127.0.0.1/"},
node: "127.0.0.1",
rateLimiter: ratelimiter.NewRateLimiter(int64(5), 2),
downloadAPI: NewMockDownloadAPI(),
Expand Down

0 comments on commit 788e97a

Please sign in to comment.