| 182 | } |
| 183 | |
| 184 | async exit(): Promise<void> { |
| 185 | return measureAndLog(() => new Promise<void>(resolve => { |
| 186 | const pid = this.mainProcess.pid!; |
| 187 | |
| 188 | let done = false; |
| 189 | |
| 190 | // Start the exit flow via driver |
| 191 | this.driver.close(); |
| 192 | |
| 193 | let safeToKill = false; |
| 194 | this.safeToKill?.then(() => { |
| 195 | this.logger.log('Smoke test exit(): safeToKill() called'); |
| 196 | safeToKill = true; |
| 197 | }); |
| 198 | |
| 199 | // Await the exit of the application |
| 200 | (async () => { |
| 201 | let retries = 0; |
| 202 | while (!done) { |
| 203 | retries++; |
| 204 | |
| 205 | if (safeToKill) { |
| 206 | this.logger.log('Smoke test exit(): call did not terminate the process yet, but safeToKill is true, so we can kill it'); |
| 207 | this.kill(pid); |
| 208 | } |
| 209 | |
| 210 | switch (retries) { |
| 211 | |
| 212 | // after 10 seconds: forcefully kill |
| 213 | case 20: { |
| 214 | this.logger.log('Smoke test exit(): call did not terminate process after 10s, forcefully exiting the application...'); |
| 215 | this.kill(pid); |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | // after 20 seconds: give up |
| 220 | case 40: { |
| 221 | this.logger.log('Smoke test exit(): call did not terminate process after 20s, giving up'); |
| 222 | this.kill(pid); |
| 223 | done = true; |
| 224 | resolve(); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | try { |
| 230 | process.kill(pid, 0); // throws an exception if the process doesn't exist anymore. |
| 231 | await this.wait(500); |
| 232 | } catch (error) { |
| 233 | this.logger.log('Smoke test exit(): call terminated process successfully'); |
| 234 | |
| 235 | done = true; |
| 236 | resolve(); |
| 237 | } |
| 238 | } |
| 239 | })(); |
| 240 | }), 'Code#exit()', this.logger); |
| 241 | } |