Skip to content

Commit

Permalink
Make stop to wait until close
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Feb 5, 2020
1 parent e67f517 commit eae7a59
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions lib/device-connections-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,45 +67,49 @@ class iProxy {
}
this.localServer.on('error', (e) => this.log.warn(e.message));

this.onBeforeProcessExit = () => {
if (this.localServer?.listening) {
this.localServer.close((e) => {
if (e) {
this.log.debug(e);
} else {
this.log.info(`The connection to ${this.udid} has been closed`);
}
this.localServer = null;
});
this.onBeforeProcessExit = async () => {
try {
await this._closeLocalServer();
} catch (e) {
this.log.debug(e);
}
};
// Make sure we free up the socket on process exit
process.on('beforeExit', this.onBeforeProcessExit);
}

// eslint-disable-next-line require-await
async quit () {
async _closeLocalServer () {
if (!this.localServer) {
return;
}

const server = this.localServer;
this.localServer = null;
if (server.listening) {
await new B((resolve, reject) => {
server.close((e) => {
if (e) {
reject(e);
} else {
this.log.info(`The connection to ${this.udid} has been successfully closed`);
resolve();
}
});
});
}
}

async stop () {
if (this.onBeforeProcessExit) {
process.off('beforeExit', this.onBeforeProcessExit);
this.onBeforeProcessExit = null;
}
if (!this.localServer) {
return;
}

// TODO: Wait until the connection is actually closed
// after the corresponding fixes are done in appium-ios-device
// module. Without the fixes the close event might be delayed for up to
// 30 seconds (see https://github.com/appium/appium-xcuitest-driver/pull/1094#issuecomment-546578765)
this.localServer.once('close', (e) => {
if (e) {
this.log.debug(e);
} else {
this.log.info(`The connection to ${this.udid} has been closed`);
}
this.localServer = null;
});
this.localServer.close();
try {
await this._closeLocalServer();
} catch (e) {
this.log.debug(e);
}
}
}

Expand Down Expand Up @@ -216,7 +220,7 @@ class DeviceConnectionsFactory {
this._connectionsMapping[currentKey] = {iproxy};
} catch (e) {
try {
await iproxy.quit();
await iproxy.stop();
} catch (ign) {}
throw e;
}
Expand Down

0 comments on commit eae7a59

Please sign in to comment.