Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huangnauh committed Oct 28, 2024
1 parent 9fa7c61 commit 4ab6bff
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 85 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/upyun/go-sdk/v3

go 1.13
go 1.17
4 changes: 2 additions & 2 deletions upyun/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -35,7 +35,7 @@ func checkResponse(res *http.Response) error {
uerr.StatusCode = res.StatusCode
uerr.Header = res.Header
defer res.Body.Close()
slurp, err := ioutil.ReadAll(res.Body)
slurp, err := io.ReadAll(res.Body)
if err != nil {
return uerr
}
Expand Down
3 changes: 1 addition & 2 deletions upyun/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
Expand Down Expand Up @@ -91,7 +90,7 @@ func (up *UpYun) FormUpload(config *FormUploadConfig) (*FormUploadResp, error) {
return nil, err
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()

if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions upyun/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"path"
"strings"
Expand Down Expand Up @@ -131,7 +131,7 @@ func (up *UpYun) doProcessRequest(method, uri string,
return errorOperation("process", err)
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return errorOperation("process read body", err)
Expand Down Expand Up @@ -215,7 +215,7 @@ func (up *UpYun) doSyncProcessRequest(method, uri string, payload string) (map[s
return nil, errorOperation("sync process", err)
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions upyun/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package upyun

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"path"
"testing"
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestNagaResult(t *testing.T) {
Equal(t, len(res), 2)
}

//由于是异步操作,不能确保文件已存在
// 由于是异步操作,不能确保文件已存在
func TestImgaudit(t *testing.T) {
task := map[string]interface{}{
"url": JPG_URL,
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestImgaudit(t *testing.T) {

}

//由于是异步操作,不能确保文件已存在
// 由于是异步操作,不能确保文件已存在
func TestVideoaudit(t *testing.T) {
task := map[string]interface{}{
"url": MP4_URL,
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestFaceDetect(t *testing.T) {
resp, err := http.Get(FACE_URL + "!/face/detection")
Nil(t, err)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
Nil(t, err)
if err != nil {
var result map[string]interface{}
Expand Down
4 changes: 2 additions & 2 deletions upyun/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package upyun

import (
"encoding/json"
"io/ioutil"
"io"
URL "net/url"
"strings"
"time"
Expand Down Expand Up @@ -33,7 +33,7 @@ func (up *UpYun) Purge(urls []string) (fails []string, err error) {
}
defer resp.Body.Close()

content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
if err != nil {
return fails, errorOperation("purge read body", err)
}
Expand Down
11 changes: 5 additions & 6 deletions upyun/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -532,7 +531,7 @@ func (up *UpYun) ListMultipartUploads(config *ListMultipartConfig) (*ListMultipa
return nil, errorOperation("list multipart", err)
}

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errorOperation("list multipart read body", err)
}
Expand Down Expand Up @@ -563,7 +562,7 @@ func (up *UpYun) ListMultipartParts(intiResult *InitMultipartUploadResult, confi
return nil, errorOperation("list multipart parts", err)
}

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errorOperation("list multipart parts read body", err)
}
Expand Down Expand Up @@ -688,7 +687,7 @@ func (up *UpYun) List(config *GetObjectsConfig) error {
return errorOperation("list", err)
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return errorOperation("list read body", err)
Expand Down Expand Up @@ -796,7 +795,7 @@ func (up *UpYun) ListObjects(config *ListObjectsConfig) (fileInfos []*FileInfo,
}

// 读取列表
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, "", errorOperation("list read body", err)
Expand Down Expand Up @@ -898,7 +897,7 @@ func (up *UpYun) doRESTRequest(config *restReqConfig) (*http.Response, error) {
}

if config.closeBody {
io.Copy(ioutil.Discard, resp.Body)
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}

Expand Down
123 changes: 60 additions & 63 deletions upyun/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package upyun
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"path"
Expand Down Expand Up @@ -123,26 +122,26 @@ func TestCopyMove(t *testing.T) {
}

/*
func TestPutWithBufferAppend(t *testing.T) {
s := BUF_CONTENT
for k := 0; k < 3; k++ {
r := strings.NewReader(s)
err := up.Put(&PutObjectConfig{
Path: REST_FILE_BUF_BUF,
Reader: r,
Headers: map[string]string{
"Content-Length": fmt.Sprint(len(s)),
},
AppendContent: true,
UseMD5: true,
})
if k != 0 {
NotNil(t, err)
} else {
Nil(t, err)
func TestPutWithBufferAppend(t *testing.T) {
s := BUF_CONTENT
for k := 0; k < 3; k++ {
r := strings.NewReader(s)
err := up.Put(&PutObjectConfig{
Path: REST_FILE_BUF_BUF,
Reader: r,
Headers: map[string]string{
"Content-Length": fmt.Sprint(len(s)),
},
AppendContent: true,
UseMD5: true,
})
if k != 0 {
NotNil(t, err)
} else {
Nil(t, err)
}
}
}
}
*/
func testMultiUpload(t *testing.T, key string, data []byte, partSize int64, parts []int, completed bool) *InitMultipartUploadResult {
uploadResult, err := up.InitMultipartUpload(&InitMultipartUploadConfig{
Expand Down Expand Up @@ -218,6 +217,7 @@ func TestResumePut(t *testing.T) {
Nil(t, err)
defer fd.Close()
defer os.RemoveAll(fname)
fd.Seek(0, 0)

now := time.Now()
path := REST_FILE_1M
Expand Down Expand Up @@ -253,23 +253,25 @@ func TestResumePut(t *testing.T) {
testPoint := &BreakPointConfig{UploadID: result.UploadID, PartSize: result.PartSize,
FileSize: fileInfo.Size(), FileModTime: fileInfo.ModTime(), LastTime: now}

// imitate upload some part and failed after testBreak part
for i := 0; i <= testBreak; i++ {
fragFile, err := newFragmentFile(fd, curSize, DefaultPartSize)
Nil(t, err)
ch := make(chan *Chunk, 1)
go GetReadChunk(fd, fileInfo.Size(), DefaultPartSize, ch)
// initate upload some part and failed after testBreak part
for chunk := range ch {
err = up.UploadPart(result, &UploadPartConfig{
Reader: fragFile,
PartSize: DefaultPartSize,
PartID: i,
Reader: chunk,
PartSize: int64(chunk.Len()),
PartID: chunk.ID(),
})
Nil(t, err)
Nilf(t, err, fmt.Sprintf("chunk %d %d", chunk.ID(), chunk.Len()))
testPoint.PartID = chunk.ID() + 1
curSize += int64(chunk.Len())
res, err := up.GetResumeProcess(result.Path)
Nil(t, err)
Equal(t, int64(i+1), res.NextPartID)

curSize += DefaultPartSize
testPoint.PartID = i + 1
Equal(t, int64(testPoint.PartID), res.NextPartID)
resSize += res.NextPartSize
if testBreak == chunk.ID() {
break
}
}
Equal(t, curSize, resSize)

Expand Down Expand Up @@ -365,10 +367,10 @@ func TestGetWithLocalPath(t *testing.T) {
_, err = os.Stat(LOCAL_SAVE_FILE)
Nil(t, err)

b1, err := ioutil.ReadFile(LOCAL_FILE)
b1, err := os.ReadFile(LOCAL_FILE)
Nil(t, err)

b2, err := ioutil.ReadFile(LOCAL_SAVE_FILE)
b2, err := os.ReadFile(LOCAL_SAVE_FILE)
Nil(t, err)

Equal(t, string(b1), string(b2))
Expand Down Expand Up @@ -536,6 +538,7 @@ func TestResumeUpload(t *testing.T) {
Nil(t, err)
defer os.RemoveAll(fname)
defer fd.Close()
fd.Seek(0, 0)

path := REST_FILE_1M
config := &PutObjectConfig{
Expand Down Expand Up @@ -565,28 +568,28 @@ func TestResumeUpload(t *testing.T) {
maxPartID := int((fileInfo.Size()+result.PartSize-1)/result.PartSize - 1)
now := time.Now()

// imitate break part
// initate break part
testBreak := 5

var curSize int64 = 0
ch := make(chan *Chunk, 1)
go GetReadChunk(fd, fileInfo.Size(), DefaultPartSize, ch)
testPoint := &BreakPointConfig{UploadID: result.UploadID}
for i := 0; i <= testBreak; i++ {
fragFile, err := newFragmentFile(fd, curSize, DefaultPartSize)
Nil(t, err)
for chunk := range ch {
err = up.UploadPart(result, &UploadPartConfig{
Reader: fragFile,
PartSize: DefaultPartSize,
PartID: i,
Reader: chunk,
PartSize: int64(chunk.Len()),
PartID: chunk.ID(),
})
Nil(t, err)
curSize += DefaultPartSize
testPoint.PartID = i + 1
testPoint.PartID = chunk.ID() + 1
if testBreak == chunk.ID() {
break
}
}
tests := []struct {
BreakPointConfig
expected BreakPointConfig
}{
// imitate failed in upload part stage
// initate failed in upload part stage
{
BreakPointConfig{
UploadID: result.UploadID, PartID: testBreak + 1, PartSize: result.PartSize,
Expand All @@ -610,7 +613,7 @@ func TestResumeUpload(t *testing.T) {
},
}

// imitate breakPoint
// initate breakPoint
for _, test := range tests {
point := test.BreakPointConfig
// resume upload
Expand Down Expand Up @@ -646,6 +649,7 @@ func TestGetDisorderResumeProcess(t *testing.T) {
Nil(t, err)
defer fd.Close()
defer os.RemoveAll(fname)
fd.Seek(0, 0)

path := "Disorder.txt"

Expand Down Expand Up @@ -673,33 +677,26 @@ func TestGetDisorderResumeProcess(t *testing.T) {
})
Nil(t, err)

// imitate upload part failed
testBreak := 10
// initate upload part failed
var curSize int64 = 0
fileSize := fileInfo.Size()

// imitate upload some part and failed after testBreak part
for i := 0; i <= testBreak; i++ {
fragFile, err := newFragmentFile(fd, curSize, DefaultPartSize)
Nil(t, err)
partSize := int64(DefaultPartSize)
res := fileSize - curSize
if res < DefaultPartSize {
partSize = res
}
ch := make(chan *Chunk, 1)
go GetReadChunk(fd, fileSize, DefaultPartSize, ch)
// initate upload some part and failed after testBreak part
for chunk := range ch {
err = up.UploadPart(result, &UploadPartConfig{
Reader: fragFile,
PartSize: partSize,
PartID: i,
Reader: chunk,
PartSize: int64(chunk.Len()),
PartID: chunk.ID(),
})
Nil(t, err)

curSize += partSize
curSize += int64(chunk.Len())
}
resp, err := up.GetResumeProcess(result.Path)

Nil(t, err)
Equal(t, testBreak+1, len(resp.Parts))
Equal(t, 11, len(resp.Parts))
Equal(t, curSize, fileSize)
err = up.CompleteMultipartUpload(result, &CompleteMultipartUploadConfig{})
Nil(t, err)
Expand Down
Loading

0 comments on commit 4ab6bff

Please sign in to comment.