| 1 |
# -*- encoding: utf-8 -*- |
| 2 |
|
| 3 |
require 'spec_helper' |
| 4 |
require 'yaml' |
| 5 |
require 'nokogiri' |
| 6 |
|
| 7 |
module Aliyun |
| 8 |
module OSS |
| 9 |
|
| 10 |
describe "Resumable download" do |
| 11 |
|
| 12 |
before :all do |
| 13 |
@endpoint = 'oss-cn-hangzhou.aliyuncs.com' |
| 14 |
@bucket_name = 'rubysdk-bucket' |
| 15 |
@object_key = 'resumable_file' |
| 16 |
@bucket = Client.new( |
| 17 |
:endpoint => @endpoint, |
| 18 |
:access_key_id => 'xxx', |
| 19 |
:access_key_secret => 'yyy').get_bucket(@bucket_name) |
| 20 |
|
| 21 |
@file = './download_file' |
| 22 |
end |
| 23 |
|
| 24 |
before :each do |
| 25 |
File.delete("#{@file}.cpt") if File.exist?("#{@file}.cpt") |
| 26 |
end |
| 27 |
|
| 28 |
def object_url |
| 29 |
"#{@bucket_name}.#{@endpoint}/#{@object_key}" |
| 30 |
end |
| 31 |
|
| 32 |
def mock_object(i) |
| 33 |
i.to_s.rjust(9, '0') + "\n" |
| 34 |
end |
| 35 |
|
| 36 |
def mock_range(i) |
| 37 |
"bytes=#{(i-1)*10}-#{i*10 - 1}" |
| 38 |
end |
| 39 |
|
| 40 |
def mock_error(code, message) |
| 41 |
Nokogiri::XML::Builder.new do |xml| |
| 42 |
xml.Error { |
| 43 |
xml.Code code |
| 44 |
xml.Message message |
| 45 |
xml.RequestId '0000' |
| 46 |
} |
| 47 |
end.to_xml |
| 48 |
end |
| 49 |
|
| 50 |
it "should download file when all goes well" do |
| 51 |
return_headers = { |
| 52 |
'x-oss-object-type' => 'Normal', |
| 53 |
'ETag' => 'xxxyyyzzz', |
| 54 |
'Content-Length' => 100, |
| 55 |
'Last-Modified' => Time.now.rfc822 |
| 56 |
} |
| 57 |
|
| 58 |
# get object meta |
| 59 |
stub_request(:head, object_url).to_return(:headers => return_headers) |
| 60 |
|
| 61 |
# get object by range |
| 62 |
stub_request(:get, object_url) |
| 63 |
.to_return((1..10).map{ |i| {:body => mock_object(i)} }) |
| 64 |
|
| 65 |
prg = [] |
| 66 |
@bucket.resumable_download( |
| 67 |
@object_key, @file, :part_size => 10) { |p| prg << p } |
| 68 |
|
| 69 |
ranges = [] |
| 70 |
expect(WebMock).to have_requested(:get, object_url).with{ |req| |
| 71 |
ranges << req.headers['Range'] |
| 72 |
}.times(10) |
| 73 |
|
| 74 |
expect(ranges).to match_array((1..10).map{ |i| mock_range(i) }) |
| 75 |
expect(File.exist?("#{@file}.cpt")).to be false |
| 76 |
expect(Dir.glob("#{@file}.part.*").empty?).to be true |
| 77 |
|
| 78 |
expect(File.read(@file).lines) |
| 79 |
.to match_array((1..10).map{ |i| mock_object(i) }) |
| 80 |
expect(prg.size).to eq(10) |
| 81 |
end |
| 82 |
|
| 83 |
it "should resume when download part fails" do |
| 84 |
return_headers = { |
| 85 |
'x-oss-object-type' => 'Normal', |
| 86 |
'ETag' => 'xxxyyyzzz', |
| 87 |
'Content-Length' => 100, |
| 88 |
'Last-Modified' => Time.now.rfc822 |
| 89 |
} |
| 90 |
|
| 91 |
# get object meta |
| 92 |
stub_request(:head, object_url).to_return(:headers => return_headers) |
| 93 |
|
| 94 |
code = 'Timeout' |
| 95 |
message = 'Request timeout.' |
| 96 |
# upload part |
| 97 |
stub_request(:get, object_url) |
| 98 |
.to_return((1..3).map{ |i| {:body => mock_object(i)} }).then |
| 99 |
.to_return(:status => 500, :body => mock_error(code, message)).times(2).then |
| 100 |
.to_return((4..9).map{ |i| {:body => mock_object(i)} }).then |
| 101 |
.to_return(:status => 500, :body => mock_error(code, message)).then |
| 102 |
.to_return((10..10).map{ |i| {:body => mock_object(i)} }) |
| 103 |
|
| 104 |
success = false |
| 105 |
4.times do |
| 106 |
begin |
| 107 |
@bucket.resumable_download( |
| 108 |
@object_key, @file, :part_size => 10, :threads => 1) |
| 109 |
success = true |
| 110 |
rescue |
| 111 |
# pass |
| 112 |
end |
| 113 |
end |
| 114 |
|
| 115 |
expect(success).to be true |
| 116 |
ranges = [] |
| 117 |
expect(WebMock).to have_requested(:get, object_url).with{ |req| |
| 118 |
ranges << req.headers['Range'] |
| 119 |
}.times(13) |
| 120 |
|
| 121 |
expect(ranges.uniq).to match_array((1..10).map{ |i| mock_range(i) }) |
| 122 |
expect(File.read(@file)).to eq((1..10).map{ |i| mock_object(i) }.join) |
| 123 |
end |
| 124 |
|
| 125 |
it "should resume when checkpoint fails" do |
| 126 |
# Monkey patch to inject failures |
| 127 |
class ::Aliyun::OSS::Multipart::Download |
| 128 |
alias :old_checkpoint :checkpoint |
| 129 |
|
| 130 |
def checkpoint_fails |
| 131 |
@@fail_injections ||= [false, false, true, true, false, true, false] |
| 132 |
@@fail_injections.shift |
| 133 |
end |
| 134 |
|
| 135 |
def checkpoint |
| 136 |
t = checkpoint_fails |
| 137 |
if t == true |
| 138 |
raise ClientError.new("fail injection") |
| 139 |
end |
| 140 |
|
| 141 |
old_checkpoint |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
return_headers = { |
| 146 |
'x-oss-object-type' => 'Normal', |
| 147 |
'ETag' => 'xxxyyyzzz', |
| 148 |
'Content-Length' => 100, |
| 149 |
'Last-Modified' => Time.now.rfc822 |
| 150 |
} |
| 151 |
|
| 152 |
# get object meta |
| 153 |
stub_request(:head, object_url).to_return(:headers => return_headers) |
| 154 |
|
| 155 |
# get object by range |
| 156 |
returns = [1, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 157 |
stub_request(:get, object_url) |
| 158 |
.to_return(returns.map{ |i| {:body => mock_object(i)} }) |
| 159 |
|
| 160 |
success = false |
| 161 |
4.times do |
| 162 |
begin |
| 163 |
@bucket.resumable_download( |
| 164 |
@object_key, @file, :part_size => 10, :threads => 1) |
| 165 |
success = true |
| 166 |
rescue |
| 167 |
# pass |
| 168 |
end |
| 169 |
end |
| 170 |
|
| 171 |
expect(success).to be true |
| 172 |
expect(WebMock).to have_requested(:get, object_url).times(13) |
| 173 |
expect(File.read(@file)).to eq((1..10).map{ |i| mock_object(i) }.join) |
| 174 |
end |
| 175 |
|
| 176 |
it "should not resume when specify disable_cpt" do |
| 177 |
return_headers = { |
| 178 |
'x-oss-object-type' => 'Normal', |
| 179 |
'ETag' => 'xxxyyyzzz', |
| 180 |
'Content-Length' => 100, |
| 181 |
'Last-Modified' => Time.now.rfc822 |
| 182 |
} |
| 183 |
|
| 184 |
# get object meta |
| 185 |
stub_request(:head, object_url).to_return(:headers => return_headers) |
| 186 |
|
| 187 |
code = 'Timeout' |
| 188 |
message = 'Request timeout.' |
| 189 |
# upload part |
| 190 |
stub_request(:get, object_url) |
| 191 |
.to_return((1..3).map{ |i| {:body => mock_object(i)} }).then |
| 192 |
.to_return(:status => 500, :body => mock_error(code, message)).times(2).then |
| 193 |
.to_return((1..9).map{ |i| {:body => mock_object(i)} }).then |
| 194 |
.to_return(:status => 500, :body => mock_error(code, message)).then |
| 195 |
.to_return((1..10).map{ |i| {:body => mock_object(i)} }) |
| 196 |
|
| 197 |
cpt_file = "#{File.expand_path(@file)}.cpt" |
| 198 |
success = false |
| 199 |
4.times do |
| 200 |
begin |
| 201 |
@bucket.resumable_download( |
| 202 |
@object_key, @file, :part_size => 10, |
| 203 |
:cpt_file => cpt_file, :disable_cpt => true, :threads => 1) |
| 204 |
success = true |
| 205 |
rescue |
| 206 |
# pass |
| 207 |
end |
| 208 |
|
| 209 |
expect(File.exists?(cpt_file)).to be false |
| 210 |
end |
| 211 |
|
| 212 |
expect(success).to be true |
| 213 |
expect(WebMock).to have_requested(:get, object_url).times(25) |
| 214 |
expect(File.read(@file)).to eq((1..10).map{ |i| mock_object(i) }.join) |
| 215 |
expect(Dir.glob("#{@file}.part.*").empty?).to be true |
| 216 |
end |
| 217 |
|
| 218 |
end # Resumable upload |
| 219 |
end # OSS |
| 220 |
end # Aliyun |