spec/mqtt_client_spec.rb in mqtt-0.0.9 vs spec/mqtt_client_spec.rb in mqtt-0.1.0

- old
+ new

@@ -61,11 +61,11 @@ 'Unsupported number of arguments' ) end end - describe "when calling the 'connect' method" do + describe "when calling the 'connect' method on a client" do before(:each) do TCPSocket.stub(:new).and_return(@socket) Thread.stub(:new) @client.stub(:receive_connack) end @@ -158,10 +158,34 @@ end end end + describe "calling 'connect' on the class" do + before(:each) do + TCPSocket.stub(:new).and_return(@socket) + MQTT::Client.stub(:new).and_return(@client) + Thread.stub(:new) + @client.stub(:receive_connack) + end + + it "should create a new client object" do + MQTT::Client.should_receive(:new).once + MQTT::Client.connect + end + + it "should call connect new client object" do + @client.should_receive(:connect).once + MQTT::Client.connect + end + + it "should return the new client object" do + client = MQTT::Client.connect + client.class.should == MQTT::Client + end + end + describe "when calling the 'receive_connack' method" do before(:each) do @client.instance_variable_set(:@socket, @socket) IO.stub(:select).and_return([[@socket], [], []]) end @@ -300,11 +324,11 @@ it "should write a valid SUBSCRIBE packet to the socket if given a two topic Strings with QoS in a Hash" do @client.subscribe('a/b' => 0,'c/d' => 1) @socket.string.should == "\x82\x0e\x00\x01\x00\x03a/b\x00\x00\x03c/d\x01" end end - + describe "when calling the 'queue_length' method" do it "should return 0 if there are no incoming messages waiting" do @client.queue_length.should == 0 end @@ -318,11 +342,10 @@ inject_packet(:topic => 'topic0', :payload => 'payload1', :qos => 0) @client.queue_length.should == 2 end end - describe "when calling the 'queue_emtpy?' method" do it "should return return true if there no incoming messages waiting" do @client.queue_empty?.should be_true end @@ -348,9 +371,62 @@ inject_packet(:topic => 'topic1', :payload => 'payload1', :qos => 1) topic,payload = @client.get topic.should == 'topic1' payload.should == 'payload1' @client.queue_empty?.should be_true + end + + context "with a block" do + it "should successfull receive a more than 1 message" do + inject_packet(:topic => 'topic0', :payload => 'payload0') + inject_packet(:topic => 'topic1', :payload => 'payload1') + payloads = [] + @client.get do |topic,payload| + payloads << payload + break if payloads.size > 1 + end + payloads.size.should == 2 + payloads.should == ['payload0', 'payload1'] + end + end + end + + describe "when calling the 'get_packet' method" do + before(:each) do + @client.instance_variable_set(:@socket, @socket) + end + + it "should successfull receive a valid PUBLISH packet with a QoS 0" do + inject_packet(:topic => 'topic0', :payload => 'payload0', :qos => 0) + packet = @client.get_packet + packet.class.should == MQTT::Packet::Publish + packet.qos.should == 0 + packet.topic.should == 'topic0' + packet.payload.should == 'payload0' + end + + it "should successfull receive a valid PUBLISH packet with a QoS 1" do + inject_packet(:topic => 'topic1', :payload => 'payload1', :qos => 1) + packet = @client.get_packet + packet.class.should == MQTT::Packet::Publish + packet.qos.should == 1 + packet.topic.should == 'topic1' + packet.payload.should == 'payload1' + @client.queue_empty?.should be_true + end + + context "with a block" do + it "should successfull receive a more than 1 packet" do + inject_packet(:topic => 'topic0', :payload => 'payload0') + inject_packet(:topic => 'topic1', :payload => 'payload1') + packets = [] + @client.get_packet do |packet| + packets << packet + break if packets.size > 1 + end + packets.size.should == 2 + packets.map{|p| p.payload}.should == ['payload0', 'payload1'] + end end end describe "when calling the 'unsubscribe' method" do before(:each) do