File: cache_test.rb

package info (click to toggle)
ruby-lru-redux 1.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: ruby: 615; makefile: 4
file content (91 lines) | stat: -rw-r--r-- 1,396 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'timecop'

class TTLCacheTest < CacheTest
  def setup
    Timecop.freeze(Time.now)
    @c = LruRedux::TTL::Cache.new 3, 5 * 60
  end

  def teardown
    Timecop.return
    assert_equal true, @c.send(:valid?)
  end

  def test_ttl
    assert_equal 300, @c.ttl

    @c.ttl = 10 * 60

    assert_equal 600, @c.ttl
  end

  # TTL tests using Timecop
  def test_ttl_eviction_on_access
    @c[:a] = 1
    @c[:b] = 2

    Timecop.freeze(Time.now + 330)

    @c[:c] = 3

    assert_equal([[:c, 3]], @c.to_a)
  end

  def test_ttl_eviction_on_expire
    @c[:a] = 1
    @c[:b] = 2

    Timecop.freeze(Time.now + 330)

    @c.expire

    assert_equal([], @c.to_a)
  end

  def test_ttl_eviction_on_new_max_size
    @c[:a] = 1
    @c[:b] = 2

    Timecop.freeze(Time.now + 330)

    @c.max_size = 10

    assert_equal([], @c.to_a)
  end

  def test_ttl_eviction_on_new_ttl
    @c[:a] = 1
    @c[:b] = 2

    Timecop.freeze(Time.now + 330)

    @c.ttl = 10 * 60

    assert_equal([[:b, 2], [:a, 1]], @c.to_a)

    @c.ttl = 2 * 60

    assert_equal([], @c.to_a)
  end

  def test_ttl_precedence_over_lru
    @c[:a] = 1

    Timecop.freeze(Time.now + 60)

    @c[:b] = 2
    @c[:c] = 3

    @c[:a]

    assert_equal [[:a, 1], [:c, 3], [:b, 2]],
                 @c.to_a

    Timecop.freeze(Time.now + 270)

    @c[:d] = 4

    assert_equal [[:d, 4], [:c, 3], [:b, 2]],
                 @c.to_a
  end
end