~ubuntu-branches/ubuntu/vivid/pyzmq/vivid

« back to all changes in this revision

Viewing changes to zmq/tests/test_message.py

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-09-08 13:50:41 UTC
  • Revision ID: james.westby@ubuntu.com-20100908135041-bogeb6ykukjrcd89
Tags: upstream-0.1.20100703+git18f5d06155
ImportĀ upstreamĀ versionĀ 0.1.20100703+git18f5d06155

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
#    Copyright (c) 2010 Brian E. Granger
 
3
#
 
4
#    This file is part of pyzmq.
 
5
#
 
6
#    pyzmq is free software; you can redistribute it and/or modify it under
 
7
#    the terms of the Lesser GNU General Public License as published by
 
8
#    the Free Software Foundation; either version 3 of the License, or
 
9
#    (at your option) any later version.
 
10
#
 
11
#    pyzmq is distributed in the hope that it will be useful,
 
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#    Lesser GNU General Public License for more details.
 
15
#
 
16
#    You should have received a copy of the Lesser GNU General Public License
 
17
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
 
 
20
#-----------------------------------------------------------------------------
 
21
# Imports
 
22
#-----------------------------------------------------------------------------
 
23
 
 
24
import copy
 
25
from sys import getrefcount as grc
 
26
import time
 
27
from unittest import TestCase
 
28
 
 
29
import zmq
 
30
from zmq.tests import PollZMQTestCase
 
31
 
 
32
#-----------------------------------------------------------------------------
 
33
# Tests
 
34
#-----------------------------------------------------------------------------
 
35
 
 
36
class TestMessage(TestCase):
 
37
 
 
38
    def test_above_30(self):
 
39
        """Message above 30 bytes are never copied by 0MQ."""
 
40
        for i in range(5, 16):  # 32, 64,..., 65536
 
41
            s = (2**i)*'x'
 
42
            self.assertEquals(grc(s), 2)
 
43
            m = zmq.Message(s)
 
44
            self.assertEquals(grc(s), 4)
 
45
            del m
 
46
            self.assertEquals(grc(s), 2)
 
47
            del s
 
48
 
 
49
    def test_str(self):
 
50
        """Test the str representations of the Messages."""
 
51
        for i in range(16):
 
52
            s = (2**i)*'x'
 
53
            m = zmq.Message(s)
 
54
            self.assertEquals(s, str(s))
 
55
            self.assert_(s is str(s))
 
56
 
 
57
    def test_len(self):
 
58
        """Test the len of the Messages."""
 
59
        for i in range(16):
 
60
            s = (2**i)*'x'
 
61
            m = zmq.Message(s)
 
62
            self.assertEquals(len(s), len(s))
 
63
 
 
64
    def test_lifecycle1(self):
 
65
        """Run through a ref counting cycle with a copy."""
 
66
        for i in range(5, 16):  # 32, 64,..., 65536
 
67
            s = (2**i)*'x'
 
68
            self.assertEquals(grc(s), 2)
 
69
            m = zmq.Message(s)
 
70
            self.assertEquals(grc(s), 4)
 
71
            m2 = copy.copy(m)
 
72
            self.assertEquals(grc(s), 5)
 
73
            self.assertEquals(s, str(m))
 
74
            self.assertEquals(s, str(m2))
 
75
            self.assert_(s is str(m))
 
76
            self.assert_(s is str(m2))
 
77
            del m2
 
78
            self.assertEquals(grc(s), 4)
 
79
            del m
 
80
            self.assertEquals(grc(s), 2)
 
81
            del s
 
82
 
 
83
    def test_lifecycle2(self):
 
84
        """Run through a different ref counting cycle with a copy."""
 
85
        for i in range(5, 16):  # 32, 64,..., 65536
 
86
            s = (2**i)*'x'
 
87
            self.assertEquals(grc(s), 2)
 
88
            m = zmq.Message(s)
 
89
            self.assertEquals(grc(s), 4)
 
90
            m2 = copy.copy(m)
 
91
            self.assertEquals(grc(s), 5)
 
92
            self.assertEquals(s, str(m))
 
93
            self.assertEquals(s, str(m2))
 
94
            self.assert_(s is str(m))
 
95
            self.assert_(s is str(m2))
 
96
            del m
 
97
            self.assertEquals(grc(s), 4)
 
98
            del m2
 
99
            self.assertEquals(grc(s), 2)
 
100
            del s
 
101