Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 90 lines (74 sloc) 2.362 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

require File.join(File.dirname(__FILE__), %w[spec_helper])

if version2?

  module ZMQ
    describe Device do
      include APIHelper

      before(:each) do
        @ctx = Context.new
      end

      after(:each) do
        @ctx.terminate
      end
      
      def create_streamer
        @backport = @frontport = nil
        Thread.new do
          back = @ctx.socket(ZMQ::PULL)
          @backport = bind_to_random_tcp_port(back)
          front = @ctx.socket(ZMQ::PUSH)
          @frontport = bind_to_random_tcp_port(front)
          Device.new(ZMQ::STREAMER, back, front)
          back.close
          front.close
        end
        sleep 0.5
      end
      
      it "should create a streamer device without error given valid opts" do
        create_streamer
      end
      
      it "should be able to send messages through the device" do
        create_streamer
      
        pusher = @ctx.socket(ZMQ::PUSH)
        pusher.connect("tcp://127.0.0.1:#{@backport}")
        puller = @ctx.socket(ZMQ::PULL)
        puller.connect("tcp://127.0.0.1:#{@frontport}")
      
        pusher.send_string("hello")
        sleep 0.5
        res = ''
        rc = puller.recv_string(res, ZMQ::NonBlocking)
        res.should == "hello"
        
        pusher.close
        puller.close
        sleep 0.5
      end
      
      it "should raise an ArgumentError when trying to pass non-socket objects into the device" do
        lambda {
          Device.new(ZMQ::STREAMER, 1,2)
        }.should raise_exception(ArgumentError)
      end
      
      it "should be able to create a forwarder device without error" do
        Thread.new do
          back = @ctx.socket(ZMQ::SUB)
          bind_to_random_tcp_port(back)
          front = @ctx.socket(ZMQ::PUB)
          bind_to_random_tcp_port(front)
          Device.new(ZMQ::FORWARDER, back, front)
          back.close
          front.close
        end
        sleep 0.5
      end
      
      it "should be able to create a queue device without error" do
        Thread.new do
          back = @ctx.socket(ZMQ::ROUTER)
          bind_to_random_tcp_port(back)
          front = @ctx.socket(ZMQ::DEALER)
          bind_to_random_tcp_port(front)
          Device.new(ZMQ::QUEUE, back, front)
          back.close
          front.close
        end
        sleep 0.5
      end
    end
  end

end # if version2?
Something went wrong with that request. Please try again.