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

- old
+ new

@@ -6,10 +6,11 @@ describe MQTT::Client do before(:each) do @client = MQTT::Client.new @socket = StringIO.new + @socket.set_encoding("binary") if @socket.respond_to?(:set_encoding) end describe "initializing a client" do it "with no arguments, it should use the defaults" do @client = MQTT::Client.new @@ -62,48 +63,48 @@ end end describe "when calling the 'connect' method" do before(:each) do - TCPSocket.stubs(:new).returns(@socket) - Thread.stubs(:new) - @client.stubs(:receive_connack) + TCPSocket.stub(:new).and_return(@socket) + Thread.stub(:new) + @client.stub(:receive_connack) end it "should create a TCP Socket if not connected" do - TCPSocket.expects(:new).once.returns(@socket) + TCPSocket.should_receive(:new).once.and_return(@socket) @client.connect('myclient') end it "should not create a new TCP Socket if connected" do - @client.stubs(:connected?).returns(true) - TCPSocket.expects(:new).never + @client.stub(:connected?).and_return(true) + TCPSocket.should_receive(:new).never @client.connect('myclient') end it "should start the reader thread if not connected" do - Thread.expects(:new).once + Thread.should_receive(:new).once @client.connect('myclient') end it "should write a valid CONNECT packet to the socket if not connected" do @client.connect('myclient') @socket.string.should == "\020\026\x00\x06MQIsdp\x03\x02\x00\x0f\x00\x08myclient" end it "should try and read an acknowledgement packet to the socket if not connected" do - @client.expects(:receive_connack).once + @client.should_receive(:receive_connack).once @client.connect('myclient') end it "should disconnect after connecting, if a block is given" do - @client.expects(:disconnect).once + @client.should_receive(:disconnect).once @client.connect('myclient') { nil } end it "should not disconnect after connecting, if no block is given" do - @client.expects(:disconnect).never + @client.should_receive(:disconnect).never @client.connect('myclient') end it "should include the username and password for an authenticated connection" do @client.username = 'username' @@ -160,11 +161,11 @@ end describe "when calling the 'receive_connack' method" do before(:each) do @client.instance_variable_set(:@socket, @socket) - IO.stubs(:select).returns([[@socket], [], []]) + IO.stub(:select).and_return([[@socket], [], []]) end it "should not throw an exception for a successful CONNACK packet" do @socket.write("\x20\x02\x00\x00") @socket.rewind @@ -202,34 +203,35 @@ end end describe "when calling the 'disconnect' method" do before(:each) do + thread = double('Read Thread', :alive? => true, :kill => true) @client.instance_variable_set(:@socket, @socket) - @client.instance_variable_set(:@read_thread, stub_everything('Read Thread')) + @client.instance_variable_set(:@read_thread, thread) end it "should not do anything if the socket is already disconnected" do - @client.stubs(:connected?).returns(false) + @client.stub(:connected?).and_return(false) @client.disconnect(true) @socket.string.should == "" end it "should write a valid DISCONNECT packet to the socket if connected and the send_msg=true an" do - @client.stubs(:connected?).returns(true) + @client.stub(:connected?).and_return(true) @client.disconnect(true) @socket.string.should == "\xE0\x00" end it "should not write anything to the socket if the send_msg=false" do - @client.stubs(:connected?).returns(true) + @client.stub(:connected?).and_return(true) @client.disconnect(false) @socket.string.should be_empty end it "should call the close method on the socket" do - @socket.expects(:close) + @socket.should_receive(:close) @client.disconnect end end describe "when calling the 'ping' method" do @@ -298,21 +300,45 @@ 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 + it "should return 1 if there is one incoming message waiting" do + inject_packet(:topic => 'topic0', :payload => 'payload0', :qos => 0) + @client.queue_length.should == 1 + end + + it "should return 2 if there are two incoming message waiting" do + inject_packet(:topic => 'topic0', :payload => 'payload0', :qos => 0) + 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 + + it "should return return false if there is an incoming messages waiting" do + inject_packet(:topic => 'topic0', :payload => 'payload0', :qos => 0) + @client.queue_empty?.should be_false + end + end + describe "when calling the 'get' method" do before(:each) do @client.instance_variable_set(:@socket, @socket) end - def inject_packet(opts={}) - packet = MQTT::Packet::Publish.new(opts) - @client.instance_variable_get('@read_queue').push(packet) - end - it "should successfull receive a valid PUBLISH packet with a QoS 0" do inject_packet(:topic => 'topic0', :payload => 'payload0', :qos => 0) topic,payload = @client.get topic.should == 'topic0' payload.should == 'payload0' @@ -321,10 +347,11 @@ it "should successfull receive a valid PUBLISH packet with a QoS 1" do 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 end describe "when calling the 'unsubscribe' method" do before(:each) do @@ -343,13 +370,14 @@ end describe "when calling the 'receive_packet' method" do before(:each) do @client.instance_variable_set(:@socket, @socket) - IO.stubs(:select).returns([[@socket], [], []]) + IO.stub(:select).and_return([[@socket], [], []]) @read_queue = @client.instance_variable_get(:@read_queue) - @parent_thread = Thread.current[:parent] = stub_everything('Parent Thread') + @parent_thread = Thread.current[:parent] = double('Parent Thread') + @parent_thread.stub(:raise) end it "should put PUBLISH messages on to the read queue" do @socket.write("\x30\x0e\x00\x05topicpayload") @socket.rewind @@ -363,25 +391,25 @@ @client.send(:receive_packet) @read_queue.size.should == 0 end it "should send a ping packet if one is due" do - IO.expects(:select).returns(nil) + IO.should_receive(:select).and_return(nil) @client.instance_variable_set(:@last_pingreq, Time.at(0)) - @client.expects(:ping).once + @client.should_receive(:ping).once @client.send(:receive_packet) end it "should close the socket if there is an exception" do - @socket.expects(:close).once - MQTT::Packet.stubs(:read).raises(MQTT::Exception) + @socket.should_receive(:close).once + MQTT::Packet.stub(:read).and_raise(MQTT::Exception) @client.send(:receive_packet) end it "should pass exceptions up to parent thread" do - @parent_thread.expects(:raise).once - MQTT::Packet.stubs(:read).raises(MQTT::Exception) + @parent_thread.should_receive(:raise).once + MQTT::Packet.stub(:read).and_raise(MQTT::Exception) @client.send(:receive_packet) end end describe "generating a client identifier" do @@ -418,8 +446,15 @@ it "should end in 16 characters of lowercase letters and numbers" do @client_id.should match(/_[a-z0-9]{16}$/) end end + end + + private + + def inject_packet(opts={}) + packet = MQTT::Packet::Publish.new(opts) + @client.instance_variable_get('@read_queue').push(packet) end end