R Under development (unstable) (2024-02-28 r85999 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(port4me) > > Sys.setenv("_PORT4ME_CHECK_AVAILABLE_PORTS_" = "any") > > message('- port4me(user = "alice")') - port4me(user = "alice") > truth <- 30845L > ports <- port4me(user = "alice") > print(ports) [1] 30845 > stopifnot( + is.integer(ports), + all(is.finite(ports)), + all(ports > 0L), + all(ports <= 65535L), + all(ports >= 1024L), + length(ports) == length(truth), + all(ports == truth) + ) > > > message('- port4me(user = "bob")') - port4me(user = "bob") > truth <- 54242L > ports <- port4me(user = "bob") > print(ports) [1] 54242 > stopifnot( + is.integer(ports), + all(is.finite(ports)), + all(ports > 0L), + all(ports <= 65535L), + all(ports >= 1024L), + length(ports) == length(truth), + all(ports == truth) + ) > > > message('- port4me(user = "alice", tool = "rstudio")') - port4me(user = "alice", tool = "rstudio") > truth <- 22486L > ports <- port4me(user = "alice", tool="rstudio") > print(ports) [1] 22486 > stopifnot( + is.integer(ports), + all(is.finite(ports)), + all(ports > 0L), + all(ports <= 65535L), + all(ports >= 1024L), + length(ports) == length(truth), + all(ports == truth) + ) > > > message('- port4me(user = "alice") with PORT4ME_TOOL=rstudio)') - port4me(user = "alice") with PORT4ME_TOOL=rstudio) > Sys.setenv(PORT4ME_TOOL = "rstudio") > truth <- 22486L > ports <- port4me(user = "alice") > print(ports) [1] 22486 > stopifnot( + is.integer(ports), + all(is.finite(ports)), + all(ports > 0L), + all(ports <= 65535L), + all(ports >= 1024L), + length(ports) == length(truth), + all(ports == truth) + ) > Sys.unsetenv("PORT4ME_TOOL") > > > message('- port4me(user = "alice", tool = "jupyter-notebook")') - port4me(user = "alice", tool = "jupyter-notebook") > truth <- 29525L > ports <- port4me(user = "alice", tool="jupyter-notebook") > print(ports) [1] 29525 > stopifnot( + is.integer(ports), + all(is.finite(ports)), + all(ports > 0L), + all(ports <= 65535L), + all(ports >= 1024L), + length(ports) == length(truth), + all(ports == truth) + ) > > message('- port4me() with PORT4ME_USER=alice') - port4me() with PORT4ME_USER=alice > Sys.setenv(PORT4ME_USER = "alice") > truth <- 30845L > ports <- port4me() > print(ports) [1] 30845 > stopifnot( + is.integer(ports), + all(is.finite(ports)), + all(ports > 0L), + all(ports <= 65535L), + all(ports >= 1024L), + length(ports) == length(truth), + all(ports == truth) + ) > > > truth <- c(30845, 19654, 32310, 63992, 15273, 31420, 62779, 55372, 24143, 41300) > exclude <- c(30845, 32310) > message(sprintf("- port4me(exclude = c(%s))", paste(exclude, collapse = ", "))) - port4me(exclude = c(30845, 32310)) > port <- port4me(exclude = exclude) > print(port) [1] 19654 > stopifnot( + length(port) == 1L, + is.integer(port), + is.finite(port), + port > 0L, + port <= 65535L, + port >= 1024L, + port == setdiff(truth, exclude)[1] + ) > > > message(sprintf("- port4me() with PORT4ME_EXCLUDE=%s", paste(exclude, collapse = ", "))) - port4me() with PORT4ME_EXCLUDE=30845, 32310 > Sys.setenv(PORT4ME_EXCLUDE = paste(exclude, collapse = ",")) > port <- port4me() > print(port) [1] 19654 > stopifnot( + length(port) == 1L, + is.integer(port), + is.finite(port), + port > 0L, + port <= 65535L, + port >= 1024L, + port == setdiff(truth, exclude)[1] + ) > Sys.unsetenv("PORT4ME_EXCLUDE") > > > include <- c(2000:2123, 4321, 10000:10999) > message("- port4me(include = c(2000:2123, 4321, 10000:10999))") - port4me(include = c(2000:2123, 4321, 10000:10999)) > port <- port4me(include = include) > print(port) [1] 10451 > stopifnot( + length(port) == 1L, + is.integer(port), + is.finite(port), + port > 0L, + port <= 65535L, + port >= 1024L, + port == 10451L + ) > > > include <- c(2000:2123, 4321, 10000:10999) > message("- port4me() with PORT4ME_INCLUDE=...") - port4me() with PORT4ME_INCLUDE=... > Sys.setenv(PORT4ME_INCLUDE = paste(include, collapse = ",")) > port <- port4me() > print(port) [1] 10451 > stopifnot( + length(port) == 1L, + is.integer(port), + is.finite(port), + port > 0L, + port <= 65535L, + port >= 1024L, + port == 10451L + ) > Sys.unsetenv("PORT4ME_INCLUDE") > > > message('- port4me(exclude = "1024-1099")') - port4me(exclude = "1024-1099") > port <- port4me(exclude = "1024-1099") > > message('- port4me(include = "1024-1099")') - port4me(include = "1024-1099") > port <- port4me(include = "1024-1099") > > message('- port4me(prepend = "1024-1099")') - port4me(prepend = "1024-1099") > port <- port4me(prepend = "1024-1099") > > prepend <- c(2000:2123, 4321, 10000:10999) > message("- port4me(prepend = c(2000:2123, 4321, 10000:10999))") - port4me(prepend = c(2000:2123, 4321, 10000:10999)) > port <- port4me(prepend = prepend) > print(port) [1] 2000 > stopifnot( + length(port) == 1L, + is.integer(port), + is.finite(port), + port > 0L, + port <= 65535L, + port >= 1024L, + port == prepend[1] + ) > > > > message("- port4me(skip = 1L)") - port4me(skip = 1L) > port <- port4me(user = "alice", skip = 1L) > print(port) [1] 19654 > stopifnot( + length(port) == 1L, + is.integer(port), + is.finite(port), + port > 0L, + port <= 65535L, + port >= 1024L, + port == 19654L + ) > > > # ------------------------------------------------------- > # Check TCP port > # ------------------------------------------------------- > message("- port4me() can detect busy port") - port4me() can detect busy port > Sys.unsetenv("_PORT4ME_CHECK_AVAILABLE_PORTS_") > port <- NA_integer_ > if (.Platform[["OS.type"]] == "unix" && Sys.info()[["sysname"]] != "Darwin") { + ## Start dynamic help, if not already running, and get its port + ## HELP WANTED: On both macOS and MS Windows, this port is still + ## available. Why? + ## system("python -m port4me --test=") confirms this. + ## /HB 2024-01-06 + port <- tools::startDynamicHelp(NA) + message("Dynamic help port: ", port) + } > > if (!is.na(port)) { + Sys.setenv(PORT4ME_DEBUG = "true") + + res <- port4me(test = port) + message(sprintf("port4me(test = %d) == %s", port, res)) + stopifnot(identical(res, FALSE)) + + Sys.setenv(PORT4ME_TEST = port) + res <- port4me() + message(sprintf("Sys.setenv('PORT4ME_TEST'='%d'); port4me() == %s", port, res)) + stopifnot(identical(res, FALSE)) + Sys.unsetenv("PORT4ME_TEST") + + res <- tryCatch({ + port4me(include = port, exclude = setdiff(1:65535, port), max_tries = 1L) + }, error = identity) + stopifnot(inherits(res, "error")) + + res <- port4me(include = port, exclude = setdiff(1:65535, port), max_tries = 1L, must_work = FALSE) + stopifnot(res == -1L) + + Sys.unsetenv("PORT4ME_DEBUG") + } else { + message("Skipping; don't know how to test on ", sQuote(.Platform[["OS.type"]])) + } Skipping; don't know how to test on 'windows' > > > > # ------------------------------------------------------- > # Exceptions > # ------------------------------------------------------- > message('- port4me(user = "") produces an error') - port4me(user = "") produces an error > res <- tryCatch({ + port4me(user = "") + }, error = identity) > stopifnot(inherits(res, "error")) > > > message('- port4me(include = "") produces an error') - port4me(include = "") produces an error > res <- tryCatch({ + port4me(include = "") + }, error = identity) > stopifnot(inherits(res, "error")) > > > > # ------------------------------------------------------- > # Addition tests to increase test coverage > # ------------------------------------------------------- > message('- port4me() with PORT4ME_DEBUG = true') - port4me() with PORT4ME_DEBUG = true > Sys.setenv(PORT4ME_DEBUG = "true") > port <- port4me() seed_str='alice' seed=92903040 List of 3 $ include: int(0) $ exclude: int [1:80] 1 7 9 11 13 15 17 19 20 21 ... $ prepend: int(0) port4me:::is_tcp_port_available(30845): available (can bind and listen) > print(port) [1] 30845 > > proc.time() user system elapsed 0.28 0.09 0.37