Skip to content

Commit

Permalink
Fix #35 Use random ports for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Oct 22, 2018
1 parent ac508c3 commit 761a18d
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 41 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Description: A very flexible framework for building server side logic in R. The
as much as e.g. the shiny package does, but instead sets you free to create
your web app the way you want.
License: MIT + file LICENSE
Encoding: UTF-8
Imports:
R6,
assertthat,
Expand All @@ -39,7 +40,7 @@ Collate:
'fake_request.R'
'fiery-package.R'
'plugin_doc.R'
RoxygenNote: 6.0.1.9000
RoxygenNote: 6.1.0
Suggests:
testthat,
covr
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(logger_console)
export(logger_file)
export(logger_null)
export(logger_switch)
export(random_port)
importFrom(R6,R6Class)
importFrom(assertthat,assert_that)
importFrom(assertthat,has_args)
Expand Down
19 changes: 19 additions & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ globalVariables(
)
)

#' Select a random safe port
#'
#' This is a small utility function to get random safe ports to run your
#' application on. It chooses a port within the range that cannot be registeret
#' to IANA and thus is safe to assume are not in use.
#'
#' @return An integer in the range 49152-65535
#'
#' @export
#'
#' @examples
#' random_port()
#'
random_port <- function() {
low <- 49152
high <- 65535
as.integer(sample(high - low, 1) + low)
}

#' Convert a request to an ID
#'
#' @param request A Request object
Expand Down
5 changes: 2 additions & 3 deletions man/Fire.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/fiery-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/random_port.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 30 additions & 30 deletions tests/testthat/test-Fire.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
context("Fire")

