Skip to content

Commit

Permalink
safely shutdown
Browse files Browse the repository at this point in the history
Signed-off-by: Nan Wang <[email protected]>
  • Loading branch information
nanwng committed Aug 29, 2023
1 parent 0ff8ffd commit 4ec6bd7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions bp/src/BPSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@
- (BOOL)needsRetry;
- (BPExitStatus)exitStatus;
- (BOOL)isApplicationLaunched;
- (BOOL)shutdownSimulatorWithError:(id *)error;
@end
72 changes: 69 additions & 3 deletions bp/src/BPSimulator.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ - (NSString *)installApplicationWithHost:(NSString *)testHost withError:(NSError
}];
return nil;
} else {
[simDevice shutdownWithError:errPtr];
// [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 +308,69 @@ - (BOOL)useSimulatorWithDeviceUDID:(NSUUID *)deviceUDID {
return YES;
}

- (void)eraseSimulator:(SimDevice *)device withError:(id *) error{
NSLog(@"Erasing Simulator %@", device.UDID.UUIDString);
[device eraseContentsAndSettingsWithError:error];
if (*error) {
NSLog(@"Error erasing simulator: %@", *error);
} else {
NSLog(@"Simulator erased successfully.");
}
}

- (BOOL)waitForSimulatorShutdown:(SimDevice *)device {
NSDate *startTime = [NSDate date];
NSTimeInterval timeoutInterval = 30.0;

while (![device.stateString isEqualToString:@"Shutdown"]) {
NSTimeInterval elapsedTime = -[startTime timeIntervalSinceNow];
if (elapsedTime >= timeoutInterval) {
return NO; // Timeout
}

// You might need to add some delay here to avoid busy-waiting
[NSThread sleepForTimeInterval:1.0];
}

return YES; // Successfully shutdown
}

- (void)eraseAndShutdownSimulator:(SimDevice *)device withError: (id *)error {
// Wait for Shutdown
BOOL didShutdown = [self waitForSimulatorShutdown:device];

if (didShutdown) {
NSLog(@"Simulator has been successfully shutdown.");
} else {
NSLog(@"Failed to shutdown simulator within the timeout, erase it.");
// Erase Simulator
[self eraseSimulator:device withError:error];
}
}


- (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;
}

// Xcode 7 has a 'Creating' step that we should wait on before confirming the simulator is ready.
// On many occasions this is the case as we wait for the Simulator to be usable.
if ([self.device.stateString isEqualToString:@"Creating"]) {
[self eraseAndShutdownSimulator:self.device withError:error];
return YES;
}

// The error code for 'Unable to shutdown device in current state: Shutdown'
// can be safely ignored since these codes confirm that the simulator is already shutdown.
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 +384,8 @@ - (void)openSimulatorHeadlessWithCompletion:(void (^)(NSError *))completion {
[self.device bootAsyncWithOptions:options completionHandler:^(NSError *bootError){
NSError *error = [self waitForDeviceReady];
if (error) {
[self.device shutdownWithError:&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 +636,8 @@ - (void)deleteSimulatorWithCompletion:(void (^)(NSError *error, BOOL success))co
}
if (self.device) {
[BPUtils printInfo:INFO withString:@"Shutting down Simulator"];
[self.device shutdownWithError:&error];
// [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 4ec6bd7

Please sign in to comment.