Skip to content

Commit 9e46a62

Browse files
authored
Added storage integration tests for testing custom callback (#5695)
* Added storage integration tests for testing custom callback * Resolved Feedback 1.0 * Switched everything back to using dispatch specifics * Resolved nit
1 parent 4f0196b commit 9e46a62

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

FirebaseStorage/Tests/Integration/FIRStorageIntegrationTests.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,38 @@ - (void)testGetDataInBackgroundQueue {
413413
[self waitForExpectations];
414414
}
415415

416+
- (void)testGetDataWithCustomCallbackQueue {
417+
XCTestExpectation *expectation =
418+
[self expectationWithDescription:@"testUnauthenticatedGetDataInCustomCallbackQueue"];
419+
420+
NSString *callbackQueueLabelString = @"customCallbackQueue";
421+
const char *callbackQueueLabel = [callbackQueueLabelString UTF8String];
422+
const void *callbackQueueKey = callbackQueueLabel;
423+
dispatch_queue_t callbackQueue = dispatch_queue_create(callbackQueueLabel, NULL);
424+
425+
dispatch_queue_set_specific(callbackQueue, callbackQueueKey, (void *)callbackQueueKey, NULL);
426+
_storage.callbackQueue = callbackQueue;
427+
428+
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"];
429+
[ref dataWithMaxSize:1 * 1024 * 1024
430+
completion:^(NSData *data, NSError *error) {
431+
XCTAssertNotNil(data, "Data should not be nil");
432+
XCTAssertNil(error, "Error should be nil");
433+
434+
char *currentQueueLabel = dispatch_get_specific(callbackQueueKey);
435+
NSString *currentQueueLabelString = [NSString stringWithUTF8String:currentQueueLabel];
436+
XCTAssertEqualObjects(currentQueueLabelString, callbackQueueLabelString);
437+
438+
[expectation fulfill];
439+
440+
// Reset the callbackQueue to default (main queue).
441+
self.storage.callbackQueue = dispatch_get_main_queue();
442+
dispatch_queue_set_specific(callbackQueue, callbackQueueKey, NULL, NULL);
443+
}];
444+
445+
[self waitForExpectations];
446+
}
447+
416448
- (void)testGetDataTooSmall {
417449
XCTestExpectation *expectation = [self expectationWithDescription:@"testGetDataTooSmall"];
418450

FirebaseStorage/Tests/SwiftIntegration/StorageIntegration.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,35 @@ class StorageIntegration: XCTestCase {
379379
waitForExpectations()
380380
}
381381

382+
func testSimpleGetDataWithCustomCallbackQueue() {
383+
let expectation = self.expectation(description: #function)
384+
385+
let callbackQueueLabel = "customCallbackQueue"
386+
let callbackQueueKey = DispatchSpecificKey<String>()
387+
let callbackQueue = DispatchQueue(label: callbackQueueLabel)
388+
callbackQueue.setSpecific(key: callbackQueueKey, value: callbackQueueLabel)
389+
storage.callbackQueue = callbackQueue
390+
391+
let ref = storage.reference(withPath: "ios/public/1mb")
392+
ref.getData(maxSize: 1024 * 1024) { data, error in
393+
XCTAssertNotNil(data, "Data should not be nil")
394+
XCTAssertNil(error, "Error should be nil")
395+
396+
XCTAssertFalse(Thread.isMainThread)
397+
398+
let currentQueueLabel = DispatchQueue.getSpecific(key: callbackQueueKey)
399+
XCTAssertEqual(currentQueueLabel, callbackQueueLabel)
400+
401+
expectation.fulfill()
402+
403+
// Reset the callbackQueue to default (main queue).
404+
self.storage.callbackQueue = DispatchQueue.main
405+
callbackQueue.setSpecific(key: callbackQueueKey, value: nil)
406+
}
407+
408+
waitForExpectations()
409+
}
410+
382411
func testSimpleGetDataTooSmall() {
383412
let expectation = self.expectation(description: #function)
384413

@@ -460,7 +489,7 @@ class StorageIntegration: XCTestCase {
460489
let fileURL = tmpDirURL.appendingPathComponent("hello.txt")
461490
let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed")
462491

463-
_ = [ref.putData(data, metadata: nil, completion: { metadata, error in
492+
ref.putData(data, metadata: nil, completion: { metadata, error in
464493
XCTAssertNotNil(metadata, "Metadata should not be nil")
465494
XCTAssertNil(error, "Error should be nil")
466495
let task = ref.write(toFile: fileURL)
@@ -487,7 +516,7 @@ class StorageIntegration: XCTestCase {
487516
task.observe(StorageTaskStatus.failure, handler: { snapshot in
488517
XCTAssertNil(snapshot.error, "Error should be nil")
489518
})
490-
})]
519+
})
491520
waitForExpectations()
492521
}
493522

FirebaseStorageSwift/Tests/Integration/StorageIntegration.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,33 @@ class StorageIntegration: XCTestCase {
333333
waitForExpectations()
334334
}
335335

336+
func testSimpleGetDataWithCustomCallbackQueue() {
337+
let expectation = self.expectation(description: #function)
338+
339+
let callbackQueueLabel = "customCallbackQueue"
340+
let callbackQueueKey = DispatchSpecificKey<String>()
341+
let callbackQueue = DispatchQueue(label: callbackQueueLabel)
342+
callbackQueue.setSpecific(key: callbackQueueKey, value: callbackQueueLabel)
343+
storage.callbackQueue = callbackQueue
344+
345+
let ref = storage.reference(withPath: "ios/public/1mb")
346+
ref.getData(maxSize: 1024 * 1024) { result in
347+
self.assertResultSuccess(result)
348+
349+
XCTAssertFalse(Thread.isMainThread)
350+
351+
let currentQueueLabel = DispatchQueue.getSpecific(key: callbackQueueKey)
352+
XCTAssertEqual(currentQueueLabel, callbackQueueLabel)
353+
354+
expectation.fulfill()
355+
356+
// Reset the callbackQueue to default (main queue).
357+
self.storage.callbackQueue = DispatchQueue.main
358+
callbackQueue.setSpecific(key: callbackQueueKey, value: nil)
359+
}
360+
waitForExpectations()
361+
}
362+
336363
func testSimpleGetDataTooSmall() {
337364
let expectation = self.expectation(description: #function)
338365

0 commit comments

Comments
 (0)