def test_resubscribe(self): try: from stuf.six import int2byte from time import sleep # Create the basic infrastructure. xpub = self.ctx.xpub() sub = self.ctx.sub() xpub.bind_tcp('127.0.0.1:5560') # Send two subscriptions upstream. sub.subscribe = b'a' self.assertEqual(sub.last_rc, 0) sub.subscribe = b'b' self.assertEqual(sub.last_rc, 0) sub.connect_tcp('127.0.0.1:5560') self.assertTrue(sub.last_rc >= -1) # Check whether subscriptions are correctly received. buf = xpub.recv(5, char=True).array self.assertEqual(xpub.last_rc, 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') buf = xpub.recv(5, char=True).array self.assertEqual(xpub.last_rc, 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(xpub.close(), 0) sleep(1) # Re-establish the connection. xpub = self.ctx.xpub() self.assertNotEqual(xpub.bind_tcp('127.0.0.1:5560'), -1) # We have to give control to the SUB socket here so that it has # chance to resend the subscriptions. try: sub.recv(3, nowait=True) except self.xs.XSError as e: self.assertEqual(e.errno, self.XS.EAGAIN) # Check whether subscriptions are correctly generated. buf = xpub.recv(5, char=True).array self.assertEqual(xpub.last_rc, 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') buf = xpub.recv(5, char=True).array self.assertEqual(xpub.last_rc, 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') finally: xpub.close() sub.close()
def test_invalid_rep(self): from stuf.six import int2byte # create REQ/XREP wiring xrep_socket = self.ctx.xrep(linger=0) req_socket = self.ctx.req(linger=0) with xrep_socket.bind_inproc('hi'), req_socket.connect_inproc('hi'): # initial request. self.assertEqual(req_socket.send('r').last_rc, 1) # receive the request addr = xrep_socket.recv(32) addr_size = addr.last_rc self.assertEqual(addr_size, 5) bottom = xrep_socket.recv(1) self.assertEqual(bottom.last_rc, 0) body = xrep_socket.recv(1) self.assertEqual(body.last_rc, 1) # send invalid reply self.assertEqual(xrep_socket.send(addr, 5).last_rc, addr_size) # send valid reply self.assertEqual( xrep_socket.send(addr, 5, True).last_rc, addr_size, ) self.assertEqual( xrep_socket.send(bottom, 0, True).last_rc, 0 ) self.assertEqual(xrep_socket.send('b').last_rc, 1) # check whether we've got the valid reply body = req_socket.recv(1, nowait=True) self.assertEqual(body.last_rc, 1) self.assertEqual(int2byte(body.msg[0]), b'b')
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_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)