11context(" Cache" )
22
33
4+ # generate lexically sortable ids. For equal timestamp, Cache$files is sorted
5+ # by id, so that the tests do not fail on file systems with low-accuracy
6+ # timestamps
7+
8+
9+ .id_cache <- new.env()
10+ assign(" id" , 0L , .id_cache )
11+ ascending_id <- function (){
12+ x <- get(" id" , .id_cache )
13+ x <- as.character(as.integer(x ) + 1L )
14+ assign(" id" , x , .id_cache )
15+ x
16+ }
17+
18+
419test_that(" Cache works as expected" , {
520 td <- file.path(tempdir(), " cache-test" )
621 on.exit(unlink(td , recursive = TRUE ))
@@ -47,14 +62,14 @@ test_that("setting hash functions work", {
4762
4863
4964 # To override this behaviour use a generate for unique ids, such as
50- cache_uid <- Cache $ new(td , hashfun = function (x ) uuid :: UUIDgenerate ())
65+ cache_uid <- Cache $ new(td , hashfun = function (x ) ascending_id ())
5166 cache_uid $ push(iris )
5267 cache_uid $ push(iris )
5368 expect_identical(cache_hash $ n , 2L )
5469 cache_hash $ purge()
5570
5671 # ensure hashfun allways returns a scalar
57- cache_err <- Cache $ new(td , hashfun = function (x ) uuid :: UUIDgenerate( n = 2 ))
72+ cache_err <- Cache $ new(td , hashfun = function (x ) c(ascending_id(), ascending_id() ))
5873 expect_error(cache_err $ push(iris ), class = " ValueError" )
5974})
6075
@@ -66,13 +81,9 @@ test_that("pruning works by number of files works", {
6681 td <- file.path(tempdir(), " cache-test" )
6782 on.exit(unlink(td , recursive = TRUE ))
6883
69- # When using a real hash function as hashfun, identical objects will only
70- # be added to the cache once
71- cache <- Cache $ new(td , hashfun = function (x ) uuid :: UUIDgenerate())
84+ cache <- Cache $ new(td , hashfun = function (x ) ascending_id())
7285 k1 <- cache $ push(iris )
73- Sys.sleep(1 )
7486 k2 <- cache $ push(letters )
75- Sys.sleep(1 )
7687 k3 <- cache $ push(cars )
7788 expect_identical(cache $ n , 3L )
7889
@@ -81,7 +92,6 @@ test_that("pruning works by number of files works", {
8192 expect_identical(cache $ files $ key [[3 ]], k3 )
8293
8394 cache $ prune(max_files = 2 )
84- cache $ files
8595 expect_identical(cache $ read(cache $ files $ key [[1 ]]), letters )
8696 expect_identical(cache $ read(cache $ files $ key [[2 ]]), cars )
8797 cache $ purge()
@@ -96,7 +106,7 @@ test_that("pruning by size works", {
96106
97107 # When using a real hash function as hashfun, identical objects will only
98108 # be added to the cache once
99- cache <- Cache $ new(td , hashfun = function (x ) uuid :: UUIDgenerate ())
109+ cache <- Cache $ new(td , hashfun = function (x ) ascending_id ())
100110 cache $ push(iris )
101111 Sys.sleep(0.1 )
102112 cache $ push(iris )
@@ -128,7 +138,7 @@ test_that("Inf max_* do not prunes", {
128138
129139 # When using a real hash function as hashfun, identical objects will only
130140 # be added to the cache once
131- cache <- Cache $ new(td , hashfun = function (x ) uuid :: UUIDgenerate ())
141+ cache <- Cache $ new(td , hashfun = function (x ) ascending_id ())
132142 cache $ push(iris )
133143 Sys.sleep(0.1 )
134144 cache $ push(iris )
@@ -194,7 +204,7 @@ test_that("pruning by age works", {
194204 )
195205 )
196206
197- cache <- MockCache $ new(dir = td , hashfun = function (x ) uuid :: UUIDgenerate ())
207+ cache <- MockCache $ new(dir = td , hashfun = function (x ) ascending_id ())
198208 on.exit(cache $ purge(), add = TRUE )
199209 cache $ push(iris )
200210 Sys.sleep(0.1 )
0 commit comments