google-pubsub-emulator icon indicating copy to clipboard operation
google-pubsub-emulator copied to clipboard

Emulator does not stop properly on exit

Open willwillems opened this issue 2 years ago • 5 comments

Even when trying to stop the emulator manually on exit the Java servers will be left running in the background resulting in the following error the next time:

Exception in thread "main" java.net.BindException: Address already in use

willwillems avatar May 02 '22 17:05 willwillems

Could you provide a minimal example that could reproduce the error?

ert78gb avatar May 22 '22 18:05 ert78gb

Basically it's just running

import Emulator from "google-pubsub-emulator"

const emulator = new Emulator({
   project: process.env.PUBSUB_PROJECT_ID,
   port: process.env.PUBSUB_PORT,
   debug: true
})

await emulator.start()

without "properly" calling

await pubsub.stop()

on exit. My current solution uses node-cleanup like:

// EXIT //
nodeCleanup(function (exitCode, signal) {
   // async only possible on signal, not exit
   if (signal) {
      pubsub.stop().then(() => {
         console.log("Pub/Sub is down")
         process.kill(process.pid, signal)
      })
      nodeCleanup.uninstall() // don't call cleanup handler again
      return false // don't call exit automatically
   }
})

I can kinda image you don't see this as a bug so feel free to close the issue but it was a ton of work for me to get this working this way (and you better hope your code doesn't crash because then you'll be closing random Java ports again).

I also had the exact same issue using my DIY implementation so I kinda want to emphasis here that I'm pretty happy with this package, thanks for making it!

willwillems avatar May 24 '22 15:05 willwillems

I see. When the node process terminates than the emulator does not close the resources.

Every test runner that I use has a tearup/down feature that I use to setup/down the test context.

Could you share more info about your use case? I would like to understand your process.

when I use mocha test runner I especially like it does not exist when something running in the background. It helps to identify runtime issues

ert78gb avatar May 24 '22 17:05 ert78gb

Ah I should have mentioned that, we use it to setup a local dev env along with another solution to emulate the cloud functions we trigger with it.

willwillems avatar May 24 '22 17:05 willwillems

for dev environment maybe the google cloud sdk docker image is better choice

ert78gb avatar May 24 '22 18:05 ert78gb