@@ -564,6 +564,7 @@ func TestObjectService_Upload2(t *testing.T) {
564564 }
565565}
566566
567+ /*
567568func TestObjectService_Download(t *testing.T) {
568569 setup()
569570 defer teardown()
@@ -636,7 +637,97 @@ func TestObjectService_Download(t *testing.T) {
636637 t.Fatalf("Object.Upload returned error: %v", err)
637638 }
638639}
640+ */
641+ func TestObjectService_DownloadWithCheckPoint (t * testing.T ) {
642+ setup ()
643+ defer teardown ()
644+
645+ filePath := "rsp.file" + time .Now ().Format (time .RFC3339 )
646+ newfile , err := os .Create (filePath )
647+ if err != nil {
648+ t .Fatalf ("create tmp file failed" )
649+ }
650+ defer os .Remove (filePath )
651+ // 源文件内容
652+ totalBytes := int64 (1024 * 1024 * 9 + 123 )
653+ partSize := 1024 * 1024
654+ b := make ([]byte , totalBytes )
655+ _ , err = rand .Read (b )
656+ newfile .Write (b )
657+ newfile .Close ()
658+ tb := crc64 .MakeTable (crc64 .ECMA )
659+ localcrc := strconv .FormatUint (crc64 .Update (0 , tb , b ), 10 )
639660
661+ oddok := false
662+ var oddcount , evencount int
663+ mux .HandleFunc ("/test.go.download" , func (w http.ResponseWriter , r * http.Request ) {
664+ if r .Method == http .MethodHead {
665+ w .Header ().Add ("Content-Length" , strconv .FormatInt (totalBytes , 10 ))
666+ w .Header ().Add ("x-cos-hash-crc64ecma" , localcrc )
667+ return
668+ }
669+ strRange := r .Header .Get ("Range" )
670+ slice1 := strings .Split (strRange , "=" )
671+ slice2 := strings .Split (slice1 [1 ], "-" )
672+ start , _ := strconv .ParseInt (slice2 [0 ], 10 , 64 )
673+ end , _ := strconv .ParseInt (slice2 [1 ], 10 , 64 )
674+ if (start / int64 (partSize ))% 2 == 1 {
675+ if oddok {
676+ io .Copy (w , bytes .NewBuffer (b [start :end + 1 ]))
677+ } else {
678+ // 数据校验失败, Download不会做重试
679+ io .Copy (w , bytes .NewBuffer (b [start :end ]))
680+ }
681+ oddcount ++
682+ } else {
683+ io .Copy (w , bytes .NewBuffer (b [start :end + 1 ]))
684+ evencount ++
685+ }
686+ })
687+
688+ opt := & MultiDownloadOptions {
689+ ThreadPoolSize : 3 ,
690+ PartSize : 1 ,
691+ CheckPoint : true ,
692+ }
693+ downPath := "down.file" + time .Now ().Format (time .RFC3339 )
694+ defer os .Remove (downPath )
695+ _ , err = client .Object .Download (context .Background (), "test.go.download" , downPath , opt )
696+ if err == nil {
697+ // 偶数块下载完成,奇数块下载失败
698+ t .Fatalf ("Object.Download returned error: %v" , err )
699+ }
700+ fd , err := os .Open (downPath )
701+ if err != nil {
702+ t .Fatalf ("Object Download Open File Failed:%v" , err )
703+ }
704+ offset := 0
705+ for i := 0 ; i < 10 ; i ++ {
706+ bs , _ := ioutil .ReadAll (io .LimitReader (fd , int64 (partSize )))
707+ offset += len (bs )
708+ if i % 2 == 1 {
709+ bs [len (bs )- 1 ] = b [offset - 1 ]
710+ }
711+ if bytes .Compare (bs , b [i * partSize :offset ]) != 0 {
712+ t .Fatalf ("Compare Error, index:%v, len:%v, offset:%v" , i , len (bs ), offset )
713+ }
714+ }
715+ fd .Close ()
716+
717+ if oddcount != 5 || evencount != 5 {
718+ t .Fatalf ("Object.Download failed, odd:%v, even:%v" , oddcount , evencount )
719+ }
720+ // 设置奇数块OK
721+ oddok = true
722+ _ , err = client .Object .Download (context .Background (), "test.go.download" , downPath , opt )
723+ if err != nil {
724+ // 下载成功
725+ t .Fatalf ("Object.Download returned error: %v" , err )
726+ }
727+ if oddcount != 10 || evencount != 5 {
728+ t .Fatalf ("Object.Download failed, odd:%v, even:%v" , oddcount , evencount )
729+ }
730+ }
640731func TestObjectService_GetTagging (t * testing.T ) {
641732 setup ()
642733 defer teardown ()
@@ -666,7 +757,7 @@ func TestObjectService_GetTagging(t *testing.T) {
666757 t .Fatalf ("Object.GetTagging returned error %v" , err )
667758 }
668759
669- want := & ObjectGetTaggingResult {
760+ want := & ObjectGetTaggingResult {
670761 XMLName : xml.Name {Local : "Tagging" },
671762 TagSet : []ObjectTaggingTag {
672763 {"test_k2" , "test_v2" },
@@ -735,7 +826,7 @@ func TestObjectService_DeleteTagging(t *testing.T) {
735826 w .WriteHeader (http .StatusNoContent )
736827 })
737828
738- _ , err := client .Object .DeleteTagging (context .Background (), "test" )
829+ _ , err := client .Object .DeleteTagging (context .Background (), "test" )
739830 if err != nil {
740831 t .Fatalf ("Object.DeleteTagging returned error: %v" , err )
741832 }
0 commit comments