App-MtAws

 view release on metacpan or  search on metacpan

t/lib/UploadMultipartTest.pm  view on Meta::CPAN

#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package UploadMultipartTest;

use strict;
use warnings;
use Test::Deep;
use App::MtAws::QueueJobResult;
use App::MtAws::QueueJob::UploadMultipart;
use App::MtAws::TreeHash;
use QueueHelpers;


sub expect_upload_multipart
{
	my ($j, $mtime, $partsize, $relfilename, $upload_id, %args_opts) = @_;

	my %args = (%args_opts);

	# TODO: also test that it works with mtime=0

	no warnings 'redefine';

	my $stdin_state = undef;

	local *App::MtAws::QueueJob::MultipartCreate::init_file = sub {
		$stdin_state = $_[0]->{stdin};
		$_[0]->{fh} = 'filehandle';
		$_[0]->{mtime} = $mtime;
	};

	local *App::MtAws::QueueJob::MultipartPart::close_file = sub {};

	cmp_deeply my $create_resp = $j->next, App::MtAws::QueueJobResult->full_new(code => JOB_OK, task => {
		args => { partsize => $partsize, mtime => $mtime, relfilename => $relfilename},
		action => 'create_upload', cb=> test_coderef, cb_task_proxy => test_coderef
	});

	expect_wait($j);

	$create_resp->{task}{cb_task_proxy}->({upload_id => $upload_id});

	my $n = 5;
	my @orig_parts = map { [$_*10, "hash $_", \"file $_"] } (1..$n);
	my @parts = @orig_parts;

	no warnings 'redefine';
	local *App::MtAws::QueueJob::MultipartPart::read_part = sub {
		my $p = shift @parts;
		if ($p) {
			shift->{position} += $partsize;
			return (1, @$p)
		} else {
			return;
		}
	};

	my @callbacks;
	for (@orig_parts) {
		my $res = $j->next;
		cmp_deeply $res,
			App::MtAws::QueueJobResult->full_new(
				task => {
					args => {
						start => $_->[0],
						upload_id => $upload_id,
						part_final_hash => $_->[1],
						relfilename => $relfilename,
						mtime => $mtime,
					},
					attachment => $_->[2],
					action => 'upload_part',
					cb => test_coderef,
					cb_task_proxy => test_coderef,
				},
				code => JOB_OK,
			);
		push @callbacks, $res->{task}{cb_task_proxy};
	}

	local *App::MtAws::TreeHash::calc_tree = sub { shift->{tree} = "my_final_hash" };
	local *App::MtAws::TreeHash::get_final_hash = sub { shift->{tree} };

	while (my $cb = shift @callbacks) {
		$cb->();
		if (@callbacks) {
			expect_wait($j);
		} else {
			cmp_deeply my $finish_resp = $j->next,
				App::MtAws::QueueJobResult->full_new(
					task => {
						args => {
							filesize => $n*$partsize,
							upload_id => $upload_id,
							relfilename => $relfilename,
							final_hash => 'my_final_hash',
							mtime => $mtime,
						},
						action => 'finish_upload',
						cb => test_coderef,
						cb_task_proxy => test_coderef,
					},
					code => JOB_OK,
				);
			call_callback($finish_resp);
			last;
		}
	}

	cmp_deeply $stdin_state, bool($args{expect_stdin}) if defined $args{expect_stdin};
}


1;



( run in 1.200 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )