Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Add second test that tries to mimic #15536
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Rex committed Sep 6, 2019
1 parent 2143e77 commit 5f1a062
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions platform/darwin/test/MGLOfflineStorageTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,63 @@ - (void)test15536RemovePacksWhileReloading {

[self waitForExpectations:@[expectation] timeout:10.0];

// TODO: What should we expect here? All packs removed?

NSLog(@"Test `%@` complete", NSStringFromSelector(_cmd));
}

// Test to explore https://github.com/mapbox/mapbox-gl-native/issues/15536
- (void)test15536RemovePacksOnBackgroundQueueWhileReloading {

[self addPacks:4];

NSInteger countOfPacks = [MGLOfflineStorage sharedOfflineStorage].packs.count;
XCTAssert(countOfPacks > 0);

// Now delete packs one by one
dispatch_queue_t queue = dispatch_queue_create("com.mapbox.testRemovePacks", DISPATCH_QUEUE_SERIAL);

XCTestExpectation *expectation = [self expectationWithDescription:@"all packs removed"];
expectation.expectedFulfillmentCount = countOfPacks;

MGLOfflineStorage *storage = [MGLOfflineStorage sharedOfflineStorage];

// Simulate what happens the first time sharedOfflineStorage is accessed
[storage reloadPacks];

// NSArray *packs = [storage.packs copy];

dispatch_async(queue, ^{
NSArray *packs = storage.packs;
NSAssertionHandler *oldHandler = [NSAssertionHandler currentHandler];
[[[NSThread currentThread] threadDictionary] setValue:[[MGLTestAssertionHandler alloc] init] forKey:NSAssertionHandlerKey];

NSArray *validPacks = [packs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
MGLOfflinePack *pack = (MGLOfflinePack*)evaluatedObject;
return pack.state != MGLOfflinePackStateInvalid;
}]];

for (MGLOfflinePack *pack in validPacks) {
// NOTE: pack can be invalid, as we have two threads potentially
// modifying the same MGLOfflinePack.

dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[storage removePack:pack withCompletionHandler:^(NSError * _Nullable error) {
dispatch_group_leave(group);
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

[expectation fulfill];
}

[[[NSThread currentThread] threadDictionary] setValue:oldHandler forKey:NSAssertionHandlerKey];
});

[self waitForExpectations:@[expectation] timeout:60.0];

// TODO: What should we expect here? All packs removed?

NSLog(@"Test `%@` complete", NSStringFromSelector(_cmd));
}

Expand Down

0 comments on commit 5f1a062

Please sign in to comment.