def bounce(self, sb, sc):
     from ctypes import c_char_p, c_int, sizeof, c_size_t, c_ubyte
     from crossroads.lowest import array
     XS, xs = twoget(self)
     content = c_char_p(b'12345678ABCDEFGH12345678abcdefgh')
     # send the message.
     self.assertEqual(xs.send(sc, content, 32, XS.SNDMORE), 32)
     self.assertEqual(xs.send(sc, content, 32, 0), 32)
     # bounce the message back.
     buf1 = array(c_ubyte, 32)
     self.assertEqual(xs.recv(sb, buf1, 32, 0), 32)
     rcvmore = c_int()
     sz = c_size_t(sizeof(rcvmore))
     self.assertEqual(xs.getsockopt(sb, XS.RCVMORE, rcvmore, sz), 0)
     self.assertTrue(rcvmore)
     self.assertEqual(xs.recv(sb, buf1, 32, 0), 32)
     self.assertEqual(xs.getsockopt(sb, XS.RCVMORE, rcvmore, sz), 0)
     self.assertFalse(rcvmore)
     self.assertEqual(xs.send(sb, buf1, 32, XS.SNDMORE), 32)
     self.assertEqual(xs.send(sb, buf1, 32, 0), 32)
     # receive the bounced message.
     buf2 = array(c_ubyte, 32)
     self.assertEqual(xs.recv(sc, buf2, 32, 0), 32)
     self.assertEqual(xs.getsockopt(sc, XS.RCVMORE, rcvmore, sz), 0)
     self.assertTrue(rcvmore)
     self.assertEqual(xs.recv(sc, buf2, 32, 0), 32)
     self.assertEqual(xs.getsockopt(sc, XS.RCVMORE, rcvmore, sz), 0)
     self.assertFalse(rcvmore)
     # check whether the message is still the same.
     self.assertEqual(bytearray(buf2), content.value)
 def test_hwn(self):
     from ctypes import sizeof, byref, c_int, c_ubyte
     from crossroads.lowest import array
     XS, xs = twoget(self)
     ctx = xs.init()
     self.assertTrue(ctx)
     # Create pair of socket, each with high watermark of 2. Thus the total
     # buffer space should be 4 messages.
     sb = xs.socket(ctx, XS.PULL)
     self.assertTrue(sb)
     hwm = c_int(2)
     self.assertEqual(
         xs.setsockopt(sb, XS.RCVHWM, byref(hwm), sizeof(hwm)), 0
     )
     self.assertNotEqual(xs.bind(sb, b'inproc://a'), -1)
     sc = xs.socket(ctx, XS.PUSH)
     self.assertTrue(sc)
     self.assertEqual(
         xs.setsockopt(sc, XS.SNDHWM, byref(hwm), sizeof(hwm)), 0
     )
     self.assertNotEqual(xs.connect(sc, b'inproc://a'), -1)
     # try to send 10 messages. Only 4 should succeed.
     for t in range(10):
         try:
             rc = xs.send(sc, None, 0, XS.DONTWAIT)
             if t < 4:
                 self.assertEqual(rc, 0)
         except xs.XSError as e:
             self.assertTrue(e.errno, XS.EAGAIN)
     # There should be now 4 messages pending, consume them.
     for i in lrange(4):  #  @UnusedVariable
         self.assertEqual(xs.recv(sb, None, 0, 0), 0)
     # Now it should be possible to send one more.
     self.assertEqual(xs.send(sc, None, 0, 0), 0)
     # Consume the remaining message.
     self.assertEqual(xs.recv(sb, None, 0, 0), 0)
     self.assertEqual(xs.close(sc), 0)
     self.assertEqual(xs.close(sb), 0)
     self.assertEqual(xs.term(ctx), 0)
     # Following part of the tests checks whether small HWMs don't interact
     # with command throttling in strange ways.
     ctx = xs.init()
     self.assertTrue(ctx)
     s1 = xs.socket(ctx, XS.PULL)
     self.assertTrue(s1)
     s2 = xs.socket(ctx, XS.PUSH)
     self.assertTrue(s2)
     hwm = c_int(5)
     self.assertEqual(
         xs.setsockopt(s2, XS.SNDHWM, byref(hwm), sizeof(hwm)), 0
     )
     self.assertTrue(xs.bind(s1, b'tcp://127.0.0.1:5858') >= 0)
     self.assertTrue(xs.connect(s2, b'tcp://127.0.0.1:5858') >= 0)
     for i in lrange(10):  #  @UnusedVariable
         self.assertEqual(xs.send(s2, b'test', 4, XS.DONTWAIT), 4)
         buf = array(c_ubyte, 4)
         self.assertEqual(xs.recv(s1, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.close(s2), 0)
     self.assertEqual(xs.close(s1), 0)
     self.assertEqual(xs.term(ctx), 0)
 def test_invalid_rep(self):
     from stuf.six import int2byte
     from ctypes import byref, sizeof, c_int, c_ubyte
     from crossroads.lowest import array
     XS, xs = twoget(self)
     # create REQ/XREP wiring.
     ctx = xs.init()
     self.assertTrue(ctx)
     xrep_socket = xs.socket(ctx, XS.XREP)
     self.assertTrue(xrep_socket)
     req_socket = xs.socket(ctx, XS.REQ)
     self.assertTrue(req_socket)
     linger = c_int(0)
     self.assertEqual(xs.setsockopt(
         xrep_socket, XS.LINGER, byref(linger), sizeof(linger)
     ), 0)
     self.assertEqual(xs.setsockopt(
         req_socket, XS.LINGER, byref(linger), sizeof(linger)
     ), 0)
     self.assertNotEqual(xs.bind(xrep_socket, b'inproc://hi'), -1)
     self.assertNotEqual(xs.connect(req_socket, b'inproc://hi'), -1)
     # initial request.
     self.assertEqual(xs.send(req_socket, b'r', 1, 0), 1)
     # receive the request.
     addr = array(c_ubyte, 32)
     addr_size = xs.recv(xrep_socket, addr, sizeof(addr), 0)
     self.assertTrue(addr_size >= 0)
     bottom = array(c_ubyte, 1)
     self.assertEqual(xs.recv(xrep_socket, bottom, sizeof(bottom), 0), 0)
     body = array(c_ubyte, 1)
     self.assertEqual(xs.recv(xrep_socket, body, sizeof(body), 0), 1)
     # send invalid reply.
     self.assertEqual(xs.send(xrep_socket, addr, addr_size, 0), addr_size)
     # send valid reply.
     self.assertEqual(
         xs.send(xrep_socket, addr, addr_size, XS.SNDMORE), addr_size
     )
     self.assertEqual(xs.send(xrep_socket, bottom, 0, XS.SNDMORE), 0)
     self.assertEqual(xs.send(xrep_socket, b'b', 1, 0), 1)
     # check whether we've got the valid reply.
     self.assertEqual(xs.recv(req_socket, body, sizeof(body), 0), 1)
     self.assertEqual(int2byte(body[0]), b'b')
     # tear down the wiring.
     self.assertEqual(xs.close(xrep_socket), 0)
     self.assertEqual(xs.close(req_socket), 0)
     self.assertEqual(xs.term(ctx), 0)
 def test_shutdown(self):
     from time import sleep
     from ctypes import sizeof, c_ubyte
     from crossroads.lowest import array
     XS, xs = twoget(self)
     buf = array(c_ubyte, 32)
     # Create infrastructure.
     ctx = xs.init()
     self.assertTrue(ctx)
     push = xs.socket(ctx, XS.PUSH)
     self.assertTrue(push)
     push_id = xs.bind(push, b'tcp://127.0.0.1:5560')
     self.assertNotEqual(push_id, -1)
     pull = xs.socket(ctx, XS.PULL)
     self.assertTrue(pull)
     self.assertNotEqual(xs.connect(pull, b'tcp://127.0.0.1:5560'), -1)
     # Pass one message through to ensure the connection is established.
     self.assertEqual(xs.send(push, b'ABC', 3, 0), 3)
     self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
     # Shut down the bound endpoint.
     self.assertEqual(xs.shutdown(push, push_id), 0)
     sleep(1)
     try:
         # Check that sending would block (there's no outbound connection).
         xs.send(push, b'ABC', 3, XS.DONTWAIT)
     except xs.XSError as e:
         self.assertEqual(e.errno, XS.EAGAIN)
     # Clean up.
     self.assertEqual(xs.close(pull), 0)
     self.assertEqual(xs.close(push), 0)
     self.assertEqual(xs.term(ctx), 0)
     # Now the other way round...Create infrastructure.
     ctx = xs.init()
     self.assertTrue(ctx)
     pull = xs.socket(ctx, XS.PULL)
     self.assertTrue(pull)
     self.assertNotEqual(xs.bind(pull, b'tcp://127.0.0.1:5560'), -1)
     push = xs.socket(ctx, XS.PUSH)
     self.assertTrue(push)
     push_id = xs.connect(push, b'tcp://127.0.0.1:5560')
     self.assertNotEqual(push_id, -1)
     # Pass one message through to ensure the connection is established.
     self.assertEqual(xs.send(push, b'ABC', 3, 0), 3)
     self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
     # Shut down the bound endpoint.
     self.assertEqual(xs.shutdown(push, push_id), 0)
     sleep(1)
     try:
         # Check that sending would block (there's no outbound connection).
         xs.send(push, b'ABC', 3, XS.DONTWAIT)
     except xs.XSError as e:
         self.assertEqual(e.errno, XS.EAGAIN)
     # Clean up.
     self.assertEqual(xs.close(pull), 0)
     self.assertEqual(xs.close(push), 0)
     self.assertEqual(xs.term(ctx), 0)
 def test_sub_forward(self):
     from time import sleep
     from ctypes import sizeof, c_ubyte
     from crossroads.lowest import array
     XS, xs = twoget(self)
     ctx = xs.init()
     self.assertTrue(ctx)
     # First, create an intermediate device.
     xpub = xs.socket(ctx, XS.XPUB)
     self.assertTrue(xpub)
     self.assertNotEqual(xs.bind(xpub, b'tcp://127.0.0.1:5560'), -1)
     xsub = xs.socket(ctx, XS.XSUB)
     self.assertTrue(xsub)
     self.assertNotEqual(xs.bind(xsub, b'tcp://127.0.0.1:5561'), -1)
     # Create a publisher.
     pub = xs.socket(ctx, XS.PUB)
     self.assertTrue(pub)
     self.assertNotEqual(xs.connect(pub, b'tcp://127.0.0.1:5561'), -1)
     # Create a subscriber.
     sub = xs.socket(ctx, XS.SUB)
     self.assertTrue(sub)
     self.assertNotEqual(xs.connect(sub, b'tcp://127.0.0.1:5560'), -1)
     # Subscribe for all messages.
     self.assertEqual(xs.setsockopt(sub, XS.SUBSCRIBE, '', 0), 0)
     # Pass the subscription upstream through the device.
     buff = array(c_ubyte, 32)
     rc = xs.recv(xpub, buff, sizeof(buff), 0)
     self.assertTrue(rc >= 0)
     self.assertTrue(xs.send(xsub, buff, rc, 0) >= 0)
     # Wait a bit till the subscription gets to the publisher.
     sleep(1)
     # Send an empty message.
     self.assertEqual(xs.send(pub, None, 0, 0), 0)
     # Pass the message downstream through the device.
     rc = xs.recv(xsub, buff, sizeof(buff), 0)
     self.assertTrue(rc >= 0)
     self.assertTrue(xs.send(xpub, buff, rc, 0) >= 0)
     # Receive the message in the subscriber.
     self.assertEqual(xs.recv(sub, buff, sizeof(buff), 0), 0)
     # Clean up.
     self.assertEqual(xs.close(xpub), 0)
     self.assertEqual(xs.close(xsub), 0)
     self.assertEqual(xs.close(pub), 0)
     self.assertEqual(xs.close(sub), 0)
     self.assertEqual(xs.term(ctx), 0)
 def test_reconnect(self):
     import sys
     from time import sleep
     from ctypes import sizeof, c_ubyte
     from crossroads.lowest import array
     XS, xs = twoget(self)
     # create the basic infrastructure.
     ctx = xs.init()
     self.assertTrue(ctx)
     push = xs.socket(ctx, XS.PUSH)
     self.assertTrue(push)
     pull = xs.socket(ctx, XS.PULL)
     self.assertTrue(push)
     # connect before bind was done at the peer and send one message.
     self.assertNotEqual(xs.connect(push, b'tcp://127.0.0.1:5560'), -1)
     self.assertEqual(xs.send(push, b'ABC', 3, 0), 3)
     # wait a while for few attempts to reconnect to happen.
     sleep(1)
     # bind the peer and get the message.
     self.assertNotEqual(xs.bind(pull, b'tcp://127.0.0.1:5560'), -1)
     buf = array(c_ubyte, 3)
     self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
     # clean up.
     self.assertEqual(xs.close(push), 0)
     self.assertEqual(xs.close(pull), 0)
     if sys.platform not in ('win32', 'cygwin'):
         # now, let's test the same scenario with IPC.
         push = xs.socket(ctx, XS.PUSH)
         self.assertTrue(push)
         pull = xs.socket(ctx, XS.PULL)
         self.assertTrue(push)
         # connect before bind was done at the peer and send one message.
         self.assertNotEqual(xs.connect(push, b'ipc:///tmp/tester'), -1)
         self.assertEqual(xs.send(push, b'ABC', 3, 0), 3)
         # wait a while for few attempts to reconnect to happen.
         sleep(1)
         # bind the peer and get the message.
         self.assertNotEqual(xs.bind(pull, b'ipc:///tmp/tester'), -1)
         self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
         # clean up.
         self.assertEqual(xs.close(push), 0)
         self.assertEqual(xs.close(pull), 0)
     self.assertEqual(xs.term(ctx), 0)
 def test_wireformat(self):
     XS, xs = twoget(self)
     import socket
     from ctypes import sizeof, c_ubyte
     from crossroads.lowest import array
     # create the basic infrastructure.
     ctx = xs.init()
     self.assertTrue(ctx)
     push = xs.socket(ctx, XS.PUSH)
     self.assertTrue(push)
     pull = xs.socket(ctx, XS.PULL)
     self.assertTrue(push)
     # bind the peer and get the message.
     self.assertNotEqual(xs.bind(pull, b'tcp://127.0.0.1:5560'), -1)
     self.assertNotEqual(xs.bind(push, b'tcp://127.0.0.1:5561'), -1)
     # connect to the peer using raw sockets.
     rpush = socket.socket(
         socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP
     )
     rpush.connect(('127.0.0.1', 5560))
     rpull = socket.socket(
         socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP,
     )
     rpull.connect(('127.0.0.1', 5561))
     # let's send some data and check if it arrived
     self.assertEqual(rpush.send(b'\x04\0abc', 0), 5)
     buf = array(c_ubyte, 3)
     self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
     self.assertNotEqual(buf, b'abc')
     # let's push this data into another socket
     self.assertEqual(xs.send(push, buf, sizeof(buf), 0), 3)
     self.assertNotEqual(rpull.recv(3, 0), b'\x04\0abc')
     rpush.close()
     rpull.close()
     self.assertEqual(xs.close(pull), 0)
     self.assertEqual(xs.close(push), 0)
     self.assertEqual(xs.term(ctx), 0)
 def test_survey(self):
     from crossroads.lowest import array
     from ctypes import sizeof, byref, c_int, c_ubyte
     XS, xs = twoget(self)
     buf = array(c_ubyte, 32)
     # Create the basic infrastructure.
     ctx = xs.init()
     self.assertTrue(ctx)
     xsurveyor = xs.socket(ctx, XS.XSURVEYOR)
     self.assertTrue(xsurveyor)
     self.assertNotEqual(xs.bind(xsurveyor, b'inproc://a'), -1)
     xrespondent = xs.socket(ctx, XS.XRESPONDENT)
     self.assertTrue(xrespondent)
     self.assertNotEqual(xs.bind(xrespondent, b'inproc://b'), -1)
     surveyor = xs.socket(ctx, XS.SURVEYOR)
     self.assertTrue(surveyor)
     self.assertNotEqual(xs.connect(surveyor, b'inproc://b'), -1)
     respondent1 = xs.socket(ctx, XS.RESPONDENT)
     self.assertTrue(respondent1)
     self.assertNotEqual(xs.connect(respondent1, b'inproc://a'), -1)
     respondent2 = xs.socket(ctx, XS.RESPONDENT)
     self.assertTrue(respondent2)
     self.assertNotEqual(xs.connect(respondent2, b'inproc://a'), -1)
     # Send the survey.
     self.assertEqual(xs.send(surveyor, b'ABC', 3, 0), 3)
     # Forward the survey through the intermediate device.
     # Survey consist of identity(4 bytes), survey ID(4 bytes) and the body.
     self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xsurveyor, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xsurveyor, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 3)
     self.assertEqual(xs.send(xsurveyor, buf, 3, 0), 3)
     # Respondent 1 responds to the survey.
     self.assertEqual(xs.recv(respondent1, buf, sizeof(buf), 0), 3)
     self.assertEqual(xs.send(respondent1, b'DE', 2, 0), 2)
     # Forward the response through the intermediate device.
     self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 2)
     self.assertEqual(xs.send(xrespondent, buf, 2, 0), 2)
     # Surveyor gets the response.
     self.assertEqual(xs.recv(surveyor, buf, sizeof(buf), 0), 2)
     # Respondent 2 responds to the survey.
     self.assertEqual(xs.recv(respondent2, buf, sizeof(buf), 0), 3)
     self.assertEqual(xs.send(respondent2, b'FGHI', 4, 0), 4)
     # Forward the response through the intermediate device.
     self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xrespondent, buf, 4, 0), 4)
     # Surveyor gets the response.
     self.assertEqual(xs.recv(surveyor, buf, sizeof(buf), 0), 4)
     # Now let's test whether survey timeout works as expected.
     timeout = c_int(100)
     self.assertEqual(xs.setsockopt(
         surveyor, XS.SURVEY_TIMEOUT, byref(timeout), sizeof(timeout)
     ), 0)
     self.assertEqual(xs.send(surveyor, b'ABC', 3, 0), 3)
     watch = xs.stopwatch_start()
     try:
         xs.recv(surveyor, buf, sizeof(buf), 0)
     except xs.XSError as e:
         self.assertEqual(e.errno, XS.ETIMEDOUT)
     self.time_assert(xs.stopwatch_stop(watch) / 1000, timeout.value)
     # Test whether responses for old surveys are discarded. First,
     # initiate new survey.
     self.assertEqual(xs.setsockopt(
         surveyor, XS.SURVEY_TIMEOUT, byref(timeout), sizeof(timeout)
     ), 0)
     self.assertEqual(xs.send(surveyor, b'DE', 2, 0), 2)
     # Read, process and reply to the old survey.
     self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 3)
     self.assertEqual(xs.send(xrespondent, buf, 3, 0), 3)
     # Read, process and reply to the new survey.
     self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
     self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
     self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 2)
     self.assertEqual(xs.send(xrespondent, buf, 2, 0), 2)
     # Get the response and check it's the response to the new survey and
     # that response to the old survey was silently discarded.
     self.assertEqual(xs.recv(surveyor, buf, sizeof(buf), 0), 2)
     self.assertEqual(xs.close(respondent2), 0)
     self.assertEqual(xs.close(respondent1), 0)
     self.assertEqual(xs.close(surveyor), 0)
     self.assertEqual(xs.close(xrespondent), 0)
     self.assertEqual(xs.close(xsurveyor), 0)
     self.assertEqual(xs.term(ctx), 0)
 def test_resubscribe(self):
     from stuf.six import int2byte
     from time import sleep
     from ctypes import sizeof, c_ubyte
     from crossroads.lowest import array
     XS, xs = twoget(self)
     # create the basic infrastructure.
     ctx = xs.init()
     self.assertTrue(ctx)
     xpub = xs.socket(ctx, XS.XPUB)
     self.assertTrue(xpub)
     sub = xs.socket(ctx, XS.SUB)
     self.assertTrue(sub)
     # send two subscriptions upstream.
     self.assertNotEqual(xs.bind(xpub, b'tcp://127.0.0.1:5560'), -1)
     self.assertEqual(xs.setsockopt(sub, XS.SUBSCRIBE, b'a', 1), 0)
     self.assertEqual(xs.setsockopt(sub, XS.SUBSCRIBE, b'b', 1), 0)
     self.assertNotEqual(xs.connect(sub, b'tcp://127.0.0.1:5560'), -1)
     # check whether subscriptions are correctly received.
     buf = array(c_ubyte, 5)
     self.assertEqual(xs.recv(xpub, buf, sizeof(buf), 0), 5)
     self.assertEqual(buf[0], 0)
     self.assertEqual(buf[1], 1)
     self.assertEqual(buf[2], 0)
     self.assertEqual(buf[3], 1)
     self.assertEqual(int2byte(buf[4]), b'a')
     self.assertEqual(xs.recv(xpub, buf, sizeof(buf), 0), 5)
     self.assertEqual(buf[0], 0)
     self.assertEqual(buf[1], 1)
     self.assertEqual(buf[2], 0)
     self.assertEqual(buf[3], 1)
     self.assertEqual(int2byte(buf[4]), b'b')
     # tear down the connection.
     self.assertEqual(xs.close(xpub), 0)
     sleep(1)
     # re-establish the connection.
     xpub = xs.socket(ctx, XS.XPUB)
     self.assertTrue(xpub)
     self.assertNotEqual(xs.bind(xpub, b'tcp://127.0.0.1:5560'), -1)
     # we have to give control to the SUB socket here so that it has a chance
     # to resend the subscriptions.
     try:
         xs.recv(sub, buf, sizeof(buf), XS.DONTWAIT)
     except xs.XSError as e:
         self.assertEqual(e.errno, XS.EAGAIN)
     # check whether subscriptions are correctly generated.
     self.assertEqual(xs.recv(xpub, buf, sizeof(buf), 0), 5)
     self.assertEqual(buf[0], 0)
     self.assertEqual(buf[1], 1)
     self.assertEqual(buf[2], 0)
     self.assertEqual(buf[3], 1)
     self.assertEqual(int2byte(buf[4]), b'a')
     self.assertEqual(xs.recv(xpub, buf, sizeof(buf), 0), 5)
     self.assertEqual(buf[0], 0)
     self.assertEqual(buf[1], 1)
     self.assertEqual(buf[2], 0)
     self.assertEqual(buf[3], 1)
     self.assertEqual(int2byte(buf[4]), b'b')
     # clean up.
     self.assertEqual(xs.close(sub), 0)
     self.assertEqual(xs.close(xpub), 0)
     self.assertEqual(xs.term(ctx), 0)
 def test_regrep_device(self):
     from ctypes import sizeof, c_int, c_size_t, c_ubyte
     from crossroads.lowest import array
     XS, xs = twoget(self)
     ctx = xs.init()
     self.assertTrue(ctx)
     # create a req/rep device.
     xreq = xs.socket(ctx, XS.XREQ)
     self.assertTrue(xreq)
     self.assertNotEqual(xs.bind(xreq, b'tcp://127.0.0.1:5560'), -1)
     xrep = xs.socket(ctx, XS.XREP)
     self.assertTrue(xrep)
     self.assertNotEqual(xs.bind(xrep, b'tcp://127.0.0.1:5561'), -1)
     # create a worker.
     rep = xs.socket(ctx, XS.REP)
     self.assertTrue(rep)
     self.assertNotEqual(xs.connect(rep, b'tcp://127.0.0.1:5560'), -1)
     # create a client.
     req = xs.socket(ctx, XS.REQ)
     self.assertTrue(req)
     self.assertNotEqual(xs.connect(req, b'tcp://127.0.0.1:5561'), -1)
     # send a request.
     self.assertEqual(xs.send(req, b'ABC', 3, XS.SNDMORE), 3)
     self.assertEqual(xs.send(req, b'DEF', 3, 0), 3)
     # pass the request through the device.
     for i in lrange(4):  #  @UnusedVariable
         msg = xs.msg_t()
         self.assertEqual(xs.msg_init(msg), 0)
         rc = xs.recvmsg(xrep, msg, 0)
         self.assertTrue(rc >= 0)
         rcvmore = c_int()
         sz = c_size_t(sizeof(rcvmore))
         self.assertEqual(xs.getsockopt(xrep, XS.RCVMORE, rcvmore, sz), 0)
         rc = xs.sendmsg(xreq, msg, XS.SNDMORE if rcvmore else 0)
         self.assertTrue(rc >= 0)
     # receive the request.
     buff = array(c_ubyte, 3)
     self.assertEqual(xs.recv(rep, buff, 3, 0), 3)
     self.assertEqual(bytearray(buff), b'ABC')
     rcvmore = c_int()
     sz = c_size_t(sizeof(rcvmore))
     self.assertEqual(xs.getsockopt(rep, XS.RCVMORE, rcvmore, sz), 0)
     self.assertTrue(rcvmore)
     self.assertEqual(xs.recv(rep, buff, 3, 0), 3)
     self.assertEqual(bytearray(buff), b'DEF')
     self.assertEqual(xs.getsockopt(rep, XS.RCVMORE, rcvmore, sz), 0)
     self.assertFalse(rcvmore)
     # send the reply.
     self.assertEqual(xs.send(rep, b'GHI', 3, XS.SNDMORE), 3)
     self.assertEqual(xs.send(rep, b'JKL', 3, 0), 3)
     # pass the reply through the device.
     for i in lrange(4):  #  @UnusedVariable
         msg = xs.msg_t()
         self.assertEqual(xs.msg_init(msg), 0)
         self.assertTrue(xs.recvmsg(xreq, msg, 0) >= 0)
         self.assertEqual(xs.getsockopt(xreq, XS.RCVMORE, rcvmore, sz), 0)
         self.assertTrue(
             xs.sendmsg(xrep, msg, XS.SNDMORE if rcvmore else 0) >= 0
         )
     # receive the reply.
     self.assertEqual(xs.recv(req, buff, 3, 0), 3)
     self.assertEqual(bytearray(buff), b'GHI')
     self.assertEqual(xs.getsockopt(req, XS.RCVMORE, rcvmore, sz), 0
     )
     self.assertTrue(rcvmore)
     self.assertEqual(xs.recv(req, buff, 3, 0), 3)
     self.assertEqual(bytearray(buff), b'JKL')
     self.assertEqual(xs.getsockopt(req, XS.RCVMORE, rcvmore, sz), 0)
     self.assertFalse(rcvmore)
     # clean up.
     self.assertEqual(xs.close(req), 0)
     self.assertEqual(xs.close(rep), 0)
     self.assertEqual(xs.close(xrep), 0)
     self.assertEqual(xs.close(xreq), 0)
     self.assertEqual(xs.term(ctx), 0)