File: tc_cache.rb

package info (click to toggle)
samizdat 0.6.1-3lenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,208 kB
  • ctags: 962
  • sloc: ruby: 6,685; xml: 636; sql: 478; sh: 131; makefile: 62
file content (67 lines) | stat: -rw-r--r-- 1,351 bytes parent folder | download
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
#!/usr/bin/env ruby
#
# Samizdat Cache tests
#
#   Copyright (c) 2002-2005  Dmitry Borodaenko <angdraug@debian.org>
#
#   This program is free software.
#   You can distribute/modify this program under the terms of
#   the GNU General Public License version 2 or later.
#
# vim: et sw=2 sts=2 ts=8 tw=0

require 'test/unit'
require 'samizdat/engine'

include Samizdat

class TC_Cache < Test::Unit::TestCase

  def test_initialize
    cache = Cache.new(3, 5)
  end

  def test_flush
    cache = Cache.new(3, 5)
    cache['t'] = 'test'
    cache.flush
    assert_equal nil, cache['t']
  end

  def test_add_fetch
    cache = Cache.new(3, 5)
    cache['t'] = 'test'
    assert_equal 'test', cache['t']
  end

  def test_fetch_or_add
    cache = Cache.new(3, 5)
    assert_equal nil, cache['t']
    cache.fetch_or_add('t') { 'test' }
    assert_equal 'test', cache['t']
  end

  def test_truncate
    cache = Cache.new(3, 5)
    1.upto(5) {|i| cache[i] = i }
    1.upto(5) do |i|
      assert_equal i, cache[i]
    end
    6.upto(10) {|i| cache[i] = i }
    1.upto(5) do |i|
      assert_equal nil, cache[i]
    end
  end

  def test_timeout
    cache = Cache.new(0.01, 5)
    1.upto(5) {|i| cache[i] = i }
    1.upto(5) do |i|
      assert_equal i, cache[i]
    end
    sleep(0.02)
    1.upto(5) do |i|
      assert_equal nil, cache[i]
    end
  end
end