test_that('handlers can be added, triggered and removed', {
app <- Fire$new()
app <- Fire$new(port = random_port())

triggerRes <- app$trigger('test')
expect_is(triggerRes, 'list')
Expand All @@ -23,7 +23,7 @@ test_that('handlers can be added, triggered and removed', {
})

test_that('Fire objects are printed', {
app <- Fire$new()
app <- Fire$new(port = random_port())
app$attach(list(
name = 'test',
on_attach = function(...) {}
Expand All @@ -37,13 +37,13 @@ test_that('Fire objects are printed', {
expect_output(print(app), 'start: 1')
expect_output(print(app), 'request: 2')

app <- Fire$new()
app <- Fire$new(port = random_port())
expect_output(print(app), 'Plugins attached: none')
expect_output(print(app), 'Event handlers added: none')
})

test_that('protected events cannot be triggered', {
app <- Fire$new()
app <- Fire$new(port = random_port())

protected <- c('start', 'resume', 'end', 'cycle-start',
'cycle-end', 'header', 'before-request', 'request',
Expand All @@ -56,7 +56,7 @@ test_that('protected events cannot be triggered', {
})

test_that('data can be set, get and removed', {
app <- Fire$new()
app <- Fire$new(port = random_port())
expect_null(app$get_data('test'))
testdata <- list(a = 1, b = 1:10, c = letters[6:10])
app$set_data('test', testdata)
Expand All @@ -68,7 +68,7 @@ test_that('data can be set, get and removed', {
})

test_that('plugins are being attached', {
app <- Fire$new()
app <- Fire$new(port = random_port())
app$set_data('test', 10)
plugin <- list(
on_attach = function(server, extraPar) {
Expand Down Expand Up @@ -98,7 +98,7 @@ test_that('plugins are being attached', {
})

test_that('id converter can be set and gets called', {
app <- Fire$new()
app <- Fire$new(port = random_port())
app$on('request', function(server, id, ...) {
server$set_data('id', id)
})
Expand All @@ -117,7 +117,7 @@ test_that('id converter can be set and gets called', {
})

test_that('active bindings work', {
app <- Fire$new()
app <- Fire$new(port = random_port())
expect_error(app$host <- 10)
expect_error(app$host <- letters[1:3])
app$host <- 'test'
Expand Down Expand Up @@ -146,7 +146,7 @@ test_that('active bindings work', {
})

test_that('life cycle events get fired', {
app <- Fire$new()
app <- Fire$new(port = random_port())
app$on('start', function(server, ...) {
server$set_data('events', c(server$get_data('events'), 'start'))
})
Expand Down Expand Up @@ -203,7 +203,7 @@ test_that('life cycle events get fired', {
})

test_that('request events fire', {
app <- Fire$new()
app <- Fire$new(port = random_port())
request <- fake_request('http://www.example.com')

app$on('before-request', function(server, ...) {
Expand All @@ -227,7 +227,7 @@ test_that('request events fire', {
})

test_that('message events fire', {
app <- Fire$new()
app <- Fire$new(port = random_port())
request <- fake_request('http://www.example.com')

app$on('before-message', function(server, ...) {
Expand All @@ -246,7 +246,7 @@ test_that('message events fire', {
expect_equal(app$get_data('events'), c('before', 'during', 'after'))
expect_equal(app$get_data('passed_args'), list(test = 4, message = 'test2'))

app <- Fire$new()
app <- Fire$new(port = random_port())
request <- fake_request('http://www.example.com')

app$on('message', function(server, message, arg_list, ...) {
Expand All @@ -262,7 +262,7 @@ test_that('message events fire', {
})

test_that('header event fire', {
app <- Fire$new()
app <- Fire$new(port = random_port())
request <- fake_request('http://www.example.com')
expect_true(is.null(app$test_header(request)))
app$on('header', function(server, ...) {
Expand All @@ -280,7 +280,7 @@ test_that('header event fire', {
})

test_that('errors in start and resume gets caught', {
app <- Fire$new()
app <- Fire$new(port = random_port())
app$set_logger(logger_console())
app$on('start', function(...) {
stop('Testing an error')
Expand All @@ -290,7 +290,7 @@ test_that('errors in start and resume gets caught', {
later::run_now()
}, 'Testing an error')
capture_output(app$extinguish())
app <- Fire$new()
app <- Fire$new(port = random_port())
app$set_logger(logger_console())
app$on('resume', function(...) {
stop('Testing an error')
Expand All @@ -303,7 +303,7 @@ test_that('errors in start and resume gets caught', {
})

test_that('futures can be added and called', {
app <- Fire$new()
app <- Fire$new(port = random_port())

app$delay({
10
Expand All @@ -328,7 +328,7 @@ test_that('futures can be added and called', {
app$remove_delay(id)
expect_silent(app$ignite(silent = TRUE))

app <- Fire$new()
app <- Fire$new(port = random_port())
app$time({
10
}, function(res, server, ...) {
Expand All @@ -344,7 +344,7 @@ test_that('futures can be added and called', {
})
expect_message(app$ignite(), '10')

app <- Fire$new()
app <- Fire$new(port = random_port())
id <- app$time({
10
}, function(res, server, ...) {
Expand All @@ -362,7 +362,7 @@ test_that('futures can be added and called', {
expect_silent(app$ignite(silent = TRUE))

skip_on_os('windows') # The async stuff fail on windows builders though it works fine locally
app <- Fire$new()
app <- Fire$new(port = random_port())
id <- app$async({
10
}, function(res, server, ...) {
Expand All @@ -379,7 +379,7 @@ test_that('futures can be added and called', {
})
expect_silent(app$ignite(silent = TRUE))

app <- Fire$new()
app <- Fire$new(port = random_port())
app$async({
10
}, function(res, server, ...) {
Expand All @@ -397,7 +397,7 @@ test_that('futures can be added and called', {
})

test_that('ignite is blocked during run', {
app <- Fire$new()
app <- Fire$new(port = random_port())
app$set_logger(logger_console())
app$refresh_rate_nb <- 0.001

Expand All @@ -410,7 +410,7 @@ test_that('ignite is blocked during run', {
})

test_that('external triggers are fired', {
app <- Fire$new()
app <- Fire$new(port = random_port())
app$set_logger(logger_console())

dir <- tempdir()
Expand All @@ -430,7 +430,7 @@ test_that('external triggers are fired', {
})

test_that('websockets are attached, and removed', {
app <- Fire$new()
app <- Fire$new(port = random_port())
req <- fake_request('http://www.example.com')
app$on('send', function(server, ...) {server$set_data('send', TRUE)})
expect_null(app$get_data('send'))
Expand All @@ -447,7 +447,7 @@ test_that('websockets are attached, and removed', {
test_that('showcase opens a browser', {
oldopt <- options(browser = function(url) message('Open browser'))

app <- Fire$new()
app <- Fire$new(port = random_port())
app$on('cycle-end', function(server, ...) server$extinguish())

expect_message(app$ignite(showcase = TRUE), 'Open browser')
Expand All @@ -457,7 +457,7 @@ test_that('showcase opens a browser', {
})

test_that('global headers are assigned and used', {
app <- Fire$new()
app <- Fire$new(port = random_port())
app$header('X-Powered-By', 'fiery')
app$header('X-XSS-Protection', '1; mode=block')
app$on('request', function(request, ...) {
Expand All @@ -472,7 +472,7 @@ test_that('global headers are assigned and used', {
})

test_that('app can be mounted at path', {
app <- Fire$new()
app <- Fire$new(port = random_port())
app$set_logger(logger_console())

expect_equal(app$root, '')
Expand Down Expand Up @@ -503,7 +503,7 @@ test_that('app can be mounted at path', {
})

test_that("Logging can be configured", {
app <- Fire$new()
app <- Fire$new(port = random_port())
expect_equal(app$access_log_format, common_log_format)
app$access_log_format <- combined_log_format
expect_equal(app$access_log_format, combined_log_format)
Expand All @@ -518,7 +518,7 @@ test_that("Logging can be configured", {
})

test_that('is_running works', {
app <- Fire$new()
app <- Fire$new(port = random_port())
expect_false(app$is_running())
app$on('cycle-start', function(server, ...) {
server$log('message', server$is_running())
Expand All @@ -529,15 +529,15 @@ test_that('is_running works', {
expect_message(app$ignite(), 'message: TRUE')
expect_false(app$is_running())

app <- Fire$new()
app <- Fire$new(port = random_port())
app$ignite(block = FALSE)
expect_true(app$is_running())
app$extinguish()
expect_false(app$is_running())
})

test_that('safe_call catches conditions', {
app <- Fire$new()
app <- Fire$new(port = random_port())
private <- environment(app$clone)$private
expect_message(private$safe_call(stop('error test')), 'error: error test')
expect_message(private$safe_call(warning('warning test')), 'warning: warning test')
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-FutureStack.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
context("FutureStack")

app <- Fire$new()
app <- Fire$new(port = random_port())

test_that('DelayStack works', {
catcher <- new.env()
Expand Down

1 comment on commit 761a18d

@gaborcsardi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is not perfect, there is a slight chance that this port is taken already. The correct solution is to try to bind to it, and see if that fails. Or to let the OS choose a port for you, but I am not sure if that's possible from R.

Please sign in to comment.