tests/testthat/test-toxics.R

context("toxics")

test_that("latency", {
  tox <- latency(1)
  expect_is(tox, "toxic")
  expect_equal(tox$type, "latency")
  expect_equal(tox$attributes$latency, 1)
  expect_equal(tox$attributes$jitter, 0)
  ## These are not filled
  expect_null(tox$name)
  expect_null(tox$stream)
  expect_null(tox$toxicity)

  tox <- latency(1.2, 3.4)
  expect_equal(tox$attributes$latency, 1.2)
  expect_equal(tox$attributes$jitter, 3.4)

  expect_error(latency(-1), "'latency' must not be negative")
  expect_error(latency(1, -1), "'jitter' must not be negative")
})

test_that("latency, set optionals", {
  tox <- latency(1, name = "foo", direction = "upstream", probability = 0.5)
  expect_is(tox, "toxic")
  expect_equal(tox$name, "foo")
  expect_equal(tox$stream, "upstream")
  expect_equal(tox$toxicity, 0.5)
})

test_that("bandwidth", {
  tox <- bandwidth(1)
  expect_is(tox, "toxic")
  expect_equal(tox$type, "bandwidth")
  expect_equal(tox$attributes$rate, 1)

  expect_error(bandwidth(-1), "'rate' must not be negative")
})

test_that("bandwidth, set optionals", {
  tox <- bandwidth(1, name = "foo", direction = "upstream", probability = 0.5)
  expect_is(tox, "toxic")
  expect_equal(tox$name, "foo")
  expect_equal(tox$stream, "upstream")
  expect_equal(tox$toxicity, 0.5)
})

test_that("slow_close", {
  tox <- slow_close(1)
  expect_is(tox, "toxic")
  expect_equal(tox$type, "slow_close")
  expect_equal(tox$attributes$delay, 1)

  expect_error(slow_close(-1), "'delay' must not be negative")
})

test_that("slow_close, set optionals", {
  tox <- slow_close(1, name = "foo", direction = "upstream", probability = 0.5)
  expect_is(tox, "toxic")
  expect_equal(tox$name, "foo")
  expect_equal(tox$stream, "upstream")
  expect_equal(tox$toxicity, 0.5)
})

test_that("timeout", {
  tox <- timeout(1)
  expect_is(tox, "toxic")
  expect_equal(tox$type, "timeout")
  expect_equal(tox$attributes$timeout, 1)

  expect_error(timeout(-1), "'timeout' must not be negative")
})

test_that("timeout, set optionals", {
  tox <- timeout(1, name = "foo", direction = "upstream", probability = 0.5)
  expect_is(tox, "toxic")
  expect_equal(tox$name, "foo")
  expect_equal(tox$stream, "upstream")
  expect_equal(tox$toxicity, 0.5)
})

test_that("slicer", {
  tox <- slicer(1)
  expect_is(tox, "toxic")
  expect_equal(tox$type, "slicer")
  expect_equal(tox$attributes$average_size, 1)
  expect_equal(tox$attributes$size_variation, 0)
  expect_equal(tox$attributes$delay, 0)

  tox <- slicer(1.2, 2.3, 3.4)
  expect_equal(tox$attributes$average_size, 1.2)
  expect_equal(tox$attributes$size_variation, 2.3)
  expect_equal(tox$attributes$delay, 3.4)

  expect_error(slicer(-1), "'average_size' must not be negative")
})

test_that("slicer, set optionals", {
  tox <- slicer(1, name = "foo", direction = "upstream", probability = 0.5)
  expect_is(tox, "toxic")
  expect_equal(tox$name, "foo")
  expect_equal(tox$stream, "upstream")
  expect_equal(tox$toxicity, 0.5)
})

test_that("limit_data", {
  tox <- limit_data(1)
  expect_is(tox, "toxic")
  expect_equal(tox$type, "limit_data")
  expect_equal(tox$attributes$bytes, 1)

  expect_error(limit_data(-1), "'bytes' must not be negative")
})

test_that("limit_data, set optionals", {
  tox <- limit_data(1, name = "foo", direction = "upstream", probability = 0.5)
  expect_is(tox, "toxic")
  expect_equal(tox$name, "foo")
  expect_equal(tox$stream, "upstream")
  expect_equal(tox$toxicity, 0.5)
})

test_that("print", {
  expect_output(
    print(bandwidth(1)),
    "'bandwidth' toxic <(unnamed)> {rate: 1}; (downstream), pr: (1)",
                fixed = TRUE)
  expect_output(print(timeout(1.23, name = "yo",
                              direction = "upstream", probability = 0)),
                "'timeout' toxic <yo> {timeout: 1.23}; upstream, pr: 0",
                fixed = TRUE)
})
richfitz/toxiproxyr documentation built on May 25, 2017, 3:10 a.m.