From 788e97a258ebb3814ad83cdec2c837d2d3294e7e Mon Sep 17 00:00:00 2001 From: PWZER Date: Wed, 2 Dec 2020 14:32:01 +0800 Subject: [PATCH] test: add unit test case for PowerClient.downloadPiece --- .../p2p_downloader/power_client_test.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/dfget/core/downloader/p2p_downloader/power_client_test.go b/dfget/core/downloader/p2p_downloader/power_client_test.go index 55695214d..331213ce2 100644 --- a/dfget/core/downloader/p2p_downloader/power_client_test.go +++ b/dfget/core/downloader/p2p_downloader/power_client_test.go @@ -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") @@ -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() { @@ -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(),