def test_change_batch_quantity_leading_to_reallocation(): orderid, sku = random_ref('o'), random_ref('s') batch1, batch2 = random_ref('b1'), random_ref('b2') post_to_add_batch(batch1, sku, 10, '2011-01-02') post_to_add_batch(batch2, sku, 10, '2011-01-03') post_to_allocate(orderid, sku, 10, expected_batch=batch1) r = redis.Redis(**config.get_redis_host_and_port()) pubsub = subscribe_to_allocated_events(r) print('sending change batch quantity for', batch1) r.publish('change_batch_quantity', json.dumps({ 'batchref': batch1, 'sku': sku, 'qty': 5 })) print('waiting for reallocation event') messages = [] def check_messages(): messages.append(wait_for(pubsub.get_message)) print(messages) data = json.loads(messages[-1]['data']) assert data['orderid'] == orderid assert data['batchref'] == batch2 wait_for_assertion(check_messages)
def wait_for_redis_to_come_up(): deadline = time.time() + 5 r = redis.Redis(**config.get_redis_host_and_port()) while time.time() < deadline: try: return r.ping() except RedisError: time.sleep(0.5) pytest.fail('Redis never came up')
def wait_for_redis_to_come_up(): r = redis.Redis(**config.get_redis_host_and_port()) return r.ping()
import json import logging import redis from allocation import bootstrap, config from allocation.domain import commands logger = logging.getLogger(__name__) r = redis.Redis(**config.get_redis_host_and_port()) <<<<<<< HEAD def main(): logger.info('Redis pubsub starting') bus = bootstrap.bootstrap() pubsub = r.pubsub(ignore_subscribe_messages=True) pubsub.subscribe('change_batch_quantity') ======= def main(): logger.info("Redis pubsub starting") bus = bootstrap.bootstrap() pubsub = r.pubsub(ignore_subscribe_messages=True) pubsub.subscribe("change_batch_quantity") >>>>>>> upstream/master for m in pubsub.listen(): handle_change_batch_quantity(m, bus)
def cleanup_redis(): r = redis.Redis(**config.get_redis_host_and_port()) yield for k in r.keys(): logging.info('cleaning up redis key', k) r.delete(k)
import json import logging from allocation import bootstrap, config from allocation.domain import commands from redis import Redis logger = logging.getLogger(__name__) redis_client = Redis(**config.get_redis_host_and_port()) def main(): messagebus = bootstrap.bootstrap() pubsub = redis_client.pubsub(ignore_subscribe_messages=True) pubsub.subscribe("change_batch_quantity") for message in pubsub.listen(): logger.debug(f"Received message: {message}") handle_change_batch_quantity(message, messagebus) def handle_change_batch_quantity(message, messagebus): data = json.loads(message["data"]) command = commands.ChangeBatchQuantity(data["batchref"], data["qty"]) messagebus.handle(message=command) if __name__ == "__main__": main()