Skip to content

Commit

Permalink
chore: factor out findRandomPort() and validate user input
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Jan 13, 2025
1 parent 6afb174 commit 8b0c288
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
65 changes: 43 additions & 22 deletions R/find-port.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,55 @@ getRandomPort <- function(){
port
}

#' Find a port either using the assigned port or randomly search 10 times for an available
#' port. If a port was manually assigned, just return it and assume it will work.
#' @noRd
findPort <- function(port){
if (missing(port) || is.null(port)){
if (!is.null(.globals$port)){
# Start by trying the .globals$port
port <- .globals$port
} else {
port <- getRandomPort()
}
findRandomPort <- function() {
port <- if (!is.null(.globals$port)) {
# Start by trying the .globals$port
.globals$port
} else {
getRandomPort()
}

for (i in 1:10){
tryCatch(srv <- httpuv::startServer("127.0.0.1", port, list(), quiet = TRUE), error=function(e){
for (i in 1:10) {
tryCatch(
srv <- httpuv::startServer("127.0.0.1", port, list(), quiet = TRUE),
error = function(e) {
port <<- 0
})
if (port != 0){
# Stop the temporary server, and retain this port number.
httpuv::stopServer(srv)
.globals$port <- port
break
}
port <- getRandomPort()
)
if (port != 0) {
# Stop the temporary server, and retain this port number.
httpuv::stopServer(srv)
.globals$port <- port
break
}
port <- getRandomPort()
}

if (port == 0) {
stop(
"Unable to start a Plumber server. We were unable to find a free port in 10 tries."
)
}

as.integer(port)
}

#' Find a port either using the assigned port or randomly search 10 times for an available
#' port. If a port was manually assigned, just return it and assume it will work.
#' @noRd
findPort <- function(port) {
if (missing(port) || is.null(port)) {
return(findRandomPort())
}

port_og <- port

if (rlang::is_character(port)) {
port <- suppressWarnings(as.integer(port))
}

if (port == 0){
stop("Unable to start a Plumber server. Either the port specified was unavailable or we were unable to find a free port.")
if (!rlang::is_integerish(port, n = 1, finite = TRUE) || port != port_og) {
stop("Port must be an integer value, not '", port_og, "'.")
}

as.integer(port)
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-find-port.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ test_that("global port used if available", {

test_that("integer type is returned", {
expect_type(findPort(), "integer")
expect_equal(findPort("8000"), 8000L)
expect_equal(findPort(8000.00000), 8000L)
})

test_that("throws if provided non-integerish port", {
expect_error(findPort("blue"))
expect_error(findPort(8000.0001))
expect_error(findPort(8000:8002))
})

test_that("finds a good port and persists it", {
Expand Down

0 comments on commit 8b0c288

Please sign in to comment.