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()))
def test_poll_eos__no_data(aeron_directory, ipc_publication, stream_id): context = Context(aeron_dir=aeron_directory) subscription = context.add_subscription('aeron:ipc', stream_id) result = subscription.poll_eos() assert_that(result, is_(0)) # def test_poll_eos__single_image(aeron_directory): # context = Context(aeron_dir=aeron_directory) # subscription = context.add_subscription('aeron:udp?endpoint=localhost:40456|ttl=0', 199) # publication = context.add_publication('aeron:udp?endpoint=localhost:40456|ttl=0', 199) # sleep(0.5) # # publication.offer(b'abc') # sleep(0.5) # # result = subscription.poll(lambda _: None) # assert_that(result, is_(1)) # # publication.close() # del publication # sleep(1) # # finished = list() # # result = subscription.poll_eos(lambda image: finished.append(image.session_id)) # assert_that(result, is_(1)) # def test_poll_eos__multiple_images(ipc_publication): # pass
def subscriber(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}') ) subscriptions = [] for id in range(0, args.processes): subscriptions.append(context.add_subscription(args.channel, id+1)) while True: for sub in subscriptions: # fragments_read = sub.poll(lambda data: print(bytes(data))) fragments_read = sub.poll(processData) if fragments_read == 0: # eos_count = sub.poll_eos(lambda *args: print(f'end of stream: {args}')) eos_count = sub.poll_eos(endOfStream) if eos_count > 0: return 0
def test_offer__multicast(aeron_directory, mcast_subscriber, stream_id): context = Context(aeron_dir=aeron_directory) publication = context.add_exclusive_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)))
def test_offer__ipc__no_subscribers(aeron_directory, stream_id): context = Context(aeron_dir=aeron_directory) publication = context.add_exclusive_publication('aeron:ipc', stream_id) assert_that(publication.is_connected, is_(False)) result = publication.offer(b'abc') assert_that(result, is_(equal_to(NOT_CONNECTED)))
def test_create__no_publisher(aeron_directory, stream_id): # always connected context = Context(aeron_dir=aeron_directory) subscription = context.add_subscription('aeron:ipc', stream_id) sleep(0.5) assert_that(subscription.is_connected, is_(True)) assert_that(subscription.images, is_(empty()))
def test_poll__not_connected(aeron_directory, stream_id): # always connected context = Context(aeron_dir=aeron_directory) subscription = context.add_subscription('aeron:ipc', stream_id) received = list() result = subscription.poll(lambda data: received.append(data)) assert_that(result, is_(0))
def test_offer__ipc__closed(aeron_directory, ipc_subscriber, stream_id): context = Context(aeron_dir=aeron_directory) publication = context.add_exclusive_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)))
def test_create__with_publisher(aeron_directory, ipc_publication, stream_id): # always connected context = Context(aeron_dir=aeron_directory) subscription = context.add_subscription('aeron:ipc', stream_id) sleep(0.5) assert_that(subscription.images, is_not(empty())) assert_that(subscription.images, has_length(1)) assert_that(subscription.images[0].session_id, ipc_publication.session_id)
def test_poll__no_data(aeron_directory, ipc_publication, stream_id): context = Context(aeron_dir=aeron_directory) subscription = context.add_subscription('aeron:ipc', stream_id) sleep(0.5) assert_that(subscription.images, is_not(empty())) assert_that(subscription.images, has_length(1)) assert_that(subscription.images[0].session_id, ipc_publication.session_id) received = list() result = subscription.poll(lambda data: received.append(data)) assert_that(result, is_(0))
def test_poll__multiple_sessions(aeron_directory, ipc_publication_1, ipc_publication_2, stream_id): context = Context(aeron_dir=aeron_directory) subscription = context.add_subscription('aeron:ipc', stream_id) sleep(0.5) assert_that(subscription.images, has_length(2)) ipc_publication_1.offer(b'abc') ipc_publication_2.offer(b'cde') received = list() result = subscription.poll(lambda data, header: received.append((header.session_id, bytes(data)))) assert_that(result, is_(equal_to(2))) assert_that(received, has_length(2))
def test_poll__multiple_fragments(aeron_directory, ipc_publication, stream_id): context = Context(aeron_dir=aeron_directory) subscription = context.add_subscription('aeron:ipc', stream_id) sleep(0.5) ipc_publication.offer(b'abc') ipc_publication.offer(b'cde') ipc_publication.offer(b'efg') received = list() result = subscription.poll(lambda data: received.append(bytes(data)), fragment_limit=3) assert_that(result, is_(equal_to(3))) assert_that(received, has_length(3)) assert_that(received[0], is_(equal_to(b'abc'))) assert_that(received[1], is_(equal_to(b'cde'))) assert_that(received[2], is_(equal_to(b'efg')))
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
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)))
def test_add_subscription__with_handlers(aeron_directory): class Handler: def __init__(self): self.subscriptions = list() def on_new_subscription(self, *args): self.subscriptions.append(args) handler = Handler() context = Context(aeron_dir=aeron_directory, new_subscription_handler=handler.on_new_subscription) subscription = context.add_subscription('aeron:ipc', 546) assert_that(handler.subscriptions, is_not(empty())) assert_that(handler.subscriptions, has_length(equal_to(1))) assert_that(handler.subscriptions[0][0], is_('aeron:ipc')) assert_that(handler.subscriptions[0][1], is_(546))
def test_offer__multiple_publishers__same_stream(aeron_directory, ipc_subscriber, stream_id): context = Context(aeron_dir=aeron_directory) publication_1 = context.add_exclusive_publication('aeron:ipc', stream_id) publication_2 = context.add_exclusive_publication('aeron:ipc', stream_id) assert_that(publication_1.session_id, is_not(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')))
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) args = parser.parse_args() context = Context(aeron_dir=args.prefix, 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}')) subscription = context.add_subscription(args.channel, args.stream_id) while True: fragments_read = subscription.poll(lambda data: print(bytes(data))) if fragments_read == 0: eos_count = subscription.poll_eos( lambda *args: print(f'end of stream: {args}')) if eos_count > 0: break sleep(0.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
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
def test_offer__ipc(aeron_directory, ipc_subscriber, stream_id): context = Context(aeron_dir=aeron_directory) publication = context.add_exclusive_publication('aeron:ipc', stream_id) result = publication.offer(b'abc') assert_that(result, is_(greater_than_or_equal_to(0)))
def test_offer__ipc__message_too_large(aeron_directory, ipc_subscriber, stream_id): context = Context(aeron_dir=aeron_directory) publication = context.add_exclusive_publication('aeron:ipc', stream_id) blob = bytearray(50 * 1024 * 1024) assert_that(calling(publication.offer).with_args(blob), raises(RuntimeError))
def test_add_subscription(aeron_directory): context = Context(aeron_dir=aeron_directory) subscription = context.add_subscription('aeron:ipc', 1000) assert_that(subscription, is_not(None)) assert_that(subscription.channel, is_('aeron:ipc')) assert_that(subscription.stream_id, is_(1000))
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))
def context(aeron_directory): return Context(aeron_dir=aeron_directory)
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'))
def test_add_publication__wrong_channel(aeron_directory): context = Context(aeron_dir=aeron_directory) assert_that( calling(context.add_publication).with_args('wrong channel', 1), raises(RuntimeError))
def test_create__default(aeron_directory): context = Context(aeron_dir=aeron_directory) assert_that(context, is_not(None))
def context(aeron_directory): return Context(aeron_dir=aeron_directory, resource_linger_timeout=timedelta(milliseconds=10))
def main(args): 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=10000) parser.add_argument('-l', '--linger', type=int, default=60*60*1000) parser.add_argument('-k', '--processes', type=int, default=4) 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}'), # 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}') ) messages = [] for i in range(args.messages): messages.append(bytearray(os.urandom(256))) start = time.time() publishers = [] for streamID in range(1, args.processes+1): publishers.append(Process(target=publisher, args=(streamID, messages, args,))) subscribers = [] for streamID in range(1, args.processes+1): publishers.append(Process(target=subscriber, args=(args,))) for p in publishers: p.start() for p in subscribers: p.start() for p in publishers: p.join() for p in subscribers: p.join() # p.terminate() for p in subscribers: p.join() end = time.time() print(str(end - start)) except ArgumentError as e: print(e, file=sys.stderr) return -1 except Exception as e: print(e, file=sys.stderr) return -2