def test_regrep_device(self): from stuf.six import byteme # create a req/rep device xreq = self.ctx.xreq() xrep = self.ctx.xrep() # create a worker rep = self.ctx.rep() # create a client req = self.ctx.req() with xreq.bind_tcp('127.0.0.1:5560'), xrep.bind_tcp('127.0.0.1:5561'),\ rep.connect_tcp('127.0.0.1:5560'), req.connect_tcp('127.0.0.1:5561'): # send a request self.assertEqual(req.send(b'ABC', 3, True).last_rc, 3) self.assertEqual(req.send(b'DEF', 3).last_rc, 3) # pass the reply through the device for i in lrange(4): # @UnusedVariable msg = xrep.recvmsg() self.assertTrue(msg.last_rc >= 0) rc = xreq.sendmsg(msg, True if msg.more else False).last_rc self.assertTrue(rc >= 0) self.assertEqual(msg.more, 0) # receive the request buff = rep.recv(3) self.assertEqual(byteme(buff), b'ABC') self.assertEqual(buff.last_rc, 3) self.assertEqual(buff.more, 1) buff = rep.recv(3) self.assertEqual(buff.last_rc, 3) self.assertEqual(byteme(buff), b'DEF') self.assertEqual(buff.more, 0) # send the reply self.assertEqual(rep.send(b'GHI', 3, more=True).last_rc, 3) self.assertEqual(rep.send(b'JKL', 3).last_rc, 3) # pass the reply through the device for i in lrange(4): # @UnusedVariable msg = xreq.recvmsg() self.assertTrue(msg.last_rc >= 0) rc = xrep.sendmsg(msg, True if msg.more else False).last_rc self.assertTrue(rc >= 0) # receive the reply buff = req.recv(3) self.assertEqual(buff.last_rc, 3) self.assertEqual(byteme(buff), b'GHI') self.assertEqual(buff.more, 1) buff2 = req.recv(3) self.assertEqual(buff2.last_rc, 3) self.assertEqual(byteme(buff2), b'JKL') self.assertEqual(buff2.more, 0)
def test_wireformat(self): import socket from stuf.six import byteme # create the basic infrastructure push = self.ctx.push() pull = self.ctx.pull() # bind the peer and get the message with pull.bind_tcp('127.0.0.1:5560'), push.bind_tcp('127.0.0.1:5561'): # 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 = pull.recv(3) self.assertEqual(pull.last_rc, 3) self.assertEqual(byteme(buf), b'abc') # let's push this data into another socket self.assertEqual(push.send(buf).last_rc, 3) self.assertNotEqual(rpull.recv(3), b'\x04\0abc') rpush.close() rpull.close()
def __init__(self, size, source): super(SendMsg, self).__init__() if isinstance(source, Msg): self.msg = source = byteme(source) elif isunicode(source): self.msg = source = tobytes(source) else: self.msg = source self.size = len(source) if size is None else size
def bounce(self, sb, sc): from stuf.six import byteme content = b'12345678ABCDEFGH12345678abcdefgh' # send the message self.assertEqual(sc.send(content, more=True).last_rc, 32) self.assertEqual(sc.send(content).last_rc, 32) # bounce the message back buf1 = sb.recv(32) self.assertEqual(len(buf1), 32) self.assertEqual(buf1.more, 1) buf2 = sb.recv(32) self.assertEqual(len(buf2), 32) self.assertEqual(buf2.more, 0) self.assertEqual(sb.send(buf1, more=True).last_rc, 32) self.assertEqual(sb.send(buf2).last_rc, 32) # receive the bounced message buf1 = sc.recv(32) self.assertEqual(buf1.last_rc, 32) self.assertEqual(buf1.more, 1) buf2 = sc.recv(32) self.assertEqual(len(buf2), 32) self.assertEqual(buf2.more, 0) # check whether the message is still the same self.assertEqual(byteme(buf1), content)