Skip to content

Commit e4eb662

Browse files
authored
Internal refactoring in preparation for NNG v2.0 (#480)
* Ignore files * Relax tests * Refactor internal `sub_real_port()` * Cater for possible changes in `stop_aio()` error code
1 parent cde1bf8 commit e4eb662

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
^[\.]?air\.toml$
1313
^\.vscode$
1414
^cran-comments\.md$
15+
^CLAUDE.md$

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ inst/doc
77
.vscode
88
air.toml
99
revdep/
10+
CLAUDE.md
11+
.DS_Store

R/daemons.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -790,15 +790,21 @@ launch_daemons <- function(seq, dots, envir) {
790790
pipe_notify(sock, NULL, add = TRUE)
791791
}
792792

793-
sub_real_port <- function(port, url) sub("(?<=:)0(?![^/])", port, url, perl = TRUE)
793+
sub_real_port <- function(sock, url) {
794+
if (parse_url(url)[["port"]] == "0") {
795+
url <- sub(
796+
"(?<=:)0(?![^/])",
797+
opt(attr(sock, "listener")[[1L]], "tcp-bound-port"),
798+
url,
799+
perl = TRUE
800+
)
801+
}
802+
url
803+
}
794804

795805
create_sock <- function(envir, url, tls) {
796806
sock <- req_socket(url, tls = tls)
797-
listener <- attr(sock, "listener")[[1L]]
798-
url <- opt(listener, "url")
799-
if (parse_url(url)[["port"]] == "0") {
800-
url <- sub_real_port(opt(listener, "tcp-bound-port"), url)
801-
}
807+
url <- sub_real_port(sock, url)
802808
`[[<-`(envir, "cv", cv())
803809
`[[<-`(envir, "sock", sock)
804810
`[[<-`(envir, "url", url)

R/dispatcher.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ dispatcher <- function(host, url = NULL, n = 0L, ...) {
8181
}
8282
} else {
8383
listen(psock, url = url, tls = tls, fail = 2L)
84-
listener <- attr(psock, "listener")[[1L]]
85-
url <- opt(listener, "url")
86-
if (parse_url(url)[["port"]] == "0") {
87-
url <- sub_real_port(opt(listener, "tcp-bound-port"), url)
88-
}
84+
url <- sub_real_port(psock, url)
8985
}
9086
send(sock, url, mode = 2L, block = TRUE)
9187

tests/tests.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ test_error(mirai(), "missing expression, perhaps wrap in {}?")
2525
test_error(mirai(a, 1), "all `...` arguments must be named")
2626
test_error(mirai(a, .args = list(1)), "all items in `.args` must be named")
2727
test_error(mirai_map(1:2, identity))
28-
test_error(daemons(url = "URL"), "Invalid argument")
28+
test_error(daemons(url = "URL"))
2929
test_error(daemons(-1), "zero or greater")
3030
test_error(daemons(raw(0L)), "must be numeric")
3131
test_error(daemons(1, dispatcher = ""))
3232
test_error(daemons(url = local_url(), dispatcher = NA))
33-
test_error(daemon("URL"), "Invalid argument")
33+
test_error(daemon("URL"))
3434
test_error(launch_local(1L), "daemons must be set")
3535
test_error(race_mirai(list()), "daemons must be set")
3636
test_false(daemons_set())
@@ -389,13 +389,13 @@ connection && Sys.getenv("NOT_CRAN") == "true" && {
389389
Sys.sleep(0.1)
390390
test_true(stop_mirai(m2))
391391
test_true(stop_mirai(m1))
392-
test_equal(m2$data, 20L)
393-
test_equal(m1$data, 20L)
392+
test_true(m2$data >= 20L)
393+
test_true(m1$data >= 20L)
394394
test_class("errorValue", mirai(res)[])
395395
m <- mirai_map(1:10, function(x) { Sys.sleep(2); y <<- TRUE })
396396
Sys.sleep(0.1)
397397
s <- stop_mirai(m)
398-
test_equal(sum(unlist(m[])), 200L)
398+
test_true(all(m[] >= 20L))
399399
test_class("errorValue", mirai(y)[])
400400
test_identical(s, !logical(10L))
401401
test_equal(status()$connections, 1L)

0 commit comments

Comments
 (0)