File: cache_spec.rb

package info (click to toggle)
ruby-rspec-puppet 2.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,416 kB
  • sloc: ruby: 6,661; makefile: 6
file content (64 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'
require 'rspec-puppet/cache'

describe RSpec::Puppet::Cache do

  let(:compiler) { Hash.new }

  subject do
    described_class.new do |args|
      compiler[args]
    end
  end

  describe "fetching cached entries" do
    it "calls the get_proc on cache misses" do
      compiler["example.com"] = "New catalog!"
      fetched_obj = subject.get("example.com")
      expect(fetched_obj).to eq("New catalog!")
    end

    it "can supply a proc to the get method" do
      compiler["example.com"] = "New catalog!"
      fetched_obj = subject.get("example.com") do |args|
        compiler[args] + "!!"
      end
      expect(fetched_obj).to eq("New catalog!!!")
    end

    it "can handle procs with multiple args" do
      compiler["example.com"] = "New catalog!"
      fetched_obj = subject.get("example.com", " Yay!") do |arg1, arg2|
        compiler[arg1] + arg2
      end
      expect(fetched_obj).to eq("New catalog! Yay!")
    end

    it "reuses cached entries" do
      compiler["example.com"] = "Cachable catalog!"

      first = subject.get("example.com")
      second = subject.get("example.com")

      expect(first.object_id).to eq(second.object_id)
    end

    it "evicts expired entries" do
      compiler["evicting.example.com"] = "Catalog to evict"
      0.upto(15) do |i|
        compiler["node#{i}.example.com"] = "Catalog for node #{i}"
      end

      first = subject.get("evicting.example.com")
      0.upto(15) do |i|
        subject.get("node#{i}.example.com")
      end
      compiler["evicting.example.com"] = "Replacement catalog"
      second = subject.get("evicting.example.com")

      expect(first).to eq("Catalog to evict")
      expect(second).to eq("Replacement catalog")

    end
  end
end