Skip to content
This repository has been archived by the owner on Jan 17, 2019. It is now read-only.

Commit

Permalink
Fix 'Unclaimed' Application Fetching
Browse files Browse the repository at this point in the history
Summary:
There is one very subtle case that will cause the 'unclaimed' (read Xcode-launched) Application fetching will fail:
1) Boot a Simulator via conventional means like via Xcode or an alias to the `Simulator.app`
2) Boot another Simulator.app via `fbsimctl`
3) `fbsimctl shutdown` will properly kill the sim from #1 but not #2

This is because the booting of #2 overwrites the `NSUserDefaults` from #1 so the 'unclaimed' simulator container app doesn't correlate. The check for the UDID in the `NSUserDefaults` is unecessary since we can reasonably assume if there is one more Simulator app that is unclaimed this must be the sole remaining Simulator.app for the `launchd_sim`.

Reviewed By: marekcirkos

Differential Revision: D3811248

fbshipit-source-id: e9f0fb97c8370f5966f191eebbd246f61ec722b5
  • Loading branch information
lawrencelomax authored and Facebook Github Bot 3 committed Sep 8, 2016
1 parent 74b19d8 commit 9eedd44
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions FBSimulatorControl/Strategies/FBSimulatorInflationStrategy.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,27 @@ - (instancetype)initWithSet:(FBSimulatorSet *)set processFetcher:(FBSimulatorPro
}

// Inflate the Simulators and join the array.
NSArray<FBSimulator *> *inflatedSimulators = [self inflateSimulators:simulatorsToInflate.allObjects availableDevices:availableDevices];
NSArray<FBProcessInfo *> *previouslyIdentifiedContainerApplications = [[simulators valueForKey:@"containerApplication"] filteredArrayUsingPredicate:NSPredicate.notNullPredicate];
NSArray<FBSimulator *> *inflatedSimulators = [self
inflateSimulators:simulatorsToInflate.allObjects
availableDevices:availableDevices
previouslyIdentifiedContainerApplications:previouslyIdentifiedContainerApplications];
return [simulators arrayByAddingObjectsFromArray:inflatedSimulators];
}

#pragma mark Private

- (NSArray<FBSimulator *> *)inflateSimulators:(NSArray<NSString *> *)simulatorsToInflate availableDevices:(NSDictionary<NSString *, SimDevice *> *)availableDevices
- (NSArray<FBSimulator *> *)inflateSimulators:(NSArray<NSString *> *)simulatorsToInflate availableDevices:(NSDictionary<NSString *, SimDevice *> *)availableDevices previouslyIdentifiedContainerApplications:(NSArray<FBProcessInfo *> *)previouslyIdentifiedContainerApplications
{
NSArray<FBProcessInfo *> *unclaimedContainerApplications = nil;
NSDictionary<NSString *, FBProcessInfo *> *launchdSims = [self.processFetcher launchdProcessesByUDIDs:simulatorsToInflate];
NSDictionary<NSString *, FBProcessInfo *> *containerApplications = [self.processFetcher simulatorApplicationProcessesByUDIDs:simulatorsToInflate unclaimed:&unclaimedContainerApplications];

containerApplications = [FBSimulatorInflationStrategy
adjustContainerApplicationsMapping:containerApplications
forLaunchdSims:launchdSims
withUnclaimedContainerApplications:unclaimedContainerApplications];
withUnclaimedContainerApplications:unclaimedContainerApplications
previouslyIdentifiedContainerApplications:previouslyIdentifiedContainerApplications];

NSMutableArray<FBSimulator *> *inflatedSimulators = [NSMutableArray array];
for (NSString *udid in simulatorsToInflate) {
Expand All @@ -102,22 +108,28 @@ - (instancetype)initWithSet:(FBSimulatorSet *)set processFetcher:(FBSimulatorPro
return [inflatedSimulators copy];
}

+ (NSDictionary<NSString *, FBProcessInfo *> *)adjustContainerApplicationsMapping:(NSDictionary<NSString *, FBProcessInfo *> *)containerApplications forLaunchdSims:(NSDictionary<NSString *, FBProcessInfo *> *)launchdSims withUnclaimedContainerApplications:(NSArray<FBProcessInfo *> *)unclaimedContainerApplications
+ (NSDictionary<NSString *, FBProcessInfo *> *)adjustContainerApplicationsMapping:(NSDictionary<NSString *, FBProcessInfo *> *)containerApplications forLaunchdSims:(NSDictionary<NSString *, FBProcessInfo *> *)launchdSims withUnclaimedContainerApplications:(NSArray<FBProcessInfo *> *)unclaimedContainerApplications previouslyIdentifiedContainerApplications:(NSArray<FBProcessInfo *> *)previouslyIdentifiedContainerApplications
{
// We can only correlate when we have one unclaimed Simulator Application.
if (unclaimedContainerApplications.count != 1) {
return containerApplications;
}
// Confirm that this one remaining container application hasn't been previously correlated with another Simulator.
NSMutableSet<FBProcessInfo *> *remainingUnclaimed = [NSMutableSet setWithArray:unclaimedContainerApplications];
[remainingUnclaimed minusSet:[NSSet setWithArray:previouslyIdentifiedContainerApplications]];
if (remainingUnclaimed.count != 1) {
return containerApplications;
}

NSMutableSet *unclaimedSimulatorUDIDs = [NSMutableSet setWithArray:launchdSims.allKeys];
// Check the Simulators that are unclaimed, if there are none there's nothing to correlate.
NSMutableSet<NSString *> *unclaimedSimulatorUDIDs = [NSMutableSet setWithArray:launchdSims.allKeys];
[unclaimedSimulatorUDIDs minusSet:[NSSet setWithArray:containerApplications.allKeys]];
if (unclaimedSimulatorUDIDs.count != 1) {
return containerApplications;
}
NSString *untaggedAssumedUDID = FBSimulator.simulatorApplicationPreferences[@"CurrentDeviceUDID"];
if (![untaggedAssumedUDID isEqualToString:unclaimedSimulatorUDIDs.anyObject]) {
return containerApplications;
}

// Assume that this sole unclaimed Simulator App belongs to the 'Containerless' Booted UDID.
NSString *untaggedAssumedUDID = [unclaimedSimulatorUDIDs anyObject];
NSMutableDictionary *adjustedContainerApplications = [containerApplications mutableCopy];
adjustedContainerApplications[untaggedAssumedUDID] = [unclaimedContainerApplications firstObject];
return [adjustedContainerApplications copy];
Expand Down

0 comments on commit 9eedd44

Please sign in to comment.