Sha256: 39288f4eb01e3c0556ce192204ee45ffc3f2d8fe54cacb324fa9302646213034

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

# encoding: utf-8

require File.expand_path("../../helper.rb", __FILE__)
require 'socket'
require 'timeout'

class TestStreamSocket < ZmqTestCase
  def test_behavior
    ctx = ZMQ::Context.new
    sock = ctx.socket(:STREAM)
    assert_equal ZMQ::STREAM, sock.type
    assert_equal "STREAM socket", sock.to_s
  ensure
    ctx.destroy
  end

  def test_recv_and_send
    ctx = ZMQ::Context.new
    sock = ctx.socket(:STREAM)
    port = sock.bind("tcp://127.0.0.1:*")

    tcp = TCPSocket.new('127.0.0.1', port)
    tcp.write("hello")

    Timeout.timeout(5) do
      msg = sock.recv_message

      # Messages received from a STREAM socket are in two parts:
      # The first frame is an identiy frame.
      # The second frame is the data received over the TCP socket.
      assert_equal 2, msg.size

      # first frame is the identity frame.
      identity = msg.pop
      assert_equal "hello", msg.first.to_s

      sock.sendm(identity)
      sock.send("goodbye")

      reply = tcp.recv(100)
      assert_equal "goodbye", reply
    end

  ensure
    tcp.close if tcp
    ctx.destroy
  end

  # This test should work, but may be a bug in zmq. leaving out for now:
  # def test_close_tcp_connection
  #   ctx = ZMQ::Context.new
  #   sock = ctx.socket(:STREAM)
  #   port = sock.bind("tcp://127.0.0.1:*")
  #
  #   tcp = TCPSocket.new('127.0.0.1', port)
  #   tcp.write("hello")
  #
  #   Timeout.timeout(5) do
  #     identity = sock.recv
  #     message = sock.recv
  #
  #     sock.sendm identity
  #     sock.send ""
  #
  #     # receiving a zero length string is a TCP end of stream = closed normally.
  #     assert_equal "", tcp.recvfrom(100)
  #   end
  #
  # ensure
  #   tcp.close if tcp
  #   ctx.destroy
  # end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rbczmq-1.7.9 test/socket/test_stream_socket.rb
rbczmq-1.7.8 test/socket/test_stream_socket.rb
rbczmq-1.7.7 test/socket/test_stream_socket.rb
rbczmq-1.7.6 test/socket/test_stream_socket.rb
rbczmq-1.7.5 test/socket/test_stream_socket.rb