@@ -60,14 +60,14 @@ test_that("setting hash functions work", {
6060 expect_identical(cache_hash $ n , 0L )
6161
6262
63- # To override this behaviour use a generate for unique ids, such as
63+ # To override this behaviour use a function that generates globally unique ids instead of hashes
6464 cache_uid <- Cache $ new(td , hashfun = function (x ) ascending_id())
6565 cache_uid $ push(iris )
6666 cache_uid $ push(iris )
6767 expect_identical(cache_hash $ n , 2L )
6868 cache_hash $ purge()
6969
70- # ensure hashfun allways returns a scalar
70+ # fail if hashfun does not returns a scalar
7171 cache_err <- Cache $ new(td , hashfun = function (x ) c(ascending_id(), ascending_id()))
7272 expect_error(cache_err $ push(iris ), class = " ValueError" )
7373})
@@ -86,6 +86,7 @@ test_that("pruning works by number of files works", {
8686 k3 <- cache $ push(cars )
8787 expect_identical(cache $ n , 3L )
8888
89+ # cached files are sorted in the order of their creation
8990 expect_identical(cache $ files $ key [[1 ]], k1 )
9091 expect_identical(cache $ files $ key [[2 ]], k2 )
9192 expect_identical(cache $ files $ key [[3 ]], k3 )
@@ -98,7 +99,7 @@ test_that("pruning works by number of files works", {
9899
99100
100101
101- test_that(" pruning works by number of files sorts by key if timestamp are identical" , {
102+ test_that(" $ files is ordered by key if timestamps are identical" , {
102103 td <- file.path(tempdir(), " cache-test" )
103104 on.exit(unlink(td , recursive = TRUE ))
104105
@@ -125,26 +126,22 @@ test_that("pruning works by number of files sorts by key if timestamp are identi
125126
126127
127128
128- test_that(" pruning by size works" , {
129+ test_that(" pruning by size works, even if timestamps are identical " , {
129130 td <- file.path(tempdir(), " cache-test" )
130131 on.exit(unlink(td , recursive = TRUE ))
131132
132133 # When using a real hash function as hashfun, identical objects will only
133134 # be added to the cache once
134135 cache <- Cache $ new(td , hashfun = function (x ) ascending_id())
135- cache $ push(iris )
136- Sys.sleep(0.1 )
137- cache $ push(iris )
138- Sys.sleep(0.1 )
139- cache $ push(iris )
140- Sys.sleep(0.1 )
141- cache $ push(iris )
142- Sys.sleep(0.1 )
143- cache $ push(iris )
144- Sys.sleep(0.1 )
136+ for (i in 1 : 5 ) cache $ push(iris )
145137 cache $ push(cars )
146138 expect_identical(cache $ n , 6L )
147139
140+
141+ for (p in cache $ files $ path ){ # loop necessary for compat with R < 3.6.0
142+ Sys.setFileTime(p , " 1999-01-01 00:00:00" )
143+ }
144+
148145 expect_true(cache $ size > 2048 )
149146 cache $ prune(max_size = " 2kb" )
150147 expect_true(cache $ size < = 2048 )
@@ -164,16 +161,7 @@ test_that("Inf max_* do not prunes", {
164161 # When using a real hash function as hashfun, identical objects will only
165162 # be added to the cache once
166163 cache <- Cache $ new(td , hashfun = function (x ) ascending_id())
167- cache $ push(iris )
168- Sys.sleep(0.1 )
169- cache $ push(iris )
170- Sys.sleep(0.1 )
171- cache $ push(iris )
172- Sys.sleep(0.1 )
173- cache $ push(iris )
174- Sys.sleep(0.1 )
175- cache $ push(iris )
176- Sys.sleep(0.1 )
164+ for (i in 1 : 5 ) cache $ push(iris )
177165 cache $ push(cars )
178166 expect_identical(cache $ n , 6L )
179167
@@ -231,16 +219,7 @@ test_that("pruning by age works", {
231219
232220 cache <- MockCache $ new(dir = td , hashfun = function (x ) ascending_id())
233221 on.exit(cache $ purge(), add = TRUE )
234- cache $ push(iris )
235- Sys.sleep(0.1 )
236- cache $ push(iris )
237- Sys.sleep(0.1 )
238- cache $ push(iris )
239- Sys.sleep(0.1 )
240- cache $ push(iris )
241- Sys.sleep(0.1 )
242- cache $ push(iris )
243- Sys.sleep(0.1 )
222+ for (i in 1 : 5 ) cache $ push(iris )
244223
245224 expect_identical(nrow(cache $ files ), 5L )
246225
0 commit comments