tests/n.dispatcher.test.R

require(qrmarkdown)
require(testthat)
#undebug(q.dispatcher)

cleanlog <- function(testdir='testlog')
{
  if(file.exists(testdir))
    system(sprintf('rm -r %s',testdir))

  q.wd(testdir)
  q.show()
}

cleanup <- function(testdir='testlog')
{
  if(file.exists(testdir))
    system(sprintf('rm -r %s',testdir))
}

# dispatcher works on single queue direcotry,
# threads safe read, write with using locking

test1 <- function()
{
  cleanlog()

  ii = q.push(code='ls()')
  q.dispatcher(wait = TRUE)

  expect(q.wait(ii), 'job not completed')


  q.shutdown()

  q.dispatcher(wait = TRUE)
}




test_in_background <- function()
{

cleanlog()

ii = q.push(code='ls()')
q.dispatcher(wait = FALSE, timer = 4)
expect(q.wait(ii), 'job not completed')
q.shutdown()
}


test_in_background_manyjob <- function()
{

  cleanlog()

  for(i in 1:2)
  ii = q.push(code='ls()')

  q.dispatcher(wait = FALSE,  timer = 4)
  q.shutdown()

  expect(q.wait(ii), 'job not completed')
  res <- q.show()
  expect(res$outbox == 2, 'job not completed')
}


test_in_background_manydispatcher <- function()
{

  cleanlog()

  for(i in 1:2)
    ii = q.push(code='ls()')

  q.dispatcher(n=5, wait = FALSE, timer = 4)
  q.show()

  expect(q.wait(ii), 'job not completed')
  res <- q.show()
  expect(res$outbox == 2, 'job not completed')

}

test_two_qwd_queue <- function()
{
  cleanlog('testlog/app1')
  for(i in 1:2)
    ii = q.push(code='ls()')
  q.dispatcher(n=5, timer = 4) # dispatcher for app1

  cleanlog('testlog/app2')
  for(i in 1:3)
    ii = q.push(code='ls()')
  q.dispatcher(n=5, timer = 4) # dispatcher for app2

  Sys.sleep(10)

  q.setwd('testlog/app1')
  res1 <- q.show()

  q.setwd('testlog/app2')
  res2 <- q.show()

  Total <- res1$outbox + res2$outbox
  expect(Total == 5, 'invalid job completion')

}



run.all <- function()
{
  test_two_qwd_queue()

  test1()
  test_in_background()
  test_in_background_manyjob()
  test_in_background_manydispatcher()
  cleanup()
}

run.all()
okux/qrmarkdown documentation built on Dec. 22, 2021, 4:17 a.m.