def test_sync_receive_messages(): c = sync.SyncConn() c.connect("127.0.0.1", 4150) c.send(protocol.identify({'short_id': 'test', 'long_id': 'test.example'})) c.send(protocol.subscribe('test', 'ch')) mock_response_write(c, protocol.FRAME_TYPE_RESPONSE, b'OK') mock_response_write(c, protocol.FRAME_TYPE_RESPONSE, b'OK') resp = c.read_response() unpacked = protocol.unpack_response(resp) assert unpacked[0] == protocol.FRAME_TYPE_RESPONSE assert unpacked[1] == b'OK' resp = c.read_response() unpacked = protocol.unpack_response(resp) assert unpacked[0] == protocol.FRAME_TYPE_RESPONSE assert unpacked[1] == b'OK' for i in range(10): c.send(protocol.ready(1)) body = b'{"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 = protocol.unpack_response(resp) assert unpacked[0] == protocol.FRAME_TYPE_MESSAGE msg = protocol.decode_message(unpacked[1]) assert msg.timestamp == ts assert msg.id == b"%016d" % i assert msg.attempts == 0 assert msg.body == body
def _on_connect(self, **kwargs): identify_data = { 'short_id': self. short_hostname, # TODO remove when deprecating pre 1.0 support 'long_id': self.hostname, # TODO remove when deprecating pre 1.0 support 'client_id': self.short_hostname, 'hostname': 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 } if self.msg_timeout: identify_data['msg_timeout'] = self.msg_timeout self.trigger(event.IDENTIFY, conn=self, data=identify_data) self.on(event.RESPONSE, self._on_identify_response) try: self.send(protocol.identify(identify_data)) except Exception as e: self.close() self.trigger( event.ERROR, conn=self, error=protocol.SendError('failed to bootstrap connection', e), )
def test_sync_authenticate_subscribe(): c = sync.SyncConn() c.connect("127.0.0.1", 4150) c.send( protocol.identify({ 'short_id': 'test', 'long_id': 'test.example', 'client_id': 'test', 'hostname': 'test.example' })) c.send(protocol.subscribe('test', 'ch')) mock_response_write(c, protocol.FRAME_TYPE_RESPONSE, b'OK') mock_response_write(c, protocol.FRAME_TYPE_RESPONSE, b'OK') resp = c.read_response() unpacked = protocol.unpack_response(resp) assert unpacked[0] == protocol.FRAME_TYPE_RESPONSE assert unpacked[1] == b'OK' resp = c.read_response() unpacked = protocol.unpack_response(resp) assert unpacked[0] == protocol.FRAME_TYPE_RESPONSE assert unpacked[1] == b'OK'