Skip to content

Commit

Permalink
fix(firebase_storage): Fix UploadTask.cancel() so that it completes…
Browse files Browse the repository at this point in the history
… when called. (#8417)
  • Loading branch information
russellwheatley committed Apr 7, 2022
1 parent 0df32f6 commit 19ee62c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 50 deletions.
Expand Up @@ -35,7 +35,6 @@ class FlutterFirebaseStorageTask {
private final StorageMetadata metadata;
private final Object pauseSyncObject = new Object();
private final Object resumeSyncObject = new Object();
private final Object cancelSyncObject = new Object();
private StorageTask<?> storageTask;
private Boolean destroyed = false;

Expand Down Expand Up @@ -152,10 +151,6 @@ void destroy() {
}
}

synchronized (cancelSyncObject) {
cancelSyncObject.notifyAll();
}

synchronized (pauseSyncObject) {
pauseSyncObject.notifyAll();
}
Expand Down Expand Up @@ -200,20 +195,7 @@ Task<Boolean> resume() {
}

Task<Boolean> cancel() {
return Tasks.call(
FlutterFirebasePlugin.cachedThreadPool,
() -> {
synchronized (cancelSyncObject) {
boolean canceled = storageTask.cancel();
if (!canceled) return false;
try {
cancelSyncObject.wait();
} catch (InterruptedException e) {
return false;
}
return true;
}
});
return Tasks.call(FlutterFirebasePlugin.cachedThreadPool, () -> storageTask.cancel());
}

void startTaskWithMethodChannel(@NonNull MethodChannel channel) throws Exception {
Expand Down
7 changes: 6 additions & 1 deletion tests/test_driver/driver_e2e.dart
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:drive/drive.dart' as drive;
import 'package:flutter/foundation.dart';
import 'cloud_functions/cloud_functions_e2e.dart' as cloud_functions;
import 'firebase_auth/firebase_auth_e2e.dart' as firebase_auth;
import 'firebase_core/firebase_core_e2e.dart' as firebase_core;
Expand All @@ -22,14 +23,18 @@ import 'firebase_ml_model_downloader/firebase_ml_model_downloader_e2e.dart'
as firebase_ml_model_downloader;
import 'firebase_remote_config/firebase_remote_config_e2e.dart'
as firebase_remote_config;
import 'firebase_storage/firebase_storage_e2e.dart' as firebase_storage;

void setupTests() {
// Core first.
firebase_core.setupTests();
// All other tests.
firebase_auth.setupTests();
cloud_functions.setupTests();
// firebase_storage.setupTests();
if (defaultTargetPlatform == TargetPlatform.android || kIsWeb) {
// TODO(russellwheatley): Pending release of latest firebase-tools with Storage emulator fix.
firebase_storage.setupTests();
}
firebase_database.setupTests();
firebase_app_check.setupTests();
firebase_messaging.setupTests();
Expand Down
2 changes: 0 additions & 2 deletions tests/test_driver/firebase_storage/instance_e2e.dart
Expand Up @@ -15,8 +15,6 @@ void setupInstanceTests() {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await FirebaseStorage.instance
.useStorageEmulator(testEmulatorHost, testEmulatorPort);
storage = FirebaseStorage.instance;
secondaryApp = await testInitializeSecondaryApp();
});
Expand Down
61 changes: 33 additions & 28 deletions tests/test_driver/firebase_storage/reference_e2e.dart
Expand Up @@ -220,41 +220,46 @@ void setupReferenceTests() {
expect(result.prefixes.length, greaterThan(0));
});

group('putData', () {
test('uploads a file with buffer', () async {
List<int> list = utf8.encode(kTestString);
group(
'putData',
() {
test('uploads a file with buffer', () async {
List<int> list = utf8.encode(kTestString);

Uint8List data = Uint8List.fromList(list);
Uint8List data = Uint8List.fromList(list);

final Reference ref = storage.ref('flutter-tests').child('flt-ok.txt');
final Reference ref =
storage.ref('flutter-tests').child('flt-ok.txt');

final TaskSnapshot complete = await ref.putData(
data,
SettableMetadata(
contentLanguage: 'en',
),
);
final TaskSnapshot complete = await ref.putData(
data,
SettableMetadata(
contentLanguage: 'en',
),
);

expect(complete.metadata?.size, kTestString.length);
// Metadata isn't saved on objects when using the emulator which fails test
// expect(complete.metadata?.contentLanguage, 'en');
});
expect(complete.metadata?.size, kTestString.length);
// Metadata isn't saved on objects when using the emulator which fails test
// expect(complete.metadata?.contentLanguage, 'en');
});

//TODO(pr-mais): causes the emulator to crash
// test('errors if permission denied', () async {
// List<int> list = utf8.encode('hello world');
// Uint8List data = Uint8List.fromList(list);
//TODO(pr-mais): causes the emulator to crash
// test('errors if permission denied', () async {
// List<int> list = utf8.encode('hello world');
// Uint8List data = Uint8List.fromList(list);

// final Reference ref = storage.ref('/uploadNope.jpeg');
// final Reference ref = storage.ref('/uploadNope.jpeg');

// await expectLater(
// () => ref.putData(data),
// throwsA(isA<FirebaseException>()
// .having((e) => e.code, 'code', 'unauthorized')
// .having((e) => e.message, 'message',
// 'User is not authorized to perform the desired action.')));
// });
});
// await expectLater(
// () => ref.putData(data),
// throwsA(isA<FirebaseException>()
// .having((e) => e.code, 'code', 'unauthorized')
// .having((e) => e.message, 'message',
// 'User is not authorized to perform the desired action.')));
// });
},
skip: kIsWeb,
);

group('putBlob', () {
test(
Expand Down

0 comments on commit 19ee62c

Please sign in to comment.