summaryrefslogtreecommitdiff
path: root/spec/unit/queue_spec.rb
blob: 9a199c9852589706d2d2b82ed059e077b04af3aa (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: GNU General Public License version 2 or later
# 
# This program and entire repository is free software; you can
# redistribute it and/or modify it under the terms of the GNU 
# General Public License as published by the Free Software 
# Foundation; either version 2 of the License, or any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))

describe Chef::Queue do
 
  it "should connect to a stomp server on localhost and 61613" do
    Stomp::Connection.should_receive(:open).with("", "", "localhost", 61613, false).once
    Chef::Queue.connect
  end
 
  it "should allow config options to override defaults on connect" do
    Chef::Config[:queue_user] = "monkey"
    Chef::Config[:queue_password] = "password"
    Chef::Config[:queue_host] = "10.10.10.10"
    Chef::Config[:queue_port] = 61614
    Stomp::Connection.should_receive(:open).with("monkey", "password", "10.10.10.10", 61614, false).once
    Chef::Queue.connect
  end
  
  it "should make a url based on type and name" do
    Chef::Queue.make_url("topic", "goal").should eql("/topic/chef/goal")
    Chef::Queue.make_url("queue", "pool").should eql("/queue/chef/pool")
  end
  
  it "should allow you to subscribe to a queue" do
    queue = mock("Queue", :null_object => true)
    queue.should_receive(:subscribe).with(Chef::Queue.make_url(:topic, :node)).once
    Stomp::Connection.stub!(:open).and_return(queue)
    Chef::Queue.connect
    Chef::Queue.subscribe(:topic, :node)
  end
  
  it "should allow you to send a message" do
    message = mock("Message", :null_object => true)
    message.should_receive(:to_json).once.and_return("some json")
    connection = mock("Connection", :null_object => true)
    connection.should_receive(:send).with(Chef::Queue.make_url(:queue, :node), "some json").once.and_return(true)
    Stomp::Connection.stub!(:open).and_return(connection)
    Chef::Queue.connect
    Chef::Queue.send_msg(:queue, :node, message)
  end
  
  it "should receive a message with receive_msg" do
    raw_msg = mock("Stomp Message", :null_object => true)
    raw_msg.should_receive(:body).twice.and_return("the body")
    connection = mock("Connection", :null_object => true)
    connection.should_receive(:receive).once.and_return(raw_msg)
    JSON.should_receive(:parse).with("the body").and_return("the body")
    Stomp::Connection.stub!(:open).and_return(connection)
    Chef::Queue.connect
    Chef::Queue.receive_msg.should eql([ "the body", raw_msg ])
  end
  
  it "should poll for a message with poll_msg, returning a message if there is one" do
    raw_msg = mock("Stomp Message", :null_object => true)
    raw_msg.should_receive(:body).once.and_return("the body")
    connection = mock("Connection", :null_object => true)
    connection.should_receive(:poll).once.and_return(raw_msg)
    JSON.should_receive(:parse).with("the body").and_return("the body")
    Stomp::Connection.stub!(:open).and_return(connection)
    Chef::Queue.connect
    Chef::Queue.poll_msg.should eql("the body")
  end
  
  it "should poll for a message with poll_msg, returning nil if there is not a message" do
    connection = mock("Connection", :null_object => true)
    connection.should_receive(:poll).once.and_return(nil)
    JSON.should_not_receive(:parse).with(nil)
    Stomp::Connection.stub!(:open).and_return(connection)
    Chef::Queue.connect
    Chef::Queue.poll_msg.should eql(nil)
  end
  
  it "should raise an exception if you disconnect without a connection" do
    Stomp::Connection.stub!(:open).and_return(nil)
    Chef::Queue.connect 
    lambda { Chef::Queue.disconnect }.should raise_error(ArgumentError)
  end
  
  it "should disconnect an active connection" do
    connection = mock("Connection", :null_object => true)
    connection.should_receive(:disconnect).once.and_return(true)
    Stomp::Connection.stub!(:open).and_return(connection)
    Chef::Queue.connect
    Chef::Queue.disconnect
  end

end