Example #1
0
def test_key_partitioner_with_bytes_uuid_key():
    key_partitioner = KeyPartitioner()
    for i in range(0, 100):
        test_uuid = uuid.uuid4().hex
        assert key_partitioner(bytes(test_uuid, 'utf-8'), [0, 1, 2, 3],
                               [0, 1, 2, 3]) == get_good_partition(
                                   bytes(test_uuid, 'utf-8'), [0, 1, 2, 3])
Example #2
0
def test_key_partitioner_with_str_uuid_key():
    key_partitioner = KeyPartitioner()
    for i in range(0, 100):
        test_uuid = uuid.uuid4().hex
        assert key_partitioner(test_uuid, [0, 1, 2, 3],
                               [0, 1, 2, 3]) == get_good_partition(
                                   test_uuid, [0, 1, 2, 3])
Example #3
0
async def coffee_new_order(request: Request) -> HTTPResponse:
    if request.json:
        # Creates coffee
        key_partitioner = KeyPartitioner()
        try:
            while True:
                coffee = Coffee(**request.json)
                if key_partitioner(
                        coffee.uuid, [0, 1],
                    [0, 1]) == request['waiter']['kafka_client'].cur_instance:
                    break
        except (KeyError, ValueError) as err:
            return response.json({'error': err, 'error_code': 500}, status=500)

        # Creates CoffeeOrdered event
        coffee_order = CoffeeOrdered(partition_key=coffee.uuid,
                                     **coffee.__to_dict__())

        # Send & await CoffeeOrdered
        event_record_metadata = await request['waiter'][
            'producer'].send_and_wait(coffee_order, 'waiter-events')

        # Save coffee in own local store
        store_record_metedata = await request['waiter']['store_builder'].\
            set_from_local_store(coffee.uuid, coffee.__to_bytes_dict__())

        return response.json(
            {
                'result': 'Coffee ordered',
                'ticket': coffee.uuid,
                'result_code': 200
            },
            status=200)
    return response.json({
        'error': 'missing json',
        'error_code': 500
    },
                         status=500)
Example #4
0
    async def handle(self, event: CoffeeOrdered) -> None:

        key_partitioner = KeyPartitioner()
        while True:
            bill = Bill(coffee_uuid=event.uuid, amount=event.amount)
            if key_partitioner(
                    bill.uuid, [0, 1],
                [0, 1]) == self._store_builder.get_current_instance():
                break

        # Sets on cash register local store
        await self._store_builder.set_from_local_store(
            bill.uuid, bill.__to_bytes_dict__())

        # Creates BillCreated event
        bill_created = BillCreated(partition_key=bill.uuid,
                                   uuid=bill.uuid,
                                   coffee_uuid=bill.coffee_uuid,
                                   amount=bill.amount,
                                   context=bill.context)

        # Sends BillCreated event
        await self._transactional_producer.send_and_wait(
            bill_created, 'cash-register-events')
Example #5
0
def test_key_partitioner_missing_key():
    key_partitioner = KeyPartitioner()
    r = key_partitioner(None, [0, 1, 2, 3], [0, 1, 2, 3])
    assert r in range(0, 4)
Example #6
0
def test_key_partitioner_bad_format():
    key_partitioner = KeyPartitioner()
    with pytest.raises(BadKeyType):
        key_partitioner(['rofl'], [0, 1, 2, 3], [0, 1, 2, 3])
Example #7
0
    # Creates & registers event loop
    bartender_app['loop'] = uvloop.new_event_loop()
    asyncio.set_event_loop(bartender_app['loop'])

    bartender_app['serializer'] = AvroSerializer(
        os.path.join(os.path.dirname(os.path.abspath(__file__)),
                     'examples/coffee_bar/avro_schemas'))

    # Creates & register KafkaProducer
    bartender_app['transactional_producer'] = KafkaProducer(
        name=f'bartender-{cur_instance}',
        bootstrap_servers='localhost:9092',
        client_id=f'bartender-{cur_instance}',
        serializer=bartender_app['serializer'],
        loop=bartender_app['loop'],
        partitioner=KeyPartitioner(),
        acks='all',
        transactional_id=f'bartender')

    # Initializes bartender handlers
    coffee_finished_handler = CoffeeFinishedHandler()
    bill_created_handler = BillCreatedHandler(
        bartender_app['transactional_producer'])
    bill_paid_handler = BillPaidHandler()
    make_coffee_handler = MakeCoffeeHandler()
    make_coffee_result_handler = MakeCoffeeResultHandler(
        bartender_app['transactional_producer'])

    # Registers events / handlers in serializer
    bartender_app['serializer'].register_class(
        'tonga.coffeemaker.command.MakeCoffee', MakeCoffee,