Debian Codegraph @

inst/testme/test-freePort.R

raw
1 library(parallelly)
2
3 message("*** initialize_internet() ...")
4 initialize_internet <- parallelly:::initialize_internet
5 environment(initialize_internet)$done <- FALSE
6 message("*** initialize_internet() ... DONE")
7
8 message("*** freePort() ...")
9
10 # Make sure to undo any changes at the end
11 oenv <- Sys.getenv("R_PARALLELLY_RANDOM_PORTS")
12 Sys.setenv(R_PARALLELLY_DEBUG = "TRUE")
13
14 set.seed(42)
15 rng <- .Random.seed
16
17 for (kk in 1:5) {
18 port <- freePort()
19 message("A random free TCP port: ", port)
20 stopifnot(is.integer(port), length(port) == 1L)
21 if (!is.na(port)) stopifnot(port >= 0L, port <= 65535L)
22 stopifnot(identical(.Random.seed, rng))
23 }
24
25 # Assert that 'default' is a valid TCP port
26 port <- freePort(default = 1024L)
27 stopifnot(is.integer(port), length(port) == 1L)
28
29 port <- tryCatch(freePort(default = -1L), error = identity)
30 stopifnot(inherits(port, "error"))
31
32 port <- freePort(ports = integer(0L), default = 1024L)
33 stopifnot(is.integer(port), length(port) == 1L, port == 1024L)
34
35
36 message("- freePort('auto')")
37
38 Sys.unsetenv("R_PARALLEL_PORT")
39 port <- freePort("auto")
40 message("A random free TCP port: ", port)
41
42 message("- freePort('auto') with env var R_PARALLEL_PORT = 8888")
43 Sys.setenv(R_PARALLEL_PORT = 8888L)
44 port <- freePort("auto")
45 message("A free TCP port: ", port)
46 stopifnot(port == 8888L)
47
48 Sys.setenv(R_PARALLEL_PORT = "invalid")
49 cond <- NULL
50 withCallingHandlers({
51 port <- freePort("auto")
52 }, warning = function(c) {
53 cond <<- c
54 })
55 stopifnot(is.integer(port), length(port) == 1L)
56 stopifnot(inherits(cond, "warning"))
57
58 Sys.unsetenv("R_PARALLEL_PORT")
59
60
61 # Get a random, free TCP port in 1024:65535
62 port <- freePort()
63 message("A free TCP port: ", port)
64
65
66 # Get a random, free TCP port in 11000:11999, which is what
67 # parallelly::makeClusterPSOCK() and parallel::makePSOCKcluster()
68 # default to (but the latter does not make sure it is available)
69 Sys.unsetenv("R_PARALLELLY_RANDOM_PORTS")
70 port <- freePort("random")
71 message("A free TCP port: ", port)
72 stopifnot(is.integer(port), length(port) == 1L, port %in% 11000:11999)
73
74 # Customize the range of ports to sample from to 30000:50000
75 Sys.setenv(R_PARALLELLY_RANDOM_PORTS = "30000:50000")
76 port <- freePort("random")
77 message("A free TCP port: ", port)
78 stopifnot(is.integer(port), length(port) == 1L, port %in% 30000:50000)
79
80 # Customize the range of ports to sample from to invalid port range
81 Sys.setenv(R_PARALLELLY_RANDOM_PORTS = "invalid:50000")
82 cond <- NULL
83 withCallingHandlers({
84 port <- freePort("random")
85 }, warning = function(c) {
86 cond <<- c
87 })
88 stopifnot(is.integer(port), length(port) == 1L)
89 stopifnot(inherits(cond, "warning"))
90
91 # Customize the range of ports to sample from to ports out of range
92 Sys.setenv(R_PARALLELLY_RANDOM_PORTS = "99999:99999")
93 cond <- NULL
94 withCallingHandlers({
95 port <- freePort("random")
96 }, warning = function(c) {
97 cond <<- c
98 })
99 stopifnot(is.integer(port), length(port) == 1L)
100 stopifnot(inherits(cond, "warning"))
101
102
103 # Test if a specific port is free
104 isPortFree <- function(port) !is.na(freePort(port, default = NA_integer_))
105
106 Sys.setenv("R_PARALLELLY_DEBUG" = "true")
107 Sys.unsetenv("_R_PARALLELLY_CHECK_AVAILABLE_PORTS_")
108 free <- isPortFree(1024)
109 message("TCP port 1024 is free: ", free)
110 stopifnot(is.logical(free), length(free) == 1L, !is.na(free))
111
112 # Fake port availability
113 Sys.setenv("_R_PARALLELLY_CHECK_AVAILABLE_PORTS_" = "any")
114 free <- isPortFree(1)
115 message("TCP port 1 is free: ", free)
116 stopifnot(is.logical(free), length(free) == 1L, !is.na(free), isTRUE(free))
117
118 Sys.setenv("_R_PARALLELLY_CHECK_AVAILABLE_PORTS_" = "invalid")
119 free <- tryCatch(isPortFree(1), error = identity)
120 stopifnot(inherits(free, "error"))
121
122 Sys.unsetenv("R_PARALLELLY_DEBUG")
123
124 # Undo changes
125 Sys.setenv(R_PARALLELLY_RANDOM_PORTS = oenv)
126
127 message("*** freePort() ... DONE")
128
129
130