Browse Source

Exit gracefully on linux (#12139)

* Fix timing issue in singleton fixture.

Singleton now sends the "we've started" message out only after it's
received a `'ready'` event from `app`. Previously it sent the message
out immediately, resulting in the parent test trying to manipulate it
before Singleton's event loop was fully bootstrapped.

* Check for graceful exits on Linux, too.

Rewrite the "exits gracefully on macos" spec to run on Linux too.

* Check for graceful exits everywhere.

* Tweak comment

* Better error logging in api-app-spec.js. (#12122)

In the 'exits gracefully' test for app.exit(exitCode),
print the relevant error information if the test fails.

* Run the exit-gracefully test on macOS and Linux.

Windows does not support sending signals, but Node.js offers some
emulation with process.kill(), and subprocess.kill(). Sending signal 0
can be used to test for the existence of a process. Sending SIGINT,
SIGTERM, and SIGKILL cause the unconditional termination of the target
process.

So, we'll need a different approach if we want to test this in win32.
Charles Kerr 7 years ago
parent
commit
2cac654974
2 changed files with 16 additions and 12 deletions
  1. 13 11
      spec/api-app-spec.js
  2. 3 1
      spec/fixtures/api/singleton/main.js

+ 13 - 11
spec/api-app-spec.js

@@ -158,21 +158,23 @@ describe('app module', () => {
       })
     })
 
-    it('exits gracefully on macos', function (done) {
-      if (process.platform !== 'darwin') {
+    it('exits gracefully', function (done) {
+      if (!['darwin', 'linux'].includes(process.platform)) {
         this.skip()
       }
-      const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton')
+
       const electronPath = remote.getGlobal('process').execPath
+      const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton')
       appProcess = ChildProcess.spawn(electronPath, [appPath])
-      appProcess.stdout.once('data', () => {
-        // The apple script will try to terminate the app
-        // If there's an error terminating the app, then it will print to stderr
-        ChildProcess.exec('osascript -e \'quit app "Electron"\'', (err, stdout, stderr) => {
-          assert(!err)
-          assert(!stderr.trim())
-          done()
-        })
+
+      // Singleton will send us greeting data to let us know it's running.
+      // After that, ask it to exit gracefully and confirm that it does.
+      appProcess.stdout.on('data', (data) => appProcess.kill())
+      appProcess.on('exit', (code, sig) => {
+        let message = ['code:', code, 'sig:', sig].join('\n')
+        assert.equal(code, 0, message)
+        assert.equal(sig, null, message)
+        done()
       })
     })
   })

+ 3 - 1
spec/fixtures/api/singleton/main.js

@@ -1,6 +1,8 @@
 const {app} = require('electron')
 
-console.log('started')  // ping parent
+app.once('ready', () => {
+  console.log('started')  // ping parent
+})
 
 const shouldExit = app.makeSingleInstance(() => {
   process.nextTick(() => app.exit(0))