Пример #1
0
def test_image_available_unavailable_callbacks(aeron_directory):
    class Handler(object):
        def __init__(self):
            self.available_images = list()
            self.unavailable_images = list()

        def on_image_available(self, *args):
            self.available_images.append(args)

        def on_image_unavailable(self, *args):
            self.unavailable_images.append(args)

    handler = Handler()
    context = Context(aeron_dir=aeron_directory,
                      available_image_handler=handler.on_image_available,
                      unavailable_image_handler=handler.on_image_unavailable)
    subscription = context.add_subscription('aeron:ipc', 6000)

    publication_context = Context(aeron_dir=aeron_directory)
    publication = publication_context.add_publication('aeron:ipc', 6000)
    sleep(1)

    publication.close()
    del publication
    sleep(1)

    assert_that(handler.available_images, is_not(empty()))
    assert_that(handler.unavailable_images, is_not(empty()))
Пример #2
0
def test_offer__multicast(aeron_directory, mcast_subscriber, stream_id):
    context = Context(aeron_dir=aeron_directory)
    publication = context.add_publication('aeron:udp?endpoint=224.0.1.1:40456|ttl=0', stream_id)
    sleep(0.5)

    result = publication.offer(b'abc')
    assert_that(result, is_(greater_than_or_equal_to(0)))
Пример #3
0
def test_offer__ipc__no_subscribers(aeron_directory):
    context = Context(aeron_dir=aeron_directory)
    publication = context.add_publication('aeron:ipc', 50)
    assert_that(publication.is_connected, is_(False))

    result = publication.offer(b'abc')
    assert_that(result, is_(equal_to(NOT_CONNECTED)))
Пример #4
0
def test_offer__ipc__closed(aeron_directory, ipc_subscriber, stream_id):
    context = Context(aeron_dir=aeron_directory)
    publication = context.add_publication('aeron:ipc', stream_id)
    publication.close()
    assert_that(publication.is_closed, is_(True))

    result = publication.offer(b'abc')
    assert_that(result, is_(equal_to(PUBLICATION_CLOSED)))
Пример #5
0
def main():
    try:
        parser = ArgumentParser()
        parser.add_argument('-p', '--prefix', default=None)
        parser.add_argument('-c',
                            '--channel',
                            default='aeron:udp?endpoint=localhost:40123')
        parser.add_argument('-s', '--stream_id', type=int, default=1)
        parser.add_argument('-m', '--messages', type=int, default=sys.maxsize)
        parser.add_argument('-l', '--linger', type=int, default=60 * 60 * 1000)

        args = parser.parse_args()
        context = Context(
            aeron_dir=args.prefix,
            resource_linger_timeout=timedelta(milliseconds=args.linger),
            new_publication_handler=lambda *args: print(
                f'new publication - {args}'))

        publication = context.add_publication(args.channel, args.stream_id)
        for i in range(args.messages):
            result = publication.offer(f'Hallo World! {i}')
            if result == BACK_PRESSURED:
                print('Offer failed due to back pressure')
            elif result == NOT_CONNECTED:
                print(
                    'Offer failed because publisher is not connected to subscriber'
                )
            elif result == ADMIN_ACTION:
                print(
                    'Offer failed because of an administration action in the system'
                )
            elif result == PUBLICATION_CLOSED:
                print('Offer failed publication is closed')
            else:
                print('yay!')

            sleep(1)

        return 0

    except ArgumentError as e:
        print(e, file=sys.stderr)
        return -1
    except Exception as e:
        print(e, file=sys.stderr)
        return -2
Пример #6
0
def test_add_publication__with_handler(aeron_directory):
    class Handler(object):
        def __init__(self):
            self.publications = list()

        def on_new_publication(self, *args):
            self.publications.append(args)

    handler = Handler()
    context = Context(aeron_dir=aeron_directory,
                      new_publication_handler=handler.on_new_publication)
    publication = context.add_publication('aeron:ipc', 3000)

    assert_that(handler.publications, is_not(empty()))
    assert_that(handler.publications, has_length(equal_to(1)))
    assert_that(handler.publications[0][0], is_(equal_to('aeron:ipc')))
    assert_that(handler.publications[0][1], is_(equal_to(3000)))
Пример #7
0
def test_offer__multiple_publishers__same_stream(aeron_directory, ipc_subscriber, stream_id):
    context = Context(aeron_dir=aeron_directory)
    publication_1 = context.add_publication('aeron:ipc', stream_id)
    publication_2 = context.add_publication('aeron:ipc', stream_id)
    assert_that(publication_1.session_id, is_(equal_to(publication_2.session_id)))

    result = publication_1.offer(b'abc')
    assert_that(result, is_(greater_than_or_equal_to(0)))

    result = publication_2.offer(b'def')
    assert_that(result, is_(greater_than_or_equal_to(0)))

    sleep(0.5)

    messages = list()
    ipc_subscriber.poll(lambda data: messages.append(data))

    assert_that(messages, has_length(equal_to(2)))
    assert_that(bytes(messages[0]), is_(equal_to(b'abc')))
    assert_that(bytes(messages[1]), is_(equal_to(b'def')))
Пример #8
0
def publisher(streamID, messages, args):

    context = Context(
        aeron_dir=args.prefix,
        resource_linger_timeout=timedelta(milliseconds=args.linger),
        # new_publication_handler=lambda *args: print(f'new publication - {args}'),
        # new_subscription_handler=lambda *args: print(f'new subscription {args}'),
        # available_image_handler=lambda *args: print(f'available image {args}'),
        # unavailable_image_handler = lambda *args: print(f'unavailable image {args}')
    )


    publication = context.add_publication(args.channel, streamID)
    for msg in messages:

        # sleep(0.001)
        result = publication.offer(msg)

        # if result == BACK_PRESSURED:
        #     print('Offer failed due to back pressure')
        #
        # elif result == NOT_CONNECTED:
        #     print('Offer failed because publisher is not connected to subscriber')
        #
        # elif result == ADMIN_ACTION:
        #     print('Offer failed because of an administration action in the system')
        #
        # elif result == PUBLICATION_CLOSED:
        #     print('Offer failed publication is closed')

        # else:
        #     print('yay!')


    # print("done.")
    return 0
Пример #9
0
def test_offer__ipc(aeron_directory, ipc_subscriber, stream_id):
    context = Context(aeron_dir=aeron_directory)
    publication = context.add_publication('aeron:ipc', stream_id)
    result = publication.offer(b'abc')
    assert_that(result, is_(greater_than_or_equal_to(0)))
Пример #10
0
def test_offer__ipc__message_too_large(aeron_directory, ipc_subscriber, stream_id):
    context = Context(aeron_dir=aeron_directory)
    publication = context.add_publication('aeron:ipc', stream_id)

    blob = bytearray(50 * 1024 * 1024)
    assert_that(calling(publication.offer).with_args(blob), raises(RuntimeError))
Пример #11
0
def test_str_(aeron_directory):
    context = Context(aeron_dir=aeron_directory)
    publication = context.add_publication('aeron:ipc', 9845)

    assert_that(str(publication), contains_string('aeron:ipc'))
    assert_that(str(publication), contains_string('9845'))
Пример #12
0
def test_add_publication(aeron_directory):
    context = Context(aeron_dir=aeron_directory)
    publication = context.add_publication('aeron:ipc', 2000)
    assert_that(publication, is_not(None))
    assert_that(publication.channel, is_('aeron:ipc'))
    assert_that(publication.stream_id, is_(2000))