Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 5c810da

Browse files
Fix check for ios simulator
When we check if there is running simulator we use ps cax but if the simulator is killed for a short period of time after that there is a process listed when using ps cax and we decide that there is a running simulator and then we try to use it but ios-sim-portable returns null. We should check if the simulator returned from ios-sim-portable is not null or undefined in order to continue to work with it.
1 parent 8bb173c commit 5c810da

File tree

2 files changed

+10
-33
lines changed

2 files changed

+10
-33
lines changed

mobile/mobile-core/ios-simulator-discovery.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {IOSSimulator} from "./../ios/simulator/ios-simulator-device";
55
export class IOSSimulatorDiscovery extends DeviceDiscovery {
66
private cachedSimulator: Mobile.IiSimDevice;
77

8-
constructor(private $childProcess: IChildProcess,
9-
private $injector: IInjector,
8+
constructor(private $injector: IInjector,
109
private $iOSSimResolver: Mobile.IiOSSimResolver,
1110
private $hostInfo: IHostInfo) {
1211
super();
@@ -18,9 +17,9 @@ export class IOSSimulatorDiscovery extends DeviceDiscovery {
1817

1918
public checkForDevices(future?: IFuture<void>): IFuture<void> {
2019
if (this.$hostInfo.isDarwin) {
21-
if (this.isSimulatorRunning().wait()) {
22-
let currentSimulator = this.$iOSSimResolver.iOSSim.getRunningSimulator();
20+
let currentSimulator = this.$iOSSimResolver.iOSSim.getRunningSimulator();
2321

22+
if (currentSimulator) {
2423
if (!this.cachedSimulator) {
2524
this.createAndAddDevice(currentSimulator);
2625
} else if (this.cachedSimulator.id !== currentSimulator.id) {
@@ -41,20 +40,10 @@ export class IOSSimulatorDiscovery extends DeviceDiscovery {
4140
return future || Future.fromResult();
4241
}
4342

44-
private isSimulatorRunning(): IFuture<boolean> {
45-
return (() => {
46-
try {
47-
let output = this.$childProcess.exec("ps cax | grep launchd_sim").wait();
48-
return output.indexOf('launchd_sim') !== -1;
49-
} catch(e) {
50-
return false;
51-
}
52-
}).future<boolean>()();
53-
}
54-
5543
private createAndAddDevice(simulator: Mobile.IiSimDevice): void {
5644
this.cachedSimulator = _.cloneDeep(simulator);
57-
this.addDevice(this.$injector.resolve(IOSSimulator, {simulator: this.cachedSimulator}));
45+
this.addDevice(this.$injector.resolve(IOSSimulator, { simulator: this.cachedSimulator }));
5846
}
5947
}
48+
6049
$injector.register("iOSSimulatorDiscovery", IOSSimulatorDiscovery);

test/unit-tests/mobile/ios-simulator-discovery.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import Future = require("fibers/future");
44
import { assert } from "chai";
55
import { DevicePlatformsConstants } from "../../../mobile/device-platforms-constants";
66

7-
let currentlyRunningSimulator: any,
8-
isCurrentlyRunning: boolean;
7+
let currentlyRunningSimulator: any;
98

109
function createTestInjector(): IInjector {
1110
let injector = new Yok();
1211
injector.register("childProcess", {
13-
exec: (command: string) => Future.fromResult(isCurrentlyRunning ? 'launchd_sim' : '')
12+
exec: (command: string) => Future.fromResult("")
1413
});
1514
injector.register("injector", injector);
1615
injector.register("iOSSimResolver", {
@@ -24,12 +23,6 @@ function createTestInjector(): IInjector {
2423

2524
injector.register("devicePlatformsConstants", DevicePlatformsConstants);
2625

27-
injector.register("iOSSimResolver", {
28-
iOSSim: {
29-
getRunningSimulator: () => currentlyRunningSimulator
30-
}
31-
});
32-
3326
injector.register("iOSSimulatorDiscovery", IOSSimulatorDiscovery);
3427
return injector;
3528
}
@@ -42,7 +35,6 @@ describe("ios-simulator-discovery", () => {
4235

4336
let detectNewSimulatorAttached = (runningSimulator: any): Mobile.IiOSSimulator => {
4437
let future = new Future<any>();
45-
isCurrentlyRunning = true;
4638
currentlyRunningSimulator = _.cloneDeep(runningSimulator);
4739
iOSSimulatorDiscovery.once("deviceFound", (device: Mobile.IDevice) => {
4840
future.return(device);
@@ -52,7 +44,7 @@ describe("ios-simulator-discovery", () => {
5244
};
5345

5446
let detectSimulatorDetached = (): Mobile.IiOSSimulator => {
55-
isCurrentlyRunning = false;
47+
currentlyRunningSimulator = null;
5648
let lostDeviceFuture = new Future<Mobile.IDevice>();
5749
iOSSimulatorDiscovery.once("deviceLost", (device: Mobile.IDevice) => {
5850
lostDeviceFuture.return(device);
@@ -62,7 +54,6 @@ describe("ios-simulator-discovery", () => {
6254
};
6355

6456
let detectSimulatorChanged = (newId: string): any => {
65-
isCurrentlyRunning = true;
6657
currentlyRunningSimulator.id = newId;
6758
let lostDeviceFuture = new Future<Mobile.IDevice>(),
6859
foundDeviceFuture = new Future<Mobile.IDevice>();
@@ -83,11 +74,11 @@ describe("ios-simulator-discovery", () => {
8374
};
8475

8576
beforeEach(() => {
86-
isCurrentlyRunning = false;
8777
currentlyRunningSimulator = null;
8878
testInjector = createTestInjector();
8979
iOSSimulatorDiscovery = testInjector.resolve("iOSSimulatorDiscovery");
90-
expectedDeviceInfo = { identifier: "id",
80+
expectedDeviceInfo = {
81+
identifier: "id",
9182
displayName: 'name',
9283
model: 'c',
9384
version: '9.2.1',
@@ -156,7 +147,6 @@ describe("ios-simulator-discovery", () => {
156147
throw new Error("Cannot find iOS Devices.");
157148
}).future<any>()();
158149
};
159-
isCurrentlyRunning = true;
160150
iOSSimulatorDiscovery.on("deviceFound", (device: Mobile.IDevice) => {
161151
throw new Error("Device found should not be raised when getting running iOS Simulator fails.");
162152
});
@@ -165,7 +155,6 @@ describe("ios-simulator-discovery", () => {
165155

166156
it("does not detect iOS Simulator when not running on OS X", () => {
167157
testInjector.resolve("hostInfo").isDarwin = false;
168-
isCurrentlyRunning = true;
169158
iOSSimulatorDiscovery.on("deviceFound", (device: Mobile.IDevice) => {
170159
throw new Error("Device found should not be raised when OS is not OS X.");
171160
});
@@ -174,7 +163,6 @@ describe("ios-simulator-discovery", () => {
174163

175164
it("checkForDevices return future", () => {
176165
testInjector.resolve("hostInfo").isDarwin = false;
177-
isCurrentlyRunning = true;
178166
iOSSimulatorDiscovery.on("deviceFound", (device: Mobile.IDevice) => {
179167
throw new Error("Device found should not be raised when OS is not OS X.");
180168
});

0 commit comments

Comments
 (0)