def test_consumer_create(self): self.mock_call.result.return_value = self.output_hosts client = Client(self.mock_tchannel, self.logger, timeout_seconds=1) consumer = client.create_consumer(self.test_path, self.test_cg) self.assertEquals(0, len(consumer.consumer_threads)) self.assertEquals(0, len(consumer.ack_threads)) self.assertIsNone(consumer.reconfigure_thread) consumer._do_not_start_consumer_thread() consumer.open() consumer.close() args, kwargs = self.mock_tchannel.thrift.call_args self.assertEquals('BFrontend::readConsumerGroupHosts', args[0].endpoint) self.assertEquals(self.test_path, args[0].call_args.getHostsRequest.destinationPath) self.assertEquals(self.test_cg, args[0].call_args.getHostsRequest.consumerGroupName) self.assertEquals(10, len(consumer.consumer_threads)) self.assertEquals(consumer.ack_threads_count, len(consumer.ack_threads)) self.assertTrue(consumer.reconfigure_thread.is_alive())
def test_publisher_publish_async_invalid_callback(self): self.mock_call.result.return_value = self.publisher_options client = Client(self.mock_tchannel, self.logger) publisher = client.create_publisher(self.test_path) publisher.open() # nothing happens if no callback provided (no exception) publisher.publish_async(self.test_msg_id, self.test_msg, None) publisher.close()
def test_publisher_publish_exception(self): self.mock_call.result.return_value = self.publisher_options client = Client(self.mock_tchannel, self.logger) publisher = client.create_publisher(self.test_path) publisher.open() self.mock_call.result.side_effect = Exception(self.test_err_msg) ack = publisher.publish(self.test_msg_id, self.test_msg) publisher.close() self.assertEquals(self.test_msg_id, ack.id) self.assertEquals(cherami.Status.FAILED, ack.status) self.assertTrue(self.test_err_msg in ack.message)
def test_publisher_publish_failed(self): self.mock_call.result.return_value = self.publisher_options client = Client(self.mock_tchannel, self.logger) publisher = client.create_publisher(self.test_path) publisher.open() self.mock_call.result.return_value = self.send_ack_failed ack = publisher.publish(self.test_msg_id, self.test_msg) publisher.close() self.assertEquals(self.test_msg_id, ack.id) self.assertEquals(cherami.Status.FAILED, ack.status) self.assertEquals(self.test_err_msg, ack.message)
def test_publisher_create(self): self.mock_call.result.return_value = self.publisher_options client = Client(self.mock_tchannel, self.logger) publisher = client.create_publisher(self.test_path) self.assertEquals(0, len(publisher.workers)) self.assertIsNone(publisher.reconfigure_thread) publisher.open() publisher.close() args, kwargs = self.mock_tchannel.thrift.call_args self.assertEquals('BFrontend::readPublisherOptions', args[0].endpoint) self.assertEquals(self.test_path, args[0].call_args.getPublisherOptionsRequest.path) self.assertEquals(10, len(publisher.workers)) self.assertTrue(publisher.reconfigure_thread.is_alive())
def test_publisher_publish_timeout(self): self.mock_call.result.return_value = self.publisher_options client = Client(self.mock_tchannel, self.logger, timeout_seconds=1) publisher = client.create_publisher(self.test_path) publisher.open() def side_effect(): time.sleep(3) return self.send_ack_success self.mock_call.result.side_effect = side_effect ack = publisher.publish(self.test_msg_id, self.test_msg) publisher.close() self.assertEquals(self.test_msg_id, ack.id) self.assertEquals(cherami.Status.TIMEDOUT, ack.status)
def test_consumer_ack_fail(self): self.mock_call.result.return_value = self.output_hosts client = Client(self.mock_tchannel, self.logger, timeout_seconds=1) consumer = client.create_consumer(self.test_path, self.test_cg) consumer._do_not_start_consumer_thread() consumer.open() self.mock_call.result.side_effect = Exception('exception') res = consumer.ack(self.test_delivery_token) consumer.close() args, kwargs = self.mock_tchannel.thrift.call_args self.assertEquals('BOut::ackMessages', args[0].endpoint) self.assertEquals(1, len(args[0].call_args.ackRequest.ackIds)) self.assertEquals(self.test_delivery_token[0], args[0].call_args.ackRequest.ackIds[0]) self.assertFalse(res)
def test_publisher_publish(self): self.mock_call.result.return_value = self.publisher_options client = Client(self.mock_tchannel, self.logger) publisher = client.create_publisher(self.test_path) publisher.open() self.mock_call.result.return_value = self.send_ack_success ack = publisher.publish(self.test_msg_id, self.test_msg) publisher.close() args, kwargs = self.mock_tchannel.thrift.call_args self.assertEquals('BIn::putMessageBatch', args[0].endpoint) self.assertEquals(self.test_path, args[0].call_args.request.destinationPath) self.assertEquals(1, len(args[0].call_args.request.messages)) self.assertEquals(self.test_msg_id, args[0].call_args.request.messages[0].id) self.assertEquals(self.test_msg, args[0].call_args.request.messages[0].data) self.assertEquals(self.test_msg_id, ack.id) self.assertEquals(cherami.Status.OK, ack.status) self.assertEquals(self.test_receipt, ack.receipt)
def test_consumer_no_message(self): self.mock_call.result.return_value = self.output_hosts client = Client(self.mock_tchannel, self.logger, timeout_seconds=1) consumer = client.create_consumer(self.test_path, self.test_cg) consumer._do_not_start_consumer_thread() consumer.open() self.mock_call.result.return_value = self.no_msg consumer.consumer_threads['0:0'].start() msgs = consumer.receive(1) consumer.close() args, kwargs = self.mock_tchannel.thrift.call_args self.assertEquals('BOut::receiveMessageBatch', args[0].endpoint) self.assertEquals(self.test_path, args[0].call_args.request.destinationPath) self.assertEquals(self.test_cg, args[0].call_args.request.consumerGroupName) self.assertEquals(0, len(msgs))
def test_publisher_publish_async(self): self.mock_call.result.return_value = self.publisher_options done_signal = threading.Event() acks = [] client = Client(self.mock_tchannel, self.logger) publisher = client.create_publisher(self.test_path) publisher.open() def callback(ack): acks.append(ack) done_signal.set() self.mock_call.result.return_value = self.send_ack_success publisher.publish_async(self.test_msg_id, self.test_msg, callback) done_signal.wait(5) publisher.close() self.assertEquals(1, len(acks)) self.assertEquals(self.test_msg_id, acks[0].id) self.assertEquals(cherami.Status.OK, acks[0].status) self.assertEquals(self.test_receipt, acks[0].receipt)
def test_consumer_exception(self): self.mock_call.result.return_value = self.output_hosts client = Client(self.mock_tchannel, self.logger, timeout_seconds=1) consumer = client.create_consumer(self.test_path, self.test_cg) consumer._do_not_start_consumer_thread() consumer.open() self.mock_call.result.side_effect = Exception('exception') consumer.consumer_threads['0:0'].start() msgs = consumer.receive(1) # verify the thread is still alive even though an excepton has been thrown by output host self.assertTrue(consumer.consumer_threads['0:0'].is_alive()) consumer.close() args, kwargs = self.mock_tchannel.thrift.call_args self.assertEquals('BOut::receiveMessageBatch', args[0].endpoint) self.assertEquals(self.test_path, args[0].call_args.request.destinationPath) self.assertEquals(self.test_cg, args[0].call_args.request.consumerGroupName) self.assertEquals(0, len(msgs))
def test_publisher_open_exception(self): self.mock_call.result.side_effect = Exception(self.test_err_msg) client = Client(self.mock_tchannel, self.logger) publisher = client.create_publisher(self.test_path) self.assertRaises(Exception, publisher.open) self.assertTrue(publisher.reconfigure_thread is None)
from __future__ import absolute_import, unicode_literals from kombu.log import get_logger from tchannel.sync import TChannel from cherami_client.client import Client from tornado import ioloop # run cherami-server locally tchannel = TChannel(name='test_service', known_peers=['127.0.0.1:4922']) logger = get_logger('kombu.transport.cherami') # cherami-client needs to make tchannel calls ioloop.IOLoop.current() client = Client(tchannel, logger, timeout_seconds=3, reconfigure_interval_seconds=10) print 'created client'
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ A client instance that can be used to create publisher or consumer instances. """ from __future__ import absolute_import, print_function import time import logging from tchannel.sync import TChannel as TChannelSyncClient from cherami_client.client import Client tchannel = TChannelSyncClient(name='example_service', known_peers=['127.0.0.1:4922']) logger = logging.getLogger('example_client') while True: try: client = Client(tchannel, logger) print('Success: Created cherami client.') break except Exception as e: logger.exception('Failed to create to cherami: %s', e) time.sleep(2)