3
require File.dirname(__FILE__) + '/../../spec_helper'
4
require 'puppet/indirector/queue'
6
class Puppet::Indirector::Queue::TestClient
12
def self.pson_create(pson)
13
new(pson['data'].to_sym)
16
def initialize(name = nil)
20
def render(format = :pson)
25
{:type => self.class.to_s, :data => name}.to_pson(*args)
29
describe Puppet::Indirector::Queue do
30
confine "PSON library is missing; cannot test queueing" => Puppet.features.pson?
34
@indirection = stub 'indirection', :name => :my_queue, :register_terminus_type => nil, :model => @model
35
Puppet::Indirector::Indirection.stubs(:instance).with(:my_queue).returns(@indirection)
36
@store_class = Class.new(Puppet::Indirector::Queue) do
41
@store = @store_class.new
43
@subject_class = FooExampleData
44
@subject = @subject_class.new
47
Puppet.settings.stubs(:value).returns("bogus setting data")
48
Puppet.settings.stubs(:value).with(:queue_type).returns(:test_client)
49
Puppet::Util::Queue.stubs(:queue_type_to_class).with(:test_client).returns(Puppet::Indirector::Queue::TestClient)
51
@request = stub 'request', :key => :me, :instance => @subject
54
it "should require PSON" do
55
Puppet.features.expects(:pson?).returns false
57
lambda { @store_class.new }.should raise_error(ArgumentError)
60
it 'should use the correct client type and queue' do
61
@store.queue.should == :my_queue
62
@store.client.should be_an_instance_of(Puppet::Indirector::Queue::TestClient)
65
describe "when saving" do
66
it 'should render the instance using pson' do
67
@subject.expects(:render).with(:pson)
68
@store.client.stubs(:send_message)
72
it "should send the rendered message to the appropriate queue on the client" do
73
@subject.expects(:render).returns "mypson"
75
@store.client.expects(:send_message).with(:my_queue, "mypson")
80
it "should catch any exceptions raised" do
81
@store.client.expects(:send_message).raises ArgumentError
83
lambda { @store.save(@request) }.should raise_error(Puppet::Error)
87
describe "when subscribing to the queue" do
89
@store_class.stubs(:model).returns @model
92
it "should use the model's Format support to intern the message from pson" do
93
@model.expects(:convert_from).with(:pson, "mymessage")
95
@store_class.client.expects(:subscribe).yields("mymessage")
96
@store_class.subscribe {|o| o }
99
it "should yield each interned received message" do
100
@model.stubs(:convert_from).returns "something"
102
@subject_two = @subject_class.new
103
@subject_two.name = :too
105
@store_class.client.expects(:subscribe).with(:my_queue).multiple_yields(@subject, @subject_two)
108
@store_class.subscribe do |obj|
112
received.should == %w{something something}
115
it "should log but not propagate errors" do
116
@store_class.client.expects(:subscribe).yields("foo")
117
@store_class.expects(:intern).raises ArgumentError
119
@store_class.subscribe {|o| o }