rstudio / httpuv

HTTP and WebSocket server package for R
Other
229 stars 86 forks source link

Add randomPort function, and add quiet option for startServer #234

Closed wch closed 5 years ago

wch commented 5 years ago

Closes #194, closes #233. The quiet option suppresses messages that are normally printed to console (and can't be intercepted with capture.output).

Update: This PR now adds a new function, randomPort.

Testing notes. The code below should have the behavior as described.

port <- 5001
# Start server
s <- startServer("127.0.0.1", port, list())

# Should print out "createTcpServer: address already in use", and throw an error
startServer("127.0.0.1", port, list())

# Should throw error only (and not print "createTcpServer: address already in use")
startServer("127.0.0.1", port, list(), quiet = TRUE)

# Should print "createTcpServer: address already in use" and return NULL
tryCatch(
  startServer("127.0.0.1", port, list()),
  error = function(e) {}
)

# Should just print NULL
tryCatch(
  startServer("127.0.0.1", port, list(), quiet = TRUE),
  error = function(e) {}
)

# Should return a random open port
randomPort()

# Should return 5002 (because 5001 is already taken)
randomPort(min = port, max = port + 1)
jcheng5 commented 5 years ago

@wch Did you want this in the upcoming release of httpuv? Or the next one?

wch commented 5 years ago

I was hoping to get it in this release. Actually, it might make sense to add in some code that finds an available port, as described in rstudio/shiny#2562.