def test_send_recv_multiple_endpoints(self): r = ZmqTestSubConnection( self.factory, ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5556")) r.addEndpoints( [ZmqEndpoint(ZmqEndpointType.bind, "inproc://endpoint")]) s1 = ZmqPubConnection( self.factory, ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5556")) s2 = ZmqPubConnection( self.factory, ZmqEndpoint(ZmqEndpointType.connect, "inproc://endpoint")) r.subscribe('') def publish(ignore): s1.publish('111', 'tag1') s2.publish('222', 'tag2') def check(ignore): result = getattr(r, 'messages', []) expected = [['tag1', '111'], ['tag2', '222']] self.failUnlessEqual(sorted(result), expected, "Message should have been received") return _wait(0.1).addCallback(publish) \ .addCallback(lambda _: _wait(0.1)).addCallback(check)
def test_send_recv_pgm(self): r = ZmqTestSubConnection( self.factory, ZmqEndpoint(ZmqEndpointType.bind, "epgm://127.0.0.1;239.192.1.1:5556")) s = ZmqPubConnection( self.factory, ZmqEndpoint(ZmqEndpointType.connect, "epgm://127.0.0.1;239.192.1.1:5556")) r.subscribe('tag') def publish(ignore): s.publish('xyz', 'different-tag') s.publish('abcd', 'tag1') def check(ignore): result = getattr(r, 'messages', []) expected = [['tag1', 'abcd']] self.failUnlessEqual(result, expected, "Message should have been received") return _wait(0.2).addCallback(publish) \ .addCallback(lambda _: _wait(0.2)).addCallback(check)
def test_send_recv_pgm(self): r = ZmqTestSubConnection(self.factory, ZmqEndpoint( ZmqEndpointType.bind, "epgm://127.0.0.1;239.192.1.1:5556")) s = ZmqPubConnection(self.factory, ZmqEndpoint( ZmqEndpointType.connect, "epgm://127.0.0.1;239.192.1.1:5556")) r.subscribe('tag') s.publish('xyz', 'different-tag') s.publish('abcd', 'tag1') def check(ignore): result = getattr(r, 'messages', []) expected = [['tag1', 'abcd']] self.failUnlessEqual( result, expected, "Message should have been received") return _wait(0.2).addCallback(check)
def test_publish_fail(self): def fakeSend(factory): raise Exception("ohnoz!") def checkPublish(error): self.assertEqual(str(error), "exceptions.Exception: ohnoz!") def checkListen(server): self.assertEqual(self.factory.testMessage, "") failure = server.publish("a really special message") d = self.assertFailure(failure, exceptions.PublishingError) d.addCallback(checkPublish) s = ZmqPubConnection(ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1")) self.patch(s, 'send', fakeSend) d = s.listen(self.factory) d.addCallback(checkListen) return d
def test_publish_success(self): def fakeSend(message): self.factory.testMessage = message def checkPublish(server): expected = "\x00a really special message" self.assertEqual(self.factory.testMessage, expected) def checkListen(server): self.assertEqual(self.factory.testMessage, "") d = server.publish("a really special message") d.addCallback(checkPublish) s = ZmqPubConnection(ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1")) self.patch(s, 'send', fakeSend) d = s.listen(self.factory) d.addCallback(checkListen) return d
def test_send_recv(self): r = ZmqTestSubConnection( self.factory, ZmqEndpoint(ZmqEndpointType.bind, "ipc://test-sock")) s = ZmqPubConnection( self.factory, ZmqEndpoint(ZmqEndpointType.connect, "ipc://test-sock")) r.subscribe('tag') s.publish('xyz', 'different-tag') s.publish('abcd', 'tag1') s.publish('efgh', 'tag2') def check(ignore): result = getattr(r, 'messages', []) expected = [['tag1', 'abcd'], ['tag2', 'efgh']] self.failUnlessEqual( result, expected, "Message should have been received") return _wait(0.01).addCallback(check)
def test_publish_fail(self): def fakeSend(factory): raise Exception("ohnoz!") def checkPublish(error): self.assertEqual(str(error), "exceptions.Exception: ohnoz!") def checkListen(server): self.assertEqual(self.factory.testMessage, "") failure = server.publish("a really special message") d = self.assertFailure(failure, exceptions.PublishingError) d.addCallback(checkPublish) s = ZmqPubConnection( ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1")) self.patch(s, 'send', fakeSend) d = s.listen(self.factory) d.addCallback(checkListen) return d
def test_publish_success(self): def fakeSend(message): self.factory.testMessage = message def checkPublish(server): expected = "\x00a really special message" self.assertEqual(self.factory.testMessage, expected) def checkListen(server): self.assertEqual(self.factory.testMessage, "") d = server.publish("a really special message") d.addCallback(checkPublish) s = ZmqPubConnection( ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1")) self.patch(s, 'send', fakeSend) d = s.listen(self.factory) d.addCallback(checkListen) return d
def setup(self): endpoint = "tcp://%s:%s" % (self.host, self.port) zf = ZmqFactory() ze = ZmqEndpoint(ZmqEndpointType.bind, endpoint) self.zcon = ZmqPubConnection(zf, ze) for ev in self.relay_events.split(','): ty = events.get_by_name(ev.strip()) if ty: self.register(self.on_event, ty) else: self.console("zeromq: couldn't bind to event: {0}".format(ev))
class Zeromq(Plugin): host = Plugin.Property(default="localhost") port = Plugin.Property(default=5000) channel = Plugin.Property(default="mark2-{server}") relay_events = Plugin.Property(default="StatPlayers,PlayerJoin,PlayerQuit,PlayerChat,PlayerDeath") def setup(self): endpoint = "tcp://%s:%s" % (self.host, self.port) zf = ZmqFactory() ze = ZmqEndpoint(ZmqEndpointType.bind, endpoint) self.zcon = ZmqPubConnection(zf, ze) for ev in self.relay_events.split(','): ty = events.get_by_name(ev.strip()) if ty: self.register(self.on_event, ty) else: self.console("zeromq: couldn't bind to event: {0}".format(ev)) def on_event(self, event): self.console("zeromq: " + self.channel) self.zcon.publish(json.dumps(event.serialize()), self.channel)
def test_send_recv_multiple_endpoints(self): r = ZmqTestSubConnection( ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5556"), ZmqEndpoint(ZmqEndpointType.bind, "inproc://endpoint")) r.listen(self.factory) s1 = ZmqPubConnection( ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5556")) s1.connect(self.factory) s2 = ZmqPubConnection( ZmqEndpoint(ZmqEndpointType.connect, "inproc://endpoint")) s2.connect(self.factory) r.subscribe('') s1.publish('111', 'tag1') s2.publish('222', 'tag2') def check(ignore): result = getattr(r, 'messages', []) expected = [['tag2', '222'], ['tag1', '111']] self.failUnlessEqual( result, expected, "Message should have been received") return _wait(0.2).addCallback(check)
def test_send_recv_pgm(self): try: r = ZmqTestSubConnection(self.factory, ZmqEndpoint( ZmqEndpointType.bind, "epgm://127.0.0.1;239.192.1.1:5556")) s = ZmqPubConnection(self.factory, ZmqEndpoint( ZmqEndpointType.connect, "epgm://127.0.0.1;239.192.1.1:5556")) except ZMQError as e: if e.strerror == "Protocol not supported": print >> sys.stderr, "EPGM not supported" return else: raise r.subscribe('tag') s.publish('xyz', 'different-tag') s.publish('abcd', 'tag1') def check(ignore): result = getattr(r, 'messages', []) expected = [['tag1', 'abcd']] self.failUnlessEqual( result, expected, "Message should have been received") return _wait(0.2).addCallback(check)
def test_send_recv(self): r = ZmqTestSubConnection( ZmqEndpoint(ZmqEndpointType.bind, "ipc://test-sock")) r.listen(self.factory) s = ZmqPubConnection( ZmqEndpoint(ZmqEndpointType.connect, "ipc://test-sock")) s.connect(self.factory) r.subscribe('tag') s.publish('xyz', 'different-tag') s.publish('abcd', 'tag1') s.publish('efgh', 'tag2') def check(ignore): result = getattr(r, 'messages', []) expected = [['tag1', 'abcd'], ['tag2', 'efgh']] self.failUnlessEqual(result, expected, "Message should have been received") return _wait(0.01).addCallback(check)