tests/test-resonance-api.R

# This file is used for low-level Resonance-Api test
# The test is pretty straightforward - we are expecting certain calls in certain order
# and test that they are performed in that order.

# Expected order
# onPrepare()
# onStart()
# onDataBlock()
# popQueue
# onStop()

require(nanotime)

testReport <- list()
queue <- list()

`SI<-` <- function(x, name=NULL, value){
  if(is.null(name)) {
    attr(x, '.StreamInfo') <- value
  } else {
    attr(x, '.StreamInfo')[[name]] <- value
  }
  x
}
TS <- function(x){
  attr(x, 'TS', TRUE)
}
`TS<-` <- function(x, value){
  attr(x, 'TS') <- nanotime(value)
  x
}

DB.channels <- function(SI, timestamp, vector){
  data <- if(is.matrix(vector)) vector else matrix(as.numeric(vector), ncol=SI$channels, byrow = T)
  if(length(timestamp)==1){
    TS(data) <- nanotime(seq(to=as.integer64(timestamp), by=1E9/SI$samplingRate, length.out=nrow(data)))
  } else {
    TS(data) <- nanotime(timestamp)
  }
  SI(data) <- SI
  data
}

DB.event <- function(SI, timestamp, message){
  TS(message) <- nanotime(timestamp)
  ret <- list(message)
  SI(ret) <- SI
  ret
}

SI.default <- function(id, name){
  ret <- list()
  if(!is.null(id)) ret$id <- id
  if(!is.null(name)) ret$name <- name
  ret
}

SI.channels <- function(channels, samplingRate, id=NULL, name=NULL){
  c(SI.default(id, name),list(
    type='channels',
    channels=as.integer(channels),
    samplingRate=as.numeric(samplingRate)
  ))
}

SI.event <- function(id=NULL, name=NULL){
  c(SI.default(id, name),list(
    type='event'
  ))
}

onPrepare <- function(inputs, code, env=new.env()){
  if(length(testReport) != 0){ 
    testReport <<- list()
    stop("Sequence started wrong")
  }
  testReport <<- list(list(call="onPrepare", args=list(inputs, code)))
  queue <<- list(
    
    list(
      cmd="createOutputStream",
      args=list(
        id=1L,
        type="channels",
        name="channels-out",
        channels=3L,
        samplingRate=21
      )
    ),
    list(
      cmd="createOutputStream",
      args=list(
        id=2L,
        type="event",
        name="event-out"
      )
    )
  )
}

onStart <- function(){
  testReport <<- c(testReport, list(list(call='onStart')))
}

onDataBlock <- function(block){
  testReport <<- c(testReport, list(list(call='onDataBlock', args=list(block))))
  
  if(attr(block, '.StreamInfo')$id==1)
  {
    data <- block
    data[,2] <- -data[,2]
    
    queue <<- c(
      queue,
      list(
        list(
          cmd="sendBlockToStream",
          args=list(
            id=1L,
            data=data
          )
        )
      )
    )
  }
  if(attr(block, '.StreamInfo')$id==2)
  {
    data <- paste(block[[1]], "out")
    TS(data) <- TS(block[[1]])
    queue <<- c(
      queue,
      list(
        list(
          cmd="sendBlockToStream",
          args=list(
            id=2L,
            data=data
          )
        )
      )
    )
  }
}

popQueue <- function(){
  testReport <<- c(testReport, list(list(call='popQueue')))
  ret <- queue
  queue <<- list()
  ret
}

