@@ -278,58 +278,110 @@ void main() {
278
278
});
279
279
280
280
group ('pause, resume, cancel' , () {
281
- const size = 1024 * 1024 * 6 ;
282
- const chars = 'qwertyuiopasdfghjklzxcvbnm' ;
283
- final content = List .generate (size, (i) => chars[i % 25 ]).join ();
284
- testWidgets ('can pause' , (_) async {
285
- final fileId = uuid ();
286
- final path = 'public/upload-file-pause-$fileId ' ;
287
- final filePath = await createFile (path: fileId, content: content);
288
- addTearDownPath (StoragePath .fromString (path));
289
- final operation = Amplify .Storage .uploadFile (
290
- localFile: AWSFile .fromPath (filePath),
291
- path: StoragePath .fromString (path),
292
- );
293
- await operation.pause ();
294
- unawaited (
295
- operation.result.then (
296
- (value) => fail ('should not complete after pause' ),
297
- ),
281
+ /// file sizes in mb
282
+ const fileSizes = [
283
+ 1 , // non multi part upload
284
+ 6 , // small multi part upload (2 parts)
285
+ 50 , // large multi part upload (10 parts)
286
+ ];
287
+ for (final fileSize in fileSizes) {
288
+ final size = 1024 * 1024 * fileSize;
289
+ const chars = 'qwertyuiopasdfghjklzxcvbnm' ;
290
+ final content = List .generate (size, (i) => chars[i % 25 ]).join ();
291
+ testWidgets (
292
+ 'can pause (file size: $fileSize mb)' ,
293
+ (_) async {
294
+ final fileId = uuid ();
295
+ final path = 'public/upload-file-pause-$fileId ' ;
296
+ final filePath = await createFile (path: fileId, content: content);
297
+ StorageTransferState ? state;
298
+ addTearDownPath (StoragePath .fromString (path));
299
+ final operation = Amplify .Storage .uploadFile (
300
+ localFile: AWSFile .fromPath (filePath),
301
+ path: StoragePath .fromString (path),
302
+ onProgress: (progress) {
303
+ state = progress.state;
304
+ },
305
+ );
306
+ await operation.pause ();
307
+ // pause is only supported for multi part uploads (over 5 mb)
308
+ // calling .pause() should not throw, but the operation will not
309
+ // actually pause.
310
+ if (fileSize > 5 ) {
311
+ unawaited (
312
+ operation.result.then (
313
+ (value) => fail ('should not complete after pause' ),
314
+ ),
315
+ );
316
+ await Future <void >.delayed (const Duration (seconds: 15 ));
317
+ expect (state, StorageTransferState .paused);
318
+ await expectLater (
319
+ () => Amplify .Storage .downloadData (
320
+ path: StoragePath .fromString (path),
321
+ ).result,
322
+ throwsA (isA <StorageNotFoundException >()),
323
+ );
324
+ }
325
+ },
298
326
);
299
- await Future <void >.delayed (const Duration (seconds: 15 ));
300
- });
301
327
302
- testWidgets ('can resume' , (_) async {
303
- final fileId = uuid ();
304
- final path = 'public/upload-file-resume-$fileId ' ;
305
- final filePath = await createFile (path: fileId, content: content);
306
- addTearDownPath (StoragePath .fromString (path));
307
- final operation = Amplify .Storage .uploadFile (
308
- localFile: AWSFile .fromPath (filePath),
309
- path: StoragePath .fromString (path),
328
+ testWidgets (
329
+ 'can resume (file size: $fileSize mb)' ,
330
+ (_) async {
331
+ final fileId = uuid ();
332
+ final path = 'public/upload-file-resume-$fileId ' ;
333
+ final filePath = await createFile (path: fileId, content: content);
334
+ final state = StreamController <StorageTransferState >();
335
+ addTearDownPath (StoragePath .fromString (path));
336
+ final operation = Amplify .Storage .uploadFile (
337
+ localFile: AWSFile .fromPath (filePath),
338
+ path: StoragePath .fromString (path),
339
+ onProgress: (progress) {
340
+ state.sink.add (progress.state);
341
+ },
342
+ );
343
+ await operation.pause ();
344
+ await operation.resume ();
345
+ final nextProgressState = await state.stream.first;
346
+ expect (nextProgressState, StorageTransferState .inProgress);
347
+ final result = await operation.result;
348
+ expect (result.uploadedItem.path, path);
349
+ final downloadResult = await Amplify .Storage .downloadData (
350
+ path: StoragePath .fromString (path),
351
+ ).result;
352
+ expect (downloadResult.bytes, content.codeUnits);
353
+ await state.close ();
354
+ },
310
355
);
311
- await operation.pause ();
312
- await operation.resume ();
313
- final result = await operation.result;
314
- expect (result.uploadedItem.path, path);
315
- });
316
356
317
- testWidgets ('can cancel' , (_) async {
318
- final fileId = uuid ();
319
- final path = 'public/upload-file-cancel-$fileId ' ;
320
- final filePath = await createFile (path: fileId, content: content);
321
- addTearDownPath (StoragePath .fromString (path));
322
- final operation = Amplify .Storage .uploadFile (
323
- localFile: AWSFile .fromPath (filePath),
324
- path: StoragePath .fromString (path),
325
- );
326
- final expectException = expectLater (
327
- () => operation.result,
328
- throwsA (isA <StorageOperationCanceledException >()),
329
- );
330
- await operation.cancel ();
331
- await expectException;
332
- });
357
+ testWidgets ('can cancel (file size: $fileSize mb)' , (_) async {
358
+ final fileId = uuid ();
359
+ final path = 'public/upload-file-cancel-$fileId ' ;
360
+ final filePath = await createFile (path: fileId, content: content);
361
+ StorageTransferState ? state;
362
+ addTearDownPath (StoragePath .fromString (path));
363
+ final operation = Amplify .Storage .uploadFile (
364
+ localFile: AWSFile .fromPath (filePath),
365
+ path: StoragePath .fromString (path),
366
+ onProgress: (progress) {
367
+ state = progress.state;
368
+ },
369
+ );
370
+ final expectException = expectLater (
371
+ () => operation.result,
372
+ throwsA (isA <StorageOperationCanceledException >()),
373
+ );
374
+ await operation.cancel ();
375
+ expect (state, StorageTransferState .canceled);
376
+ await expectException;
377
+ await expectLater (
378
+ () => Amplify .Storage .downloadData (
379
+ path: StoragePath .fromString (path),
380
+ ).result,
381
+ throwsA (isA <StorageNotFoundException >()),
382
+ );
383
+ });
384
+ }
333
385
});
334
386
});
335
387
0 commit comments