scalatestingnettyunfiltered

How to get notified when unfiltered Netty server actually gets shutdown?


I have an Unfiltered Netty server that I need to shutdown and restart after every test.

val mockService = unfiltered.netty.Server.http(mockServicePort).handler(mockServicePlan)

before {
  proxyServer.start()
}

after {
  proxyServer.stop()
}

Currently, this is not working, and I am fairly certain that is because the stop() function is non-blocking and so the following start() function gets called to early.

I looked for a way to block or get notified on server closure, but it would not appear to be surfaced through the current API.

Is there a better way of achieving what I am trying to achieve?


Solution

  • Easy answer: replace your Unfiltered Netty server with a HTTP4S Blaze server.

    var server: org.http4s.server.Server = null
    
    val go: Task[Server] = org.http4s.server.blaze.BlazeBuilder
      .bindHttp(mockServicePort)
      .mountService(mockService)
      .start
    
    before {
      server = go.run
    }
    
    after {
      server.shutdown.run
    }
    

    There's also an awaitShutdown that blocks until the server shuts down. But since shutdown is a Task, it's easy to get notified when it has finished. Just flatMap that shit.