Skip to content

Commit 5774c0f

Browse files
committed
test(kv): assert upper bound of remaining ttl (#258)
The shared expiration tests only checked a lower bound for `ttl!/1`, so an adapter returning the TTL in the wrong unit still passed. That's how the Redis adapter ended up returning microseconds instead of milliseconds (elixir-nebulex/nebulex_redis_adapter#75). The tests now also check that the remaining TTL is not larger than the one that was set.
1 parent fcc0b82 commit 5774c0f

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

‎test/shared/cache/kv_expiration_test.exs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ defmodule Nebulex.Cache.KVExpirationTest do
109109
str = rand_str()
110110

111111
assert cache.put!(str, str, ttl: :timer.seconds(1)) == :ok
112-
assert cache.ttl!(str) |> is_integer()
112+
assert cache.ttl!(str) in 1..:timer.seconds(1)
113113

114114
assert cache.put!(str, str, ttl: :timer.minutes(1), keep_ttl: false) == :ok
115-
assert cache.ttl!(str) > :timer.seconds(1)
115+
assert cache.ttl!(str) in (:timer.seconds(1) + 1)..:timer.minutes(1)
116116

117117
assert cache.put!(str, str) == :ok
118118
assert cache.ttl!(str) == :infinity
@@ -139,7 +139,7 @@ defmodule Nebulex.Cache.KVExpirationTest do
139139
str = rand_str()
140140

141141
assert cache.put!(str, str, ttl: :timer.seconds(1)) == :ok
142-
assert cache.ttl!(str) |> is_integer()
142+
assert cache.ttl!(str) in 1..:timer.seconds(1)
143143

144144
assert cache.replace!(str, str, keep_ttl: false) == true
145145
assert cache.ttl!(str) == :infinity
@@ -169,12 +169,12 @@ defmodule Nebulex.Cache.KVExpirationTest do
169169
ttl = :timer.seconds(1)
170170

171171
assert cache.put!(:ttl!, 1, ttl: ttl) == :ok
172-
assert cache.ttl!(:ttl!) > 0
172+
assert cache.ttl!(:ttl!) in 1..ttl
173173
assert cache.put!(:infinity_ttl, 2) == :ok
174174

175175
t = t_sleep(10)
176176

177-
assert cache.ttl!(:ttl!) > 0
177+
assert cache.ttl!(:ttl!) in 1..(ttl - t)
178178
assert cache.ttl!(:infinity_ttl) == :infinity
179179

180180
_ = t_sleep(ttl + t + 10)
@@ -198,10 +198,10 @@ defmodule Nebulex.Cache.KVExpirationTest do
198198
describe "expire!/2" do
199199
test "alters the expiration time for the given key", %{cache: cache} do
200200
assert cache.put!(:expire!, 1, ttl: :timer.seconds(1)) == :ok
201-
assert cache.ttl!(:expire!) > 0
201+
assert cache.ttl!(:expire!) in 1..:timer.seconds(1)
202202

203203
assert cache.expire!(:expire!, :timer.seconds(10)) == true
204-
assert cache.ttl!(:expire!) > :timer.seconds(1)
204+
assert cache.ttl!(:expire!) in (:timer.seconds(1) + 1)..:timer.seconds(10)
205205

206206
assert cache.expire!(:expire!, :infinity) == true
207207
assert cache.ttl!(:expire!) == :infinity

0 commit comments

Comments
 (0)