2
# Copyright (c) 2010 Brian E. Granger
4
# This file is part of pyzmq.
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.
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.
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/>.
1
#-----------------------------------------------------------------------------
2
# Copyright (c) 2010-2012 Brian Granger, Min Ragan-Kelley
4
# This file is part of pyzmq
6
# Distributed under the terms of the New BSD License. The full license is in
7
# the file COPYING.BSD, distributed as part of this software.
8
#-----------------------------------------------------------------------------
20
10
#-----------------------------------------------------------------------------
22
12
#-----------------------------------------------------------------------------
25
16
from unittest import TestCase
19
from zmq.log import handlers
20
from zmq.utils.strtypes import b
28
21
from zmq.tests import BaseZMQTestCase
30
from zmq.log import handlers
33
23
#-----------------------------------------------------------------------------
35
25
#-----------------------------------------------------------------------------
60
50
def test_init_iface(self):
61
51
logger = self.logger
63
handler = handlers.PUBHandler(self.iface, ctx)
64
self.assertTrue(handler.ctx is ctx)
65
logger.removeHandler(handler)
66
handler.socket.close()
53
handler = handlers.PUBHandler(self.iface)
54
self.assertFalse(handler.ctx is ctx)
55
self.sockets.append(handler.socket)
68
57
handler = handlers.PUBHandler(self.iface, self.context)
58
self.sockets.append(handler.socket)
69
59
self.assertTrue(handler.ctx is ctx)
71
60
handler.setLevel(logging.DEBUG)
72
61
handler.root_topic = self.topic
73
62
logger.addHandler(handler)
75
63
sub = ctx.socket(zmq.SUB)
64
self.sockets.append(sub)
65
sub.setsockopt(zmq.SUBSCRIBE, self.topic)
76
66
sub.connect(self.iface)
77
sub.setsockopt(zmq.SUBSCRIBE, self.topic)
78
import time; time.sleep(0.1)
67
import time; time.sleep(0.25)
82
71
(topic, msg2) = sub.recv_multipart()
83
self.assertEquals(topic, 'zmq.INFO'.encode())
84
self.assertEquals(msg2, (msg1+'\n').encode())
72
self.assertEquals(topic, b'zmq.INFO')
73
self.assertEquals(msg2, b(msg1)+b'\n')
85
74
logger.removeHandler(handler)
86
handler.socket.close()
88
76
def test_init_socket(self):
89
77
pub,sub = self.create_bound_pair(zmq.PUB, zmq.SUB)
96
84
self.assertTrue(handler.socket is pub)
97
85
self.assertTrue(handler.ctx is pub.context)
98
86
self.assertTrue(handler.ctx is self.context)
99
# handler.socket.close()
100
87
sub.setsockopt(zmq.SUBSCRIBE, self.topic)
101
88
import time; time.sleep(0.1)
105
92
(topic, msg2) = sub.recv_multipart()
106
self.assertEquals(topic, 'zmq.INFO'.encode())
107
self.assertEquals(msg2, (msg1+'\n').encode())
93
self.assertEquals(topic, b'zmq.INFO')
94
self.assertEquals(msg2, b(msg1)+b'\n')
108
95
logger.removeHandler(handler)
109
handler.socket.close()
111
97
def test_root_topic(self):
112
98
logger, handler, sub = self.connect_handler()
113
99
handler.socket.bind(self.iface)
114
100
sub2 = sub.context.socket(zmq.SUB)
101
self.sockets.append(sub2)
115
102
sub2.connect(self.iface)
116
sub2.setsockopt(zmq.SUBSCRIBE, ''.encode())
117
handler.root_topic = 'twoonly'.encode()
103
sub2.setsockopt(zmq.SUBSCRIBE, b'')
104
handler.root_topic = b'twoonly'
119
106
logger.info(msg1)
120
107
self.assertRaisesErrno(zmq.EAGAIN, sub.recv, zmq.NOBLOCK)
121
108
topic,msg2 = sub2.recv_multipart()
122
self.assertEquals(topic, 'twoonly.INFO'.encode())
123
self.assertEquals(msg2, (msg1+'\n').encode())
109
self.assertEquals(topic, b'twoonly.INFO')
110
self.assertEquals(msg2, b(msg1)+b'\n')