R Under development (unstable) (2023-08-18 r84986 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > > ## Version 1 using rredis > if (Sys.getenv("RunRcppRedisTests") == "yes" && requireNamespace("rredis", quietly=TRUE)) { + suppressMessages(require(rredis)) + ## redusMonitorChannels blocks forever until a message is received. We + ## use a background R process to send us some test messages. + publisher_rredis <- function() { + Rbin <- paste(R.home(component="bin"), "R", sep="/") + cmd <- "suppressMessages(require(rredis));redisConnect();Sys.sleep(1);redisPublish('channel1', charToRaw('1'));redisPublish('channel2', charToRaw('2'))" + args <- c("--slave", "-e", paste("\"", cmd, "\"", sep="")) + system(paste(c(Rbin, args), collapse=" "), intern=FALSE, wait=FALSE) + } + checkEquals <- function(x, y) if (!isTRUE(all.equal(x, y, check.attributes=FALSE))) stop() + + redisConnect() + redisPublish("channel1", charToRaw("A raw charachter data message example")) + redisPublish("channel2", charToRaw("A raw charachter data message example")) + + ## Define a callback function to process messages from channel 1: + channel1 <- function(x) { + cat("Message received from channel 1: ",x,"\n") + checkEquals("1", x) + } + ## Define a callback function to process messages from channel 2: + channel2 <- function(x) { + cat("Message received from channel 2: ",x,"\n") + checkEquals("2", x) + } + redisSubscribe(c('channel1','channel2')) + ## Monitor channels for a few seconds until the background R process sends us some messages... + publisher_rredis() + redisMonitorChannels() + redisMonitorChannels() + redisUnsubscribe(c('channel1','channel2')) + + cat("Done rredis\n") + } > > ## Version 2 using RcppRedis > if (Sys.getenv("RunRcppRedisTests") == "yes") { + suppressMessages(library(RcppRedis)) + publisher_rcppredis <- function() { + Rscript <- paste(R.home(component="bin"), "Rscript", sep="/") + cmd <- "suppressMessages(library(RcppRedis)); r <- new(Redis); Sys.sleep(1); r\\$publish('channel1', '1'); r\\$publish('channel2', '2')" + args <- c("-e", paste("\"", cmd, "\"", sep="")) + system(paste(c(Rscript, args), collapse=" "), intern=FALSE, wait=FALSE) + } + + checkEquals <- function(x, y) if (!isTRUE(all.equal(x, y, check.attributes=FALSE))) stop() + + redis <- new(Redis) + redis$publish("channel1", charToRaw("A raw charachter data message example")) + redis$publish("channel2", charToRaw("A raw charachter data message example")) + + ## Define a callback function to process messages from channel 1: + channel1 <- function(x) { + cat("Message received from channel 1: ",x,"\n") + checkEquals("1", x) + } + ## Define a callback function to process messages from channel 2: + channel2 <- function(x) { + cat("Message received from channel 2: ",x,"\n") + checkEquals("2", x) + } + redis$subscribe(c('channel1','channel2')) + ## Monitor channels for a few seconds until the background R process sends us some messages... + publisher_rcppredis() + redisMonitorChannels(redis) + redisMonitorChannels(redis) + redis$unsubscribe(c('channel1','channel2')) + + cat("Done rcppredis\n") + } > > proc.time() user system elapsed 0.23 0.07 0.25