Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1291 from fenggw-fnst/work
Browse files Browse the repository at this point in the history
test: add unit test case for super_reader.go
  • Loading branch information
lowzj authored Apr 19, 2020
2 parents efbf460 + 58e82bb commit 4c2af2b
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions supernode/daemon/mgr/cdn/super_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package cdn

import (
"bytes"
"context"
"crypto/md5"
"encoding/binary"
"fmt"

"github.com/dragonflyoss/Dragonfly/pkg/fileutils"

"github.com/dragonflyoss/Dragonfly/supernode/config"
"github.com/go-check/check"
)

Expand All @@ -36,7 +38,33 @@ func init() {
check.Suite(&SuperReaderTestSuite{})
}

// TODO: add more unit tests
func (s *SuperReaderTestSuite) TestReadFile(c *check.C) {
testStr1 := []byte("hello ")
testStr2 := []byte("dragonfly")
testPiece1 := append(append([]byte{0, 0, 0, 6}, testStr1...), 0x7f)
testPiece2 := append(append([]byte{0, 0, 0, 9}, testStr2...), 0x7f)
testStr := append(testPiece1, testPiece2...)

contentBuf := &bytes.Buffer{}
binary.Write(contentBuf, binary.BigEndian, testStr)

cacheReader := newSuperReader()
result, err := cacheReader.readFile(context.Background(), contentBuf, true, true)

c.Check(err, check.IsNil)
c.Check(int64(len(testStr)), check.Equals, result.fileLength)
c.Check(2, check.Equals, result.pieceCount)
md5Init := md5.New()
md5Init.Write(testPiece1)
c.Check(fmt.Sprintf("%s:%d", fileutils.GetMd5Sum(md5Init, nil), len(testStr1)+config.PieceWrapSize), check.Equals, result.pieceMd5s[0])
md5Init.Reset()
md5Init.Write(testPiece2)
c.Check(fmt.Sprintf("%s:%d", fileutils.GetMd5Sum(md5Init, nil), len(testStr2)+config.PieceWrapSize), check.Equals, result.pieceMd5s[1])
md5Init.Reset()
md5Init.Write(testStr1)
md5Init.Write(testStr2)
c.Check(fileutils.GetMd5Sum(md5Init, nil), check.Equals, fileutils.GetMd5Sum(result.fileMd5, nil))
}

func (s *SuperReaderTestSuite) TestGetMD5ByReadFile(c *check.C) {
testStr := []byte("hello dragonfly")
Expand Down

0 comments on commit 4c2af2b

Please sign in to comment.