diff options
Diffstat (limited to 'qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging')
8 files changed, 1417 insertions, 0 deletions
diff --git a/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/address_spec.rb b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/address_spec.rb new file mode 100644 index 0000000000..05c97ddf30 --- /dev/null +++ b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/address_spec.rb @@ -0,0 +1,87 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'spec_helper' + +module Qpid + + module Messaging + + describe Address do + + before(:each) do + @address = Qpid::Messaging::Address.new "my-name/my-subject;{create:always}" + end + + it "stores the name, subject and options when created" do + name = @address.name + subject = @address.subject + create = @address.options["create"] + + name.should == "my-name" + subject.should == "my-subject" + create.should == "always" + end + + it "can update the name" do + @address.name = "new-name" + + name = @address.name + + name.should == "new-name" + end + + it "can update the subject" do + @address.subject = "new-subject" + + subject = @address.subject + + subject.should == "new-subject" + end + + it "can update the type" do + @address.address_type = "routed" + + type = @address.address_type + + type.should == "routed" + end + + it "can update the options" do + @address.options[:create] = :never + + create = @address.options["create"] + + create.should == "always" + end + + it "can return a string representation" do + address = Qpid::Messaging::Address.new "foo/bar:{create:always,link:durable}" + result = address.to_s + + result.should =~ /foo\/bar/ + result.should =~ /create:always/ + result.should =~ /link:durable/ + end + + end + + end + +end diff --git a/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/connection_spec.rb b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/connection_spec.rb new file mode 100644 index 0000000000..50038a0fc0 --- /dev/null +++ b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/connection_spec.rb @@ -0,0 +1,191 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'spec_helper' + +module Qpid + + module Messaging + + describe Connection do + + before(:each) do + @session_impl = double('Cqpid::Session') + @connection_impl = double('Cqpid::Connection') + + @connection = Qpid::Messaging::Connection.new :impl => @connection_impl + end + + it "accepts options on construction" do + expect { + connection = Qpid::Messaging::Connection.new :options => {:username => "foo"} + + connection.options.should include("username") + }.to_not raise_error + end + + it "returns the underlying implementation" do + impl = @connection.connection_impl + + impl.should == @connection_impl + end + + it "opens the connection" do + @connection_impl.should_receive(:open) + + @connection.open + end + + it "closes the connection" do + @connection_impl.should_receive(:close) + + @connection.close + end + + it "retrieves a session by name" do + @connection_impl.should_receive(:getSession). + with("farkle"). + and_return(@session_impl) + + session = @connection.session "farkle" + + session.session_impl.should == @session_impl + end + + it "raises an error when a session name is invalid" do + @connection_impl.should_receive(:getSession). + with("farkle"). + and_raise(RuntimeError) + + expect { + @connection.session "farkle" + }.to raise_error + end + + #################################################################### + # test conditions for when a connection is not connected to a broker + #################################################################### + describe "when closed" do + + before(:each) do + @connection_impl.should_receive(:isOpen). + and_return(false) + end + + it "returns false when not connected to a broker" do + open = @connection.open? + + open.should == false + end + + it "should raise an error when creating a session on a closed connection" do + expect { + @connection.create_session + }.to raise_error(RuntimeError) + end + + it "raises an error when creating a transactional session on a closed connection" do + expect { + @connection.create_session :transactional => true + }.to raise_error(RuntimeError) + end + + it "raises an error when creating a named session on a closed connection" do + expect { + @connection.create_session :name => "test", :transactional => true + }.to raise_error(RuntimeError) + end + + it "returns a null username when not connected" do + username = @connection.authenticated_username + + username.should be_nil + end + + end + + ######################################################### + # test conditions for when a connection must be connected + ######################################################### + describe "when connected" do + + before(:each) do + @connection_impl.should_receive(:isOpen). + and_return(true) + end + + it "returns true when connected to a broker" do + open = @connection.open? + + open.should == true + end + + it "creates a session" do + @connection_impl.should_receive(:createSession). + and_return(@session_impl) + + session = @connection.create_session + + session.session_impl.should == @session_impl + end + + it "creates a named session with a name when provided" do + @connection_impl.should_receive(:createSession).with("farkle"). + and_return(@session_impl) + + session = @connection.create_session :name => "farkle" + + session.session_impl.should == @session_impl + end + + it "creates a transactional session when specified" do + @connection_impl.should_receive(:createTransactionalSession). + and_return(@session_impl) + + session = @connection.create_session :transactional => true + + session.session_impl.should == @session_impl + end + + it "creates a named transactional session when specified" do + @connection_impl.should_receive(:createTransactionalSession). + with("farkle"). + and_return(@session_impl) + + session = @connection.create_session :transactional => true, :name => "farkle" + + session.session_impl.should == @session_impl + end + + it "returns the authenticated username when connected" do + @connection_impl.should_receive(:getAuthenticatedUsername). + and_return("mcpierce") + + username = @connection.authenticated_username + + username.should == "mcpierce" + end + + end + + end + + end + +end diff --git a/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/duration_spec.rb b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/duration_spec.rb new file mode 100644 index 0000000000..202332d232 --- /dev/null +++ b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/duration_spec.rb @@ -0,0 +1,83 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'spec_helper' + +module Qpid + + module Messaging + + describe Duration do + + before(:each) do + @duration = Qpid::Messaging::Duration::SECOND + end + + it "returns the underlying implementation" do + impl = @duration.duration_impl + + impl.should_not be_nil + end + + it "can create a duration with a millisecond value" do + duration = Qpid::Messaging::Duration.new 500 + + milliseconds = duration.milliseconds + + milliseconds.should == 500 + end + + it "returns the time in milliseconds" do + milliseconds = @duration.milliseconds + + milliseconds.should == 1000 + end + + it "raises an error when multiplied by a negative" do + expect { + twomin = Qpid::Messaging::Duration::MINUTE * -2 + }.to raise_error + end + + it "returns IMMEDIATE if the factor is zero" do + result = Qpid::Messaging::Duration::MINUTE * 0 + result.should be(Qpid::Messaging::Duration::IMMEDIATE) + end + + it "fractional factors return a reduced duration" do + factor = rand(1) + first = Qpid::Messaging::Duration::MINUTE + second = first * factor + + second.milliseconds.should == ((first.milliseconds * factor).floor) + end + + it "can return a multiple of its duration" do + factor = rand(10).floor + first = Qpid::Messaging::Duration.new(rand(10).floor * 10000) + second = first * factor + + second.milliseconds.should == first.milliseconds * factor + end + + end + + end + +end diff --git a/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/encoding_spec.rb b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/encoding_spec.rb new file mode 100644 index 0000000000..58b8447278 --- /dev/null +++ b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/encoding_spec.rb @@ -0,0 +1,63 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'spec_helper' + +module Qpid + + module Messaging + + describe "encoding" do + end + + describe "decoding" do + + before(:each) do + @message = Qpid::Messaging::Message.new + end + + it "can decode a message's text content" do + @message.content = "This is an unencoded message." + + content = Qpid::Messaging.decode @message + + content.should == "This is an unencoded message." + end + + it "can decode a message's list content" do + @message.content = ["this", "that"] + + content = Qpid::Messaging.decode @message + + content.should == ["this", "that"] + end + + it "can decode a message's map content" do + @message.content = {"this" => "that"} + + content = Qpid::Messaging.decode @message + + content.should == {"this" => "that"} + end + + end + + end + +end diff --git a/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/message_spec.rb b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/message_spec.rb new file mode 100644 index 0000000000..3581174548 --- /dev/null +++ b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/message_spec.rb @@ -0,0 +1,335 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'spec_helper' + +module Qpid + + module Messaging + + describe Message do + + before(:each) do + @message = Qpid::Messaging::Message.new :content => "My content" + end + + it "returns its implementation" do + impl = @message.message_impl + + impl.class.should == Cqpid::Message + end + + it "can set the reply to address" do + address = Qpid::Messaging::Address.new "my-queue;{create:always}" + + @message.reply_to = address + + reply_to = @message.reply_to + + reply_to.name.should == address.name + end + + it "can set the reply to from an address string" do + name = "my-queue" + subject = "responses" + address = "#{name}/#{subject}" + + @message.reply_to = address + + reply_to = @message.reply_to + + reply_to.name.should == name + reply_to.subject.should == subject + end + + it "should store the content when created" do + content = @message.content + + content.should == "My content" + end + + it "should store and retrieve string content properly" do + content = random_string(64) + + @message.content_object = content + @message.content_object.should == content + end + + it "should store and retrieve numeric content properly" do + content = rand(65536) + + @message.content_object = content + @message.content_object.should == content + end + + it "should store and retrieve map content properly" do + content = {} + (1..rand(128)).each { content["#{random_string(64)}"] = "#{random_string(64)}" } + + @message.content = content + @message.content.should eq content + end + + it "should store and retrieve list content properly" do + content = [] + (1..rand(128)).each { content << "#{random_string(64)}" } + + @message.content = content + @message.content.should eq content + end + + it "should properly encode a map when created" do + message = Qpid::Messaging::Message.new :content => {"foo" => "bar"} + + content = message.content + content_type = message.content_type + + content_type.should == "amqp/map" + content.class == Hash + content["foo"].should == "bar" + end + + it "should properly encode a list when created" do + message = Qpid::Messaging::Message.new :content => ["foo", "bar"] + + content = message.content + content_type = message.content_type + + content_type.should == "amqp/list" + content.class == Array + content.should include("foo") + content.should include("bar") + end + + it "should store the subject" do + @message.subject = "new-subject" + + subject = @message.subject + + subject.should == "new-subject" + end + + it "should update the content type" do + @message.content_type = "amqp/audio" + + content_type = @message.content_type + + content_type.should == "amqp/audio" + end + + it "should store the message id" do + @message.message_id = "foo" + + id = @message.message_id + + id.should == "foo" + end + + it "should store the user id" do + @message.user_id = "foo" + + id = @message.user_id + + id.should == "foo" + end + + it "should store the correlation id" do + @message.correlation_id = "message1" + + id = @message.correlation_id + + id.should == "message1" + end + + it "should store the priority" do + @message.priority = 7 + + priority = @message.priority + + priority.should == 7 + end + + it "should accept a Duration as the time to live" do + @message.ttl = Qpid::Messaging::Duration::SECOND + + ttl = @message.ttl + + ttl.milliseconds.should == Qpid::Messaging::Duration::SECOND.milliseconds + end + + it "should accept an integer value as the time to live" do + @message.ttl = 15000 + + ttl = @message.ttl + + ttl.milliseconds.should == 15000 + end + + it "should update the durable flag" do + @message.durable = true + + durable = @message.durable + + durable.should == true + end + + it "should update the redelivered flag" do + @message.redelivered = true + + redelivered = @message.redelivered + + redelivered.should == true + end + + it "should store a property" do + property = @message[:test_property] + + property.should == nil + + @message[:test_property] = "test_value1" + + property = @message[:test_property] + + property.should == "test_value1" + end + + it "should convert a symbol property value to a string" do + @message[:test_property] = :test_value2 + + property = @message[:test_property] + + property.should == "test_value2" + end + + it "should convert a symbol property name to a string" do + @message[:test_property] = "test_value3" + + property = @message["test_property"] + + property.should == "test_value3" + end + + it "should store text content" do + @message.content = "This is the content." + + content = @message.content + + content.should == "This is the content." + end + + it "should store list content" do + list = ["foo", "bar"] + + @message.content = list + + content = @message.content + content_type = @message.content_type + + content.should == list + content_type.should == "amqp/list" + end + + it "should convert symbol list elements to strings" do + @message.content = [:farkle] + + content = @message.content.first + + content.should == "farkle" + end + + it "should store map content" do + map = {"foo" => "bar"} + + @message.content = map + + content = @message.content + content_type = @message.content_type + + content.should == map + content_type.should == "amqp/map" + end + + it "should convert symbol map elements to strings" do + @message.content = {:first_name => :qpid} + + content = @message.content["first_name"] + + content.should == "qpid" + end + + describe "with content from the underlying implementation" do + + before(:each) do + @message_impl = double("Cqpid::Message") + @message = Qpid::Messaging::Message.new :impl => @message_impl + end + + it "should return simple text content" do + @message_impl.should_receive(:getContent). + and_return("my content") + @message_impl.should_receive(:getContentType). + and_return("") + + content = @message.content + + content.should == "my content" + end + + it "should decode a list" do + list = ["first", "second"] + + @message_impl.should_receive(:getContent). + and_return(list) + @message_impl.should_receive(:getContentType). + twice. + and_return("amqp/list") + Qpid::Messaging.stub!(:decode). + with(@message, "amqp/list"). + and_return(list) + + content = @message.content + + content.should == list + end + + it "should decode a map" do + map = {"first" => "second"} + + @message_impl.should_receive(:getContent). + and_return(map) + @message_impl.should_receive(:getContentType). + twice. + and_return("amqp/map") + Qpid::Messaging.stub!(:decode). + with(@message, "amqp/map"). + and_return(map) + + content = @message.content + + content.should == map + end + + + end + + end + + end + +end diff --git a/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/receiver_spec.rb b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/receiver_spec.rb new file mode 100644 index 0000000000..81ae935dcb --- /dev/null +++ b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/receiver_spec.rb @@ -0,0 +1,170 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'spec_helper' + +module Qpid + + module Messaging + + describe Receiver do + + before(:each) do + @message_impl = double("Cqpid::Message") + @session = double("Qpid::Messaging::Session") + @receiver_impl = double("Cqpid::Receiver") + + @receiver = Qpid::Messaging::Receiver.new @session, @receiver_impl + end + + it "returns the underlying implementation" do + impl = @receiver.receiver_impl + + impl.should == @receiver_impl + end + + it "gets a message with the default duration" do + @receiver_impl.should_receive(:get). + with(Qpid::Messaging::Duration::FOREVER.duration_impl). + and_return(@message_impl) + + message = @receiver.get + + message.message_impl.should == @message_impl + end + + it "gets a message with a specified duration" do + @receiver_impl.should_receive(:get). + with(Qpid::Messaging::Duration::SECOND.duration_impl). + and_return(@message_impl) + + message = @receiver.get Qpid::Messaging::Duration::SECOND + + message.message_impl.should == @message_impl + end + + it "returns nil when get receives no message" do + @receiver_impl.should_receive(:get). + with(Qpid::Messaging::Duration::MINUTE.duration_impl). + and_return(nil) + + message = @receiver.get Qpid::Messaging::Duration::MINUTE + + message.should be_nil + end + + it "fetches a message with the default duration" do + @receiver_impl.should_receive(:fetch). + with(Qpid::Messaging::Duration::FOREVER.duration_impl). + and_return(@message_impl) + + message = @receiver.fetch + + message.message_impl.should == @message_impl + end + + it "fetches a message with a specified duration" do + @receiver_impl.should_receive(:fetch). + with(Qpid::Messaging::Duration::SECOND.duration_impl). + and_return(@message_impl) + + message = @receiver.fetch Qpid::Messaging::Duration::SECOND + + message.message_impl.should == @message_impl + end + + it "returns nil when fetch recieves no message" do + @receiver_impl.should_receive(:fetch). + with(Qpid::Messaging::Duration::MINUTE.duration_impl). + and_return(nil) + + message = @receiver.fetch Qpid::Messaging::Duration::MINUTE + + message.should be_nil + end + + it "assigns capacity" do + @receiver_impl.should_receive(:setCapacity). + with(10) + + @receiver.capacity = 10 + end + + it "returns the capacity" do + @receiver_impl.should_receive(:getCapacity). + and_return(10) + + capacity = @receiver.capacity + + capacity.should == 10 + end + + it "reports the number of available messages" do + @receiver_impl.should_receive(:getAvailable). + and_return(20) + + available = @receiver.available + + available.should == 20 + end + + it "reports the number of unsettled messages" do + @receiver_impl.should_receive(:getUnsettled). + and_return(25) + + unsettled = @receiver.unsettled + + unsettled.should == 25 + end + + it "closes" do + @receiver_impl.should_receive(:close) + + @receiver.close + end + + it "reports its closed status" do + @receiver_impl.should_receive(:isClosed). + and_return(true) + + closed = @receiver.closed? + + closed.should == true + end + + it "returns its name" do + @receiver_impl.should_receive(:getName). + and_return("farkle") + + name = @receiver.name + + name.should == "farkle" + end + + it "returns its related session" do + session = @receiver.session + + session.should == @session + end + + end + + end + +end diff --git a/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/sender_spec.rb b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/sender_spec.rb new file mode 100644 index 0000000000..fa3a2a5b1f --- /dev/null +++ b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/sender_spec.rb @@ -0,0 +1,135 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'spec_helper' + +module Qpid + + module Messaging + + describe Sender do + + before(:each) do + @message = double("Qpid::Messaging::Message") + @message_impl = double("Cqpid::Message") + @sender_impl = double("Cqpid::Sender") + @session = double("Qpid::Messaging::Session") + + @sender = Qpid::Messaging::Sender.new @session, @sender_impl + end + + it "returns its implementation" do + impl = @sender.sender_impl + + impl.should == @sender_impl + end + + it "sends a message" do + @message.should_receive(:message_impl). + and_return(@message_impl) + @sender_impl.should_receive(:send). + with(@message_impl, false) + + @sender.send @message + end + + it "sends a message with optional synch" do + @message.should_receive(:message_impl). + and_return(@message_impl) + @sender_impl.should_receive(:send). + with(@message_impl, true) + + @sender.send @message, :sync => true + end + + it "sends a message with an optional block" do + block_called = false + + @message.should_receive(:message_impl). + and_return(@message_impl) + @sender_impl.should_receive(:send). + with(@message_impl, false) + + @sender.send @message do |message| + block_called = true if message == @message + end + + block_called.should be_true + end + + it "closes" do + @sender_impl.should_receive(:close) + + @sender.close + end + + it "returns its name" do + @sender_impl.should_receive(:getName). + and_return("farkle") + + name = @sender.name + + name.should == "farkle" + end + + it "sets its capacity" do + @sender_impl.should_receive(:setCapacity). + with(100) + + @sender.capacity = 100 + end + + it "returns its capacity" do + @sender_impl.should_receive(:getCapacity). + and_return(25) + + capacity = @sender.capacity + + capacity.should == 25 + end + + it "returns the number of unsettled messages" do + @sender_impl.should_receive(:getUnsettled). + and_return(15) + + unsettled = @sender.unsettled + + unsettled.should == 15 + end + + it "returns the number of available message slots" do + @sender_impl.should_receive(:getAvailable). + and_return(50) + + available = @sender.available + + available.should == 50 + end + + it "returns a reference to its session" do + session = @sender.session + + session.should == @session + end + + end + + end + +end diff --git a/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/session_spec.rb b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/session_spec.rb new file mode 100644 index 0000000000..cb1e94a72f --- /dev/null +++ b/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/session_spec.rb @@ -0,0 +1,353 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'spec_helper' + +module Qpid + + module Messaging + + describe Session do + + before(:each) do + @connection = double("Qpid::Messaging::Connection") + @session_impl = double("Cqpid::Session") + @session = Qpid::Messaging::Session.new @connection, @session_impl + @sender_impl = double("Cqpid::Sender") + @receiver_impl = double("Cqpid::Receiver") + end + + it "returns its implementation" do + impl = @session.session_impl + + impl.should == @session_impl + end + + it "returns its connection" do + connection = @session.connection + + connection.should == @connection + end + + it "creates a Sender from an Address" do + address = Qpid::Messaging::Address.new "my-queue;{create:always}" + + @session_impl.should_receive(:createSender). + with(address.address_impl). + and_return(@sender_impl) + @sender_impl.should_receive(:getName). + and_return("my-queue") + + sender = @session.create_sender address + + sender.sender_impl.should == @sender_impl + end + + it "creates a Sender from an address string" do + address = "my-queue;{create:true}" + + @session_impl.should_receive(:createSender). + with(address). + and_return(@sender_impl) + @sender_impl.should_receive(:getName). + and_return("my-queue") + + sender = @session.create_sender address + + sender.sender_impl.should == @sender_impl + end + + ####################################### + # scenarios involing an existing Sender + ####################################### + describe "when retrieving a Sender by name" do + + before(:each) do + address = "my-queue;{create:always}" + @name = "my-queue" + + @session_impl.should_receive(:createSender). + with(address). + and_return(@sender_impl) + @sender_impl.should_receive(:getName). + and_return(@name) + + @sender = @session.create_sender address + end + + it "works when the name is valid" do + sender = @session.sender @name + + sender.should == @sender + end + + it "raises an error when the name is invalid" do + expect { + @session.sender @name.reverse + }.to raise_error + end + + end + + it "creates a Receiver from an Address" do + address = Qpid::Messaging::Address.new "my-queue", "" + + @session_impl.should_receive(:createReceiver). + with(address.address_impl). + and_return(@receiver_impl) + @receiver_impl.should_receive(:getName). + and_return("my-queue") + + receiver = @session.create_receiver address + + receiver.receiver_impl.should == @receiver_impl + end + + it "creates a Receiver from an address string" do + address = "my-queue" + + @session_impl.should_receive(:createReceiver). + with(address). + and_return(@receiver_impl) + @receiver_impl.should_receive(:getName). + and_return("my-queue") + + receiver = @session.create_receiver address + + receiver.receiver_impl.should == @receiver_impl + end + + ######################################### + # scenarios involving an existing Receiver + ########################################## + describe "when retrieving a Receiver by name" do + + before(:each) do + address = "my-queue" + @name = "my-queue" + + @session_impl.should_receive(:createReceiver). + with(address). + and_return(@receiver_impl) + @receiver_impl.should_receive(:getName). + and_return(@name) + + @receiver = @session.create_receiver address + end + + it "works with a valid name" do + receiver = @session.receiver @name + + receiver.should == @receiver + end + + it "raises an error when the name is invalid" do + expect { + @session.receiver @name.reverse + }.to raise_error + end + + end + + it "closes the session" do + @session_impl.should_receive(:close) + + @session.close + end + + it "commits a pending transaction" do + @session_impl.should_receive(:commit) + + @session.commit + end + + it "rolls back an uncommitted transaction" do + @session_impl.should_receive(:rollback) + + @session.rollback + end + + it "acknowledges all received messages" do + @session_impl.should_receive(:acknowledge). + with(false) + + @session.acknowledge + end + + it "acknowledges all messages synchronously" do + @session_impl.should_receive(:acknowledge). + with(true) + + @session.acknowledge :sync => true + end + + it "acknowledges all messages asynchronously" do + @session_impl.should_receive(:acknowledge). + with(false) + + @session.acknowledge :sync => false + end + + ###################################### + # Scenarios involving a single message + ###################################### + describe "with a single message" do + + before(:each) do + @message = Qpid::Messaging::Message.new :content => "Testing" + end + + it "can acknowledge asynchronously by default" do + @session_impl.should_receive(:acknowledge). + with(@message.message_impl, false) + + @session.acknowledge :message => @message + end + + it "can acknowledge synchronously" do + @session_impl.should_receive(:acknowledge). + with(@message.message_impl, true) + + @session.acknowledge :message => @message, :sync => true + end + + it "can acknowledge asynchronously" do + @session_impl.should_receive(:acknowledge). + with(@message.message_impl, false) + + @session.acknowledge :message => @message, :sync => false + end + + it "can reject it" do + @session_impl.should_receive(:reject). + with(@message.message_impl) + + @session.reject @message + end + + it "can release it" do + @session_impl.should_receive(:release). + with(@message.message_impl) + + @session.release @message + end + + end + + it "does not block by default when synchronizating with the broker" do + @session_impl.should_receive(:sync). + with(false) + + @session.sync + end + + it "can block while synchronizing with the broker" do + @session_impl.should_receive(:sync). + with(true) + + @session.sync :block => true + end + + it "can not block while synchronizing with the broker" do + @session_impl.should_receive(:sync). + with(false) + + @session.sync :block => false + end + + it "returns the number of messages that are receivable" do + @session_impl.should_receive(:getReceivable). + and_return(15) + + receivable = @session.receivable + + receivable.should == 15 + end + + it "returns the number of unsettled messages" do + @session_impl.should_receive(:getUnsettledAcks). + and_return(25) + + unsettled = @session.unsettled_acks + + unsettled.should == 25 + end + + it "waits forever by default for the next Receiver with messages" do + @session_impl.should_receive(:nextReceiver). + with(Qpid::Messaging::Duration::FOREVER.duration_impl). + and_return(@receiver_impl) + + receiver = @session.next_receiver + + receiver.receiver_impl.should == @receiver_impl + end + + it "uses the specified time when waiting for the next Receiver with messages" do + @session_impl.should_receive(:nextReceiver). + with(Qpid::Messaging::Duration::SECOND.duration_impl). + and_return(@receiver_impl) + + receiver = @session.next_receiver Qpid::Messaging::Duration::SECOND + + receiver.receiver_impl.should == @receiver_impl + end + + it "returns nil when no Receiver has messages within the timeout period" do + @session_impl.should_receive(:nextReceiver). + with(Qpid::Messaging::Duration::MINUTE.duration_impl). + and_return(nil) + + receiver = @session.next_receiver Qpid::Messaging::Duration::MINUTE + + receiver.should be_nil + end + + it "returns true when there are errors on the session" do + @session_impl.should_receive(:hasError). + and_return(true) + + errors = @session.errors? + + errors.should be_true + end + + it "returns false when there are no errors on the session" do + @session_impl.should_receive(:hasError). + and_return(false) + + errors = @session.errors? + + errors.should be_false + end + + it "causes exceptions to be raised when there are errors" do + @session_impl.should_receive(:checkError). + and_raise(RuntimeError) + + expect { + @session.errors + }.to raise_error(RuntimeError) + end + + end + + end + +end |