onStop <- function(){
  testReport <- c(testReport, list(list(call='onStop')))
  
  expectedReport <- list(
    list(
      call = "onPrepare",
      args = list(
        list(
          list(
            id = 1,
            name = "channels", 
            type = "channels",
            channels = 3L,
            samplingRate = 21,
            online = TRUE
          ), 
          list(
            id = 2, 
            name = "event", 
            type = "event",
            online = TRUE
          )
        ),
        "")
    ),
    list(call = "popQueue"),
    list(call = "onStart"),
    list(call = "popQueue"),
    list(
      call = "onDataBlock",
      args = list(
        structure(
          c(0.29475517441090415, 0.56332005806362195, 0.7818314824680298, 
            0.93087374864420414, 0.99720379718118013, 0.97492791218182362, 
            0.86602540378443871, 0.68017273777091969, 0.43388373911755823, 
            0.14904226617617472, -0.14904226617617447, -0.43388373911755801, 
            -0.68017273777091947, -0.86602540378443837, -0.97492791218182362, 
            -0.99720379718118024, -0.93087374864420447, -0.78183148246802991, 
            -0.56332005806362195, -0.29475517441090471, -2.4492935982947064e-16, 
            0.29475517441090426, 0.56332005806362229, 0.78183148246802958, 
            0.93087374864420425, 0.99720379718118013, 0.97492791218182373, 
            0.86602540378443915, 0.68017273777091924, 0.43388373911755845, 
            0.14904226617617364, -0.14904226617617292, -0.43388373911755779, 
            -0.68017273777091869, -0.86602540378443871, -0.97492791218182351, 
            -0.99720379718118024, -0.93087374864420425, -0.78183148246803014, 
            -0.56332005806362295, -0.29475517441090582, -4.8985871965894128e-16, 
            0.29475517441090315, 0.56332005806362206, 0.78183148246802947, 
            0.93087374864420447, 0.99720379718118013, 0.97492791218182373, 
            0.86602540378443837, 0.68017273777091936, 0.43388373911756023, 
            0.14904226617617389, -0.14904226617617267, -0.43388373911755757, 
            -0.6801727377709198, -0.86602540378443771, -0.97492791218182351, 
            -0.99720379718118013, -0.93087374864420491, -0.78183148246803025, 
            -0.56332005806362462, -0.29475517441090265, -7.3478807948841188e-16, 
            0.29475517441090415, 0.56332005806362195, 0.7818314824680298, 
            0.93087374864420414, 0.99720379718118013, 0.97492791218182362, 
            0.86602540378443871, 0.68017273777091969, 0.43388373911755823, 
            0.14904226617617472, -0.14904226617617447, -0.43388373911755801, 
            -0.68017273777091947, -0.86602540378443837, -0.97492791218182362, 
            -0.99720379718118024, -0.93087374864420447, -0.78183148246802991, 
            -0.56332005806362195, -0.29475517441090471, -2.4492935982947064e-16, 
            0.29475517441090426, 0.56332005806362229, 0.78183148246802958, 
            0.93087374864420425, 0.99720379718118013, 0.97492791218182373, 
            0.86602540378443915, 0.68017273777091924, 0.43388373911755845, 
            0.14904226617617364, -0.14904226617617292, -0.43388373911755779, 
            -0.68017273777091869, -0.86602540378443871, -0.97492791218182351, 
            -0.99720379718118024, -0.93087374864420425, -0.78183148246803014, 
            -0.56332005806362295, -0.29475517441090582, -4.8985871965894128e-16, 
            0.29475517441090315, 0.56332005806362206, 0.78183148246802947, 
            0.93087374864420447, 0.99720379718118013, 0.97492791218182373, 
            0.86602540378443837, 0.68017273777091936, 0.43388373911756023, 
            0.14904226617617389, -0.14904226617617267, -0.43388373911755757, 
            -0.6801727377709198, -0.86602540378443771, -0.97492791218182351, 
            -0.99720379718118013, -0.93087374864420491, -0.78183148246803025, 
            -0.56332005806362462, -0.29475517441090265, -7.3478807948841188e-16, 
            0.29475517441090415, 0.56332005806362195, 0.7818314824680298, 
            0.93087374864420414, 0.99720379718118013, 0.97492791218182362, 
            0.86602540378443871, 0.68017273777091969, 0.43388373911755823, 
            0.14904226617617472, -0.14904226617617447, -0.43388373911755801, 
            -0.68017273777091947, -0.86602540378443837, -0.97492791218182362, 
            -0.99720379718118024, -0.93087374864420447, -0.78183148246802991, 
            -0.56332005806362195, -0.29475517441090471, -2.4492935982947064e-16, 
            0.29475517441090426, 0.56332005806362229, 0.78183148246802958, 
            0.93087374864420425, 0.99720379718118013, 0.97492791218182373, 
            0.86602540378443915, 0.68017273777091924, 0.43388373911755845, 
            0.14904226617617364, -0.14904226617617292, -0.43388373911755779, 
            -0.68017273777091869, -0.86602540378443871, -0.97492791218182351, 
            -0.99720379718118024, -0.93087374864420425, -0.78183148246803014, 
            -0.56332005806362295, -0.29475517441090582, -4.8985871965894128e-16, 
            0.29475517441090315, 0.56332005806362206, 0.78183148246802947, 
            0.93087374864420447, 0.99720379718118013, 0.97492791218182373, 
            0.86602540378443837, 0.68017273777091936, 0.43388373911756023, 
            0.14904226617617389, -0.14904226617617267, -0.43388373911755757, 
            -0.6801727377709198, -0.86602540378443771, -0.97492791218182351, 
            -0.99720379718118013, -0.93087374864420491, -0.78183148246803025, 
            -0.56332005806362462, -0.29475517441090265, -7.3478807948841188e-16
          ),
          .Dim = c(63L, 3L),
          TS = nanotime::nanotime(c(
            "2000-01-01T12:30:02.297619086+00:00", "2000-01-01T12:30:02.345238133+00:00", "2000-01-01T12:30:02.392857180+00:00",
            "2000-01-01T12:30:02.440476227+00:00", "2000-01-01T12:30:02.488095274+00:00", "2000-01-01T12:30:02.535714321+00:00",
            "2000-01-01T12:30:02.583333368+00:00", "2000-01-01T12:30:02.630952415+00:00", "2000-01-01T12:30:02.678571462+00:00",
            "2000-01-01T12:30:02.726190509+00:00", "2000-01-01T12:30:02.773809556+00:00", "2000-01-01T12:30:02.821428603+00:00",
            "2000-01-01T12:30:02.869047650+00:00", "2000-01-01T12:30:02.916666697+00:00", "2000-01-01T12:30:02.964285744+00:00",
            "2000-01-01T12:30:03.011904791+00:00", "2000-01-01T12:30:03.059523838+00:00", "2000-01-01T12:30:03.107142885+00:00",
            "2000-01-01T12:30:03.154761932+00:00", "2000-01-01T12:30:03.202380979+00:00", "2000-01-01T12:30:03.250000026+00:00",
            "2000-01-01T12:30:03.297619073+00:00", "2000-01-01T12:30:03.345238120+00:00", "2000-01-01T12:30:03.392857167+00:00",
            "2000-01-01T12:30:03.440476214+00:00", "2000-01-01T12:30:03.488095261+00:00", "2000-01-01T12:30:03.535714308+00:00",
            "2000-01-01T12:30:03.583333355+00:00", "2000-01-01T12:30:03.630952402+00:00", "2000-01-01T12:30:03.678571449+00:00",
            "2000-01-01T12:30:03.726190496+00:00", "2000-01-01T12:30:03.773809543+00:00", "2000-01-01T12:30:03.821428590+00:00",
            "2000-01-01T12:30:03.869047637+00:00", "2000-01-01T12:30:03.916666684+00:00", "2000-01-01T12:30:03.964285731+00:00",
            "2000-01-01T12:30:04.011904778+00:00", "2000-01-01T12:30:04.059523825+00:00", "2000-01-01T12:30:04.107142872+00:00",
            "2000-01-01T12:30:04.154761919+00:00", "2000-01-01T12:30:04.202380966+00:00", "2000-01-01T12:30:04.250000013+00:00",
            "2000-01-01T12:30:04.297619060+00:00", "2000-01-01T12:30:04.345238107+00:00", "2000-01-01T12:30:04.392857154+00:00",
            "2000-01-01T12:30:04.440476201+00:00", "2000-01-01T12:30:04.488095248+00:00", "2000-01-01T12:30:04.535714295+00:00",
            "2000-01-01T12:30:04.583333342+00:00", "2000-01-01T12:30:04.630952389+00:00", "2000-01-01T12:30:04.678571436+00:00",
            "2000-01-01T12:30:04.726190483+00:00", "2000-01-01T12:30:04.773809530+00:00", "2000-01-01T12:30:04.821428577+00:00",
            "2000-01-01T12:30:04.869047624+00:00", "2000-01-01T12:30:04.916666671+00:00", "2000-01-01T12:30:04.964285718+00:00",
            "2000-01-01T12:30:05.011904765+00:00", "2000-01-01T12:30:05.059523812+00:00", "2000-01-01T12:30:05.107142859+00:00",
            "2000-01-01T12:30:05.154761906+00:00", "2000-01-01T12:30:05.202380953+00:00", "2000-01-01T12:30:05.250000000+00:00"
          )
          ),
          .StreamInfo = list(
            id = 1,
            name = "channels",
            type = "channels",
            channels = 3L, 
            samplingRate = 21, 
            online = TRUE
          )
        )
      )
    ),
    list(call = "popQueue"), 
    list(
      call = "onDataBlock",
      args = list(
        structure(
          list(
            structure(
              "test",
              TS = nanotime::nanotime(946729905250000000)
            )
          ),
          .StreamInfo = list(
            id = 2, 
            name = "event",
            type = "event",
            online = TRUE
          )
        )
      )
    ),
    list(call = "popQueue"), 
    list(call = "onStop")
  )
  
  if(!identical(testReport, expectedReport))
  {
    print(dump("testReport", file='', control = c("all", "digits17")))
    stop("Test didn't pass")
  }
  
}
tz-lom/Resonance-Rproj documentation built on July 1, 2019, 4:53 p.m.