chuckremes / ffi-rzmq

FFI bindings for ZeroMQ so the library can be used under JRuby and other FFI-compliant ruby runtimes
242 stars 62 forks source link

option ROUTER_MANDATORY has no effect #115

Closed paddor closed 9 years ago

paddor commented 10 years ago

The following code provides a simple way to test if it works or not (unless I missed something). It prints BAD! Should have raised! on my computer.

require 'ffi-rzmq'

class ZMQTest
  def initialize(addr)
    @ctx = ::ZMQ::Context.create
    @socket = @ctx.socket(::ZMQ::ROUTER)
    warn "setting option ::ZMQ::ROUTER_MANDATORY (#{::ZMQ::ROUTER_MANDATORY}) to 1"
    @socket.setsockopt(::ZMQ::ROUTER_MANDATORY, 1)
    @socket.bind(addr)
  end

  def test
    warn "sending message"
    @socket.send_strings(["test_client", "foo"])
  rescue
    warn "YAY!"
  else
    warn "BAD! Should have raised!"
  end
end

ZMQTest.new("inproc://server").test

Also as gist: https://gist.github.com/paddor/70fbbce8071f6ba1288e

paddor commented 10 years ago

Any news on this?

chuckremes commented 9 years ago

Sorry for the long delay in responding. This is not a bug. The Socket#send* methods never raise exceptions. They return -1 from the method and require you to handle the error.

I modified your example to prove it.

require 'ffi-rzmq'

class ZMQTest
  def initialize(addr)
    @ctx = ::ZMQ::Context.create
    @socket = @ctx.socket(::ZMQ::ROUTER)
    warn "setting option ::ZMQ::ROUTER_MANDATORY (#{::ZMQ::ROUTER_MANDATORY}) to 1"
    @socket.setsockopt(::ZMQ::ROUTER_MANDATORY, 1)
    @socket.bind(addr)
  end

  def test
    warn "sending message"
    rc = @socket.send_strings(["test_client", "foo"])
  rescue
    warn "BAD! Sending messages should never raise an exception!"
  else
    warn "Failed and returned [#{rc}]. Check ZMQ::Util.errno and handle it."
  end
end

ZMQTest.new("inproc://server").test