Commits

Michael Granger committed d77d58a

Checkpoint: add more specs

Comments (0)

Files changed (5)

             retval = rb_str_new (identity, optvalsize);
         }
         break;
+
+	case ZMQ_LAST_ENDPOINT:
+		{
+            char endpoint[4096];
+            size_t optvalsize = sizeof (endpoint);
+
+            rc = zmq_getsockopt (s->socket, NUM2INT (option_),
+                                 (void *)endpoint, &optvalsize);
+
+            if (rc != 0) {
+              rb_raise (exception_type, "%s", zmq_strerror (zmq_errno ()));
+              return Qnil;
+            }
+
+            if (optvalsize > sizeof (endpoint))
+                optvalsize = sizeof (endpoint);
+
+            retval = rb_str_new (endpoint, optvalsize);
+		}
     default:
         rb_raise (exception_type, "%s", zmq_strerror (EINVAL));
         return Qnil;
     rb_define_const (zmq_module, "DOWNSTREAM", INT2NUM (ZMQ_DOWNSTREAM));
 #endif
 
+#ifdef ZMQ_POLLIN
+	rb_define_const (zmq_module, "POLLIN", INT2NUM (ZMQ_POLLIN));
+	rb_define_const (zmq_module, "POLLOUT", INT2NUM (ZMQ_POLLOUT));
+#endif
+
+#ifdef ZMQ_LAST_ENDPOINT
+	rb_define_const (zmq_module, "LAST_ENDPOINT", INT2NUM (ZMQ_LAST_ENDPOINT));
+#endif
+
 }
 
 module ZMQ::SpecHelpers
 
+
+	# A collection of constants to use in testing.
+	module TestConstants
+
+		# 0mq socket specifications for Handlers
+		TEST_SEND_SPEC = 'inproc://zmq-spec-socket'
+		TEST_RECV_SPEC = 'inproc://zmq-spec-socket'
+		
+	end # module TestConstants
+
+
     ### A type matcher for collections.
 	class AllBeMatcher
 
 			]
 		end
 
-	end
+	end # class AllBeMatcher
 
 
 	###############
 		AllBeMatcher.new( expected_class )
 	end
 
-end
+end # module ZMQ::SpecHelpers
 
 
 ### Mock with RSpec
 RSpec.configure do |c|
+	include ZMQ::SpecHelpers::TestConstants
+
 	c.mock_with( :rspec )
 
 	c.include( ZMQ::SpecHelpers )

spec/zmq/context_spec.rb

 
 describe ZMQ::Context do
 
-	# 0mq socket specifications for Handlers
-	TEST_SEND_SPEC = 'inproc://zmq-context-spec-socket'
-	TEST_RECV_SPEC = 'inproc://zmq-context-spec-socket'
-
-
 	it "can be created without specifying a thread pool size" do
 		ctx = described_class.new
 		ctx.should be_a( described_class )
 			@ctx.close
 		end
 
-		it "can be used to create PUSH sockets" do
+		it "can be used to create sockets" do
 			@ctx.socket( ZMQ::PUSH ).should be_a( ZMQ::Socket )
 		end
 		

spec/zmq/socket_spec.rb

+# -*- ruby -*-
+# encoding: utf-8
+# vim: set nosta noet ts=4 sw=4:
+
+require 'rspec'
+require 'zmq'
+require 'helpers'
+require 'socket'
+
+#####################################################################
+###	C O N T E X T S
+#####################################################################
+
+describe ZMQ::Socket do
+
+	before( :all ) do
+		@context = ZMQ::Context.new
+	end
+
+	after( :all ) do
+		@context.close
+	end
+
+
+	it "can't be instantiated directly" do
+		expect {
+			described_class.new
+		}.to raise_error( TypeError, /allocator/i )
+	end
+
+
+	describe "instances" do
+	
+		before( :each ) do
+			@socket = @context.socket( ZMQ::PUSH )
+		end
+
+		after( :each ) do
+			@socket.setsockopt( ZMQ::LINGER, 0 )
+			@socket.close
+		end
+
+
+		it "allow socket options to be fetched" do
+			@socket.getsockopt( ZMQ::TYPE ).should == ZMQ::PUSH
+		end
+
+		it "allow socket options to be set" do
+			@socket.setsockopt( ZMQ::IDENTITY, 'notices' )
+			@socket.getsockopt( ZMQ::IDENTITY ).should == 'notices'
+		end
+
+		it "can bind to (listen on) an endpoint" do
+			@socket.bind( TEST_RECV_SPEC )
+		end
+
+	end
+
+	describe "a pair of PUSH/PULL instances" do
+
+		before( :each ) do
+			@push = @context.socket( ZMQ::PUSH )
+			@pull = @context.socket( ZMQ::PULL )
+		end
+
+		after( :each ) do
+			@push.setsockopt( ZMQ::LINGER, 0 )
+			@push.close
+			@pull.setsockopt( ZMQ::LINGER, 0 )
+			@pull.close
+		end
+
+
+		it "can send and receive messages" do
+			@pull.bind( TEST_RECV_SPEC )
+			@push.connect( TEST_SEND_SPEC )
+
+			@push.send( 'some stuff' )
+			sleep 0.1 until ( @pull.getsockopt(ZMQ::EVENTS) & ZMQ::POLLIN ).nonzero?
+			@pull.recv.should == 'some stuff'
+		end
+		
+	end
+
+end
+
 
 describe ZMQ do
 
-	# 0mq socket specifications for Handlers
-	TEST_SEND_SPEC = 'inproc://zmq-spec-socket'
-	TEST_RECV_SPEC = 'inproc://zmq-spec-socket'
-
 	it "knows what version of the zeromq library it's using" do
 		described_class.zeromq_version.should be_an( Array )
 		described_class.zeromq_version.should have( 3 ).members