kstyrc / embedded-redis

Redis embedded server for Java integration testing
840 stars 368 forks source link

Improvements towards orphaned Redis processes #74

Open Sergeant007 opened 7 years ago

Sergeant007 commented 7 years ago

As it was mentioned in other issues here, embedded-redis leaves orphaned child processes when parent is aborted without RedisServer#stop(). Someone reported such behavior on CI servers, but for me this is happening when I stop debugging from IDE (IntelliJ) in the middle of a test case.

Currently auto-termination of child processes is unsolved problem in Java (stackoverflow). Hopefully that will be fixed once Java 9 is live (JEP-102). Until then I suggest a couple of improvements:

  1. Test if local port is available first and throw BindException if not. Let user define what to do in this case. In my case I've just decided to naively and blindly guess that there is already Redis up and running:
def isLocalPortAvailable(port: Int): Boolean = {
    var portAvailable = true
    var socket: ServerSocket = null
    try {
      socket = new ServerSocket(port)
    } catch {
      case e: BindException => portAvailable = false
    } finally {
      if (socket != null) {
        socket.close()
      }
    }
    portAvailable
  }
  1. Do not throw generic RuntimeException in AbstractRedisInstance, line 61. Just inherit something custom from runtime. It will be easier for end users to add custom handling logic
  2. Add Runtime.getRuntime().addShutdownHook logic as suggested in mentioned above stackoverflow, which will cover some cases of process termination, like SIGINT