/usr/lib/python2.7/dist-packages/zmq/tests/test_message.py is in python-zmq 14.0.1-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | # -*- coding: utf8 -*-
#-----------------------------------------------------------------------------
# Copyright (c) 2010-2012 Brian Granger, Min Ragan-Kelley
#
# This file is part of pyzmq
#
# Distributed under the terms of the New BSD License. The full license is in
# the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import copy
import sys
try:
from sys import getrefcount as grc
except ImportError:
grc = None
import time
from pprint import pprint
from unittest import TestCase
import zmq
from zmq.tests import BaseZMQTestCase, SkipTest, skip_pypy, PYPY
from zmq.utils.strtypes import unicode, bytes, b, u
try:
from zmq.utils.rebuffer import array_from_buffer
except:
array_from_buffer = None
#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------
# some useful constants:
x = b'x'
try:
view = memoryview
except NameError:
view = buffer
if grc:
rc0 = grc(x)
v = view(x)
view_rc = grc(x) - rc0
def await_gc(obj, rc):
"""wait for refcount on an object to drop to an expected value
Necessary because of the zero-copy gc thread,
which can take some time to receive its DECREF message.
"""
for i in range(50):
# rc + 2 because of the refs in this function
if grc(obj) <= rc + 2:
return
time.sleep(0.05)
class TestFrame(BaseZMQTestCase):
@skip_pypy
def test_above_30(self):
"""Message above 30 bytes are never copied by 0MQ."""
for i in range(5, 16): # 32, 64,..., 65536
s = (2**i)*x
self.assertEqual(grc(s), 2)
m = zmq.Frame(s)
self.assertEqual(grc(s), 4)
del m
await_gc(s, 2)
self.assertEqual(grc(s), 2)
del s
def test_str(self):
"""Test the str representations of the Frames."""
for i in range(16):
s = (2**i)*x
m = zmq.Frame(s)
m_str = str(m)
m_str_b = b(m_str) # py3compat
self.assertEqual(s, m_str_b)
def test_bytes(self):
"""Test the Frame.bytes property."""
for i in range(1,16):
s = (2**i)*x
m = zmq.Frame(s)
b = m.bytes
self.assertEqual(s, m.bytes)
if not PYPY:
# check that it copies
self.assert_(b is not s)
# check that it copies only once
self.assert_(b is m.bytes)
def test_unicode(self):
"""Test the unicode representations of the Frames."""
s = u('asdf')
self.assertRaises(TypeError, zmq.Frame, s)
for i in range(16):
s = (2**i)*u('§')
m = zmq.Frame(s.encode('utf8'))
self.assertEqual(s, unicode(m.bytes,'utf8'))
def test_len(self):
"""Test the len of the Frames."""
for i in range(16):
s = (2**i)*x
m = zmq.Frame(s)
self.assertEqual(len(s), len(m))
@skip_pypy
def test_lifecycle1(self):
"""Run through a ref counting cycle with a copy."""
for i in range(5, 16): # 32, 64,..., 65536
s = (2**i)*x
rc = 2
self.assertEqual(grc(s), rc)
m = zmq.Frame(s)
rc += 2
self.assertEqual(grc(s), rc)
m2 = copy.copy(m)
rc += 1
self.assertEqual(grc(s), rc)
buf = m2.buffer
rc += view_rc
self.assertEqual(grc(s), rc)
self.assertEqual(s, b(str(m)))
self.assertEqual(s, bytes(m2))
self.assertEqual(s, m.bytes)
# self.assert_(s is str(m))
# self.assert_(s is str(m2))
del m2
rc -= 1
self.assertEqual(grc(s), rc)
rc -= view_rc
del buf
self.assertEqual(grc(s), rc)
del m
rc -= 2
await_gc(s, rc)
self.assertEqual(grc(s), rc)
self.assertEqual(rc, 2)
del s
@skip_pypy
def test_lifecycle2(self):
"""Run through a different ref counting cycle with a copy."""
for i in range(5, 16): # 32, 64,..., 65536
s = (2**i)*x
rc = 2
self.assertEqual(grc(s), rc)
m = zmq.Frame(s)
rc += 2
self.assertEqual(grc(s), rc)
m2 = copy.copy(m)
rc += 1
self.assertEqual(grc(s), rc)
buf = m.buffer
rc += view_rc
self.assertEqual(grc(s), rc)
self.assertEqual(s, b(str(m)))
self.assertEqual(s, bytes(m2))
self.assertEqual(s, m2.bytes)
self.assertEqual(s, m.bytes)
# self.assert_(s is str(m))
# self.assert_(s is str(m2))
del buf
self.assertEqual(grc(s), rc)
del m
# m.buffer is kept until m is del'd
rc -= view_rc
rc -= 1
self.assertEqual(grc(s), rc)
del m2
rc -= 2
await_gc(s, rc)
self.assertEqual(grc(s), rc)
self.assertEqual(rc, 2)
del s
@skip_pypy
def test_tracker(self):
m = zmq.Frame(b'asdf', track=True)
self.assertFalse(m.tracker.done)
pm = zmq.MessageTracker(m)
self.assertFalse(pm.done)
del m
for i in range(10):
if pm.done:
break
time.sleep(0.1)
self.assertTrue(pm.done)
def test_no_tracker(self):
m = zmq.Frame(b'asdf', track=False)
self.assertEqual(m.tracker, None)
m2 = copy.copy(m)
self.assertEqual(m2.tracker, None)
self.assertRaises(ValueError, zmq.MessageTracker, m)
@skip_pypy
def test_multi_tracker(self):
m = zmq.Frame(b'asdf', track=True)
m2 = zmq.Frame(b'whoda', track=True)
mt = zmq.MessageTracker(m,m2)
self.assertFalse(m.tracker.done)
self.assertFalse(mt.done)
self.assertRaises(zmq.NotDone, mt.wait, 0.1)
del m
time.sleep(0.1)
self.assertRaises(zmq.NotDone, mt.wait, 0.1)
self.assertFalse(mt.done)
del m2
self.assertTrue(mt.wait() is None)
self.assertTrue(mt.done)
def test_buffer_in(self):
"""test using a buffer as input"""
ins = b("§§¶•ªº˜µ¬˚…∆˙åß∂©œ∑´†≈ç√")
m = zmq.Frame(view(ins))
def test_bad_buffer_in(self):
"""test using a bad object"""
self.assertRaises(TypeError, zmq.Frame, 5)
self.assertRaises(TypeError, zmq.Frame, object())
def test_buffer_out(self):
"""receiving buffered output"""
ins = b("§§¶•ªº˜µ¬˚…∆˙åß∂©œ∑´†≈ç√")
m = zmq.Frame(ins)
outb = m.buffer
self.assertTrue(isinstance(outb, view))
self.assert_(outb is m.buffer)
self.assert_(m.buffer is m.buffer)
def test_multisend(self):
"""ensure that a message remains intact after multiple sends"""
a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
s = b"message"
m = zmq.Frame(s)
self.assertEqual(s, m.bytes)
a.send(m, copy=False)
time.sleep(0.1)
self.assertEqual(s, m.bytes)
a.send(m, copy=False)
time.sleep(0.1)
self.assertEqual(s, m.bytes)
a.send(m, copy=True)
time.sleep(0.1)
self.assertEqual(s, m.bytes)
a.send(m, copy=True)
time.sleep(0.1)
self.assertEqual(s, m.bytes)
for i in range(4):
r = b.recv()
self.assertEqual(s,r)
self.assertEqual(s, m.bytes)
def test_buffer_numpy(self):
"""test non-copying numpy array messages"""
try:
import numpy
except ImportError:
raise SkipTest("numpy required")
rand = numpy.random.randint
shapes = [ rand(2,16) for i in range(5) ]
for i in range(1,len(shapes)+1):
shape = shapes[:i]
A = numpy.random.random(shape)
m = zmq.Frame(A)
if view.__name__ == 'buffer':
self.assertEqual(A.data, m.buffer)
B = numpy.frombuffer(m.buffer,dtype=A.dtype).reshape(A.shape)
else:
self.assertEqual(memoryview(A), m.buffer)
B = numpy.array(m.buffer,dtype=A.dtype).reshape(A.shape)
self.assertEqual((A==B).all(), True)
def test_memoryview(self):
"""test messages from memoryview"""
major,minor = sys.version_info[:2]
if not (major >= 3 or (major == 2 and minor >= 7)):
raise SkipTest("memoryviews only in python >= 2.7")
s = b'carrotjuice'
v = memoryview(s)
m = zmq.Frame(s)
buf = m.buffer
s2 = buf.tobytes()
self.assertEqual(s2,s)
self.assertEqual(m.bytes,s)
def test_noncopying_recv(self):
"""check for clobbering message buffers"""
null = b'\0'*64
sa,sb = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
for i in range(32):
# try a few times
sb.send(null, copy=False)
m = sa.recv(copy=False)
mb = m.bytes
# buf = view(m)
buf = m.buffer
del m
for i in range(5):
ff=b'\xff'*(40 + i*10)
sb.send(ff, copy=False)
m2 = sa.recv(copy=False)
if view.__name__ == 'buffer':
b = bytes(buf)
else:
b = buf.tobytes()
self.assertEqual(b, null)
self.assertEqual(mb, null)
self.assertEqual(m2.bytes, ff)
@skip_pypy
def test_buffer_numpy(self):
"""test non-copying numpy array messages"""
try:
import numpy
except ImportError:
raise SkipTest("requires numpy")
if sys.version_info < (2,7):
raise SkipTest("requires new-style buffer interface (py >= 2.7)")
rand = numpy.random.randint
shapes = [ rand(2,5) for i in range(5) ]
a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
dtypes = [int, float, '>i4', 'B']
for i in range(1,len(shapes)+1):
shape = shapes[:i]
for dt in dtypes:
A = numpy.random.uniform(-10000000,
1000000, size=shape).astype(dt)
a.send(A, copy=False)
msg = b.recv(copy=False)
B = array_from_buffer(msg, A.dtype, A.shape)
self.assertEqual(A.shape, B.shape)
self.assertTrue((A==B).all())
A = numpy.ndarray(shape, dtype=[('a', int), ('b', float), ('c', 'a32')])
A['a'] = 1024
A['b'] = 1e9
A['c'] = 'hello there'
a.send(A, copy=False)
msg = b.recv(copy=False)
B = array_from_buffer(msg, A.dtype, A.shape)
self.assertEqual(A.shape, B.shape)
self.assertTrue((A==B).all())
def test_frame_more(self):
"""test Frame.more attribute"""
frame = zmq.Frame(b"hello")
self.assertFalse(frame.more)
sa,sb = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
sa.send_multipart([b'hi', b'there'])
frame = self.recv(sb, copy=False)
self.assertTrue(frame.more)
if zmq.zmq_version_info()[0] >= 3 and not PYPY:
self.assertTrue(frame.get(zmq.MORE))
frame = self.recv(sb, copy=False)
self.assertFalse(frame.more)
if zmq.zmq_version_info()[0] >= 3 and not PYPY:
self.assertFalse(frame.get(zmq.MORE))
|