Skip to content

Commit

Permalink
Proper shutdown mechanism (#559) (#560)
Browse files Browse the repository at this point in the history
* safely shutdown



* no need to erase sims



---------

Signed-off-by: Nan Wang <nanwang1101@yahoo.com>
  • Loading branch information
nanwng authored Aug 29, 2023
1 parent a7fcb47 commit d14d127
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions bp/src/BPSimulator.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ - (NSString *)installApplicationWithHost:(NSString *)testHost withError:(NSError
}];
return nil;
} else {
[simDevice shutdownWithError:errPtr];
[self shutdownSimulatorWithError:errPtr];
if(*errPtr) {
[BPUtils printInfo:ERROR withString:@"Shutdown simulator failed with error: %@", [*errPtr localizedDescription]];
[deviceSet deleteDeviceAsync:simDevice completionHandler:^(NSError *error) {
Expand Down Expand Up @@ -307,6 +307,19 @@ - (BOOL)useSimulatorWithDeviceUDID:(NSUUID *)deviceUDID {
return YES;
}

- (BOOL)shutdownSimulatorWithError:(id *)error {
[BPUtils printInfo:INFO withString:@"Starting Safe Shutdown of %@", self.device.UDID.UUIDString];

// Calling shutdown when already shutdown should be avoided (if detected).
if ([self.device.stateString isEqualToString:@"Shutdown"]) {
[BPUtils printInfo:INFO withString:@"Shutdown of %@ succeeded as it is already shutdown", self.device];
*error = nil;
return YES;
}

return [self.device shutdownWithError:error];
}

- (void)bootWithCompletion:(void (^)(NSError *error))completion {
// Now boot it.
[BPUtils printInfo:INFO withString:@"Booting a simulator without launching Simulator app"];
Expand All @@ -320,7 +333,7 @@ - (void)openSimulatorHeadlessWithCompletion:(void (^)(NSError *))completion {
[self.device bootAsyncWithOptions:options completionHandler:^(NSError *bootError){
NSError *error = [self waitForDeviceReady];
if (error) {
[self.device shutdownWithError:&error];
[self shutdownSimulatorWithError:&error];
if (error) {
[BPUtils printInfo:ERROR withString:@"Shutting down Simulator failed: %@", [error localizedDescription]];
}
Expand Down Expand Up @@ -571,7 +584,7 @@ - (void)deleteSimulatorWithCompletion:(void (^)(NSError *error, BOOL success))co
}
if (self.device) {
[BPUtils printInfo:INFO withString:@"Shutting down Simulator"];
[self.device shutdownWithError:&error];
[self shutdownSimulatorWithError:&error];
if (error) {
[BPUtils printInfo:ERROR withString:@"Shutting down Simulator failed: %@", [error localizedDescription]];
completion(error, NO);
Expand Down

0 comments on commit d14d127

Please sign in to comment.