def test_sync_receive_messages(): c = nsq.SyncConn() c.connect("127.0.0.1", 4150) c.send(nsq.identify({'short_id': 'test', 'long_id': 'test.example'})) c.send(nsq.subscribe('test', 'ch')) mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK') mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK') resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE assert unpacked[1] == six.b('OK') resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE assert unpacked[1] == six.b('OK') for i in range(10): c.send(nsq.ready(1)) body = '{"data": {"test_key": %d}}' % i body = body.encode('utf-8') ts = int(time.time() * 1000 * 1000) mock_response_write_message(c, ts, 0, i, body) resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_MESSAGE msg = nsq.decode_message(unpacked[1]) assert msg.timestamp == ts assert msg.id == ("%016d" % i).encode('utf-8') assert msg.attempts == 0 assert msg.body == body
def _on_connect(self, **kwargs): identify_data = { 'short_id': self.short_hostname, 'long_id': self.hostname, 'heartbeat_interval': self.heartbeat_interval, 'feature_negotiation': True, 'tls_v1': self.tls_v1, 'snappy': self.snappy, 'deflate': self.deflate, 'deflate_level': self.deflate_level, 'output_buffer_timeout': self.output_buffer_timeout, 'output_buffer_size': self.output_buffer_size, 'sample_rate': self.sample_rate, 'user_agent': self.user_agent } self.trigger('identify', conn=self, data=identify_data) self.on('response', self._on_identify_response) try: self.send(nsq.identify(identify_data)) except Exception, e: self.close() self.trigger('error', conn=self, error=nsq.SendError('failed to bootstrap connection', e))
def test_sync_receive_messages(): c = nsq.SyncConn() c.connect("127.0.0.1", 4150) c.send(nsq.identify({'short_id': 'test', 'long_id': 'test.example'})) c.send(nsq.subscribe('test', 'ch')) mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK') mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK') resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE assert unpacked[1] == 'OK' resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE assert unpacked[1] == 'OK' for i in range(10): c.send(nsq.ready(1)) body = '{"data": {"test_key": %d}}' % i ts = int(time.time() * 1000 * 1000) mock_response_write_message(c, ts, 0, i, body) resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_MESSAGE msg = nsq.decode_message(unpacked[1]) assert msg.timestamp == ts assert msg.id == "%016d" % i assert msg.attempts == 0 assert msg.body == body
def test_sync_authenticate_subscribe(): c = nsq.SyncConn() c.connect("127.0.0.1", 4150) c.send(nsq.identify({'short_id': 'test', 'long_id': 'test.example'})) c.send(nsq.subscribe('test', 'ch')) mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK') mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK') resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE assert unpacked[1] == six.b('OK') resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE assert unpacked[1] == six.b('OK')
def test_sync_authenticate_subscribe(): c = nsq.SyncConn() c.connect("127.0.0.1", 4150) c.send(nsq.identify({'short_id': 'test', 'long_id': 'test.example'})) c.send(nsq.subscribe('test', 'ch')) mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK') mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK') resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE assert unpacked[1] == 'OK' resp = c.read_response() unpacked = nsq.unpack_response(resp) assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE assert unpacked[1] == 'OK'
def _on_connect(self, **kwargs): identify_data = { 'short_id': self.short_hostname, 'long_id': self.hostname, 'heartbeat_interval': self.heartbeat_interval, 'feature_negotiation': True, 'tls_v1': self.tls_v1, 'snappy': self.snappy, 'output_buffer_timeout': self.output_buffer_timeout, 'output_buffer_size': self.output_buffer_size, 'sample_rate': self.sample_rate, 'user_agent': self.user_agent } self.trigger('identify', conn=self, data=identify_data) self.on('response', self._on_identify_response) try: self.send(nsq.identify(identify_data)) except Exception, e: self.close() self.trigger('error', conn=self, error=nsq.SendError('failed to bootstrap connection', e))
def _on_connect(self, **kwargs): identify_data = { "short_id": self.short_hostname, "long_id": self.hostname, "heartbeat_interval": self.heartbeat_interval, "feature_negotiation": True, "tls_v1": self.tls_v1, "snappy": self.snappy, "deflate": self.deflate, "deflate_level": self.deflate_level, "output_buffer_timeout": self.output_buffer_timeout, "output_buffer_size": self.output_buffer_size, "sample_rate": self.sample_rate, "user_agent": self.user_agent, } self.trigger("identify", conn=self, data=identify_data) self.on("response", self._on_identify_response) try: self.send(nsq.identify(identify_data)) except Exception, e: self.close() self.trigger("error", conn=self, error=nsq.SendError("failed to bootstrap connection", e))