def __init__(self, gui, sender_address, receiver_address): super(ChatMesssenger, self).__init__() self.gui = gui self.sender_address = sender_address self.receiver_address = receiver_address self.events = EventInjector() self.sender = None self.ready_to_send = False self.outgoing_queue = Queue()
class ApplicationEventTest(Test): """Test application defined events and handlers.""" class MyTestServer(TestServer): def __init__(self): super(ApplicationEventTest.MyTestServer, self).__init__() class MyHandler(Handler): def __init__(self, test): super(ApplicationEventTest.MyHandler, self).__init__() self._test = test def on_hello(self, event): # verify PROTON-1056 self._test.hello_rcvd = str(event) def on_goodbye(self, event): self._test.goodbye_rcvd = str(event) def setUp(self): import os if not hasattr(os, 'pipe'): # KAG: seems like Jython doesn't have an os.pipe() method raise SkipTest() if os.name=="nt": # Correct implementation on Windows is complicated raise SkipTest("PROTON-1071") self.server = ApplicationEventTest.MyTestServer() self.server.reactor.handler.add(ApplicationEventTest.MyHandler(self)) self.event_injector = EventInjector() self.hello_event = ApplicationEvent("hello") self.goodbye_event = ApplicationEvent("goodbye") self.server.reactor.selectable(self.event_injector) self.hello_rcvd = None self.goodbye_rcvd = None self.server.start() def tearDown(self): self.server.stop() def _wait_for(self, predicate, timeout=10.0): deadline = time.time() + timeout while time.time() < deadline: if predicate(): break time.sleep(0.1) assert predicate() def test_application_events(self): self.event_injector.trigger(self.hello_event) self._wait_for(lambda: self.hello_rcvd is not None) self.event_injector.trigger(self.goodbye_event) self._wait_for(lambda: self.goodbye_rcvd is not None)
class ChatMesssenger(MessagingHandler): def __init__(self, gui, sender_address, receiver_address): super(ChatMesssenger, self).__init__() self.gui = gui self.sender_address = sender_address self.receiver_address = receiver_address self.events = EventInjector() self.sender = None self.ready_to_send = False self.outgoing_queue = Queue() def on_start(self, event): print('on_start') event.container.selectable(self.events) event.container.create_receiver(self.receiver_address) event.container.create_sender(self.sender_address, options=AtMostOnce()) def on_sendable(self, event): print('on_sendable') self.sender = event.sender self.ready_to_send = True self.on_message_to_send() # TODO: ??? I cannot send my messages from on_sendable because I do not always have something to send here # and if I miss one on_sendable and do not send anything, it will not be called again the next time def on_message(self, event): print("on_message") self.gui.receive_message(str(event.message.body)) def on_message_to_send(self, _=None): if not self.ready_to_send: return while self.sender.credit: if self.outgoing_queue.empty(): return message = proton.Message() body = self.outgoing_queue.get() message.body = body self.sender.send(message) self.ready_to_send = False def send_message(self, body): self.outgoing_queue.put(body) self.events.trigger(ApplicationEvent('message_to_send'))
def setUp(self): import os if not hasattr(os, 'pipe'): # KAG: seems like Jython doesn't have an os.pipe() method raise SkipTest() if os.name=="nt": # Correct implementation on Windows is complicated raise SkipTest("PROTON-1071") self.server = ApplicationEventTest.MyTestServer() self.server.reactor.handler.add(ApplicationEventTest.MyHandler(self)) self.event_injector = EventInjector() self.hello_event = ApplicationEvent("hello") self.goodbye_event = ApplicationEvent("goodbye") self.server.reactor.selectable(self.event_injector) self.hello_rcvd = None self.goodbye_rcvd = None self.server.start()
def __init__(self, url, count): super(Recv, self).__init__(auto_accept=False) self.url = url self.delay = 0 self.last_id = None self.expected = count self.received = 0 self.accepted = 0 self.db = Db("dst_db", EventInjector())
def __init__(self, aInOAmqpClient): super(Recv, self).__init__() self.oAmqpClient = aInOAmqpClient self.received = 0 self.ForceDeploy = ForceDeploy(self.oAmqpClient.vsdUserName, self.oAmqpClient.vsdPassword, self.oAmqpClient.vsdOrg, self.oAmqpClient.vsdUrl, LOG, EventInjector())
def __init__(self, url, count): super(Send, self).__init__() self.url = url self.delay = 0 self.sent = 0 self.confirmed = 0 self.load_count = 0 self.records = Queue.Queue(maxsize=50) self.target = count self.db = Db("src_db", EventInjector())
class Lock(MessagingHandler): def __init__(self, reactor, connection, lock_name, label=None, user_context=None): super(Lock, self).__init__(prefetch=0, auto_accept=False, auto_settle=False) self.reactor = reactor self.connection = connection self.lock_name = lock_name self.user_context = user_context self.acquired_state = False self.events = EventInjector() self.reactor.selectable(self.events) self.lock_receiver = None def destroy(self): if self.acquired_state and self.lock_receiver: self.lock_receiver.close() self.events.close() @property def is_acquired(self): return self.acquired_state def acquire(self): if self.acquired_state: raise LockException("Lock already acquired") self.lock_receiver = self.reactor.create_receiver(self.connection, self.lock_name, handler=self) def release(self): if not self.acquired_state: raise LockException("Lock not acquired") self.acquired_state = False self.lock_receiver.close() def on_link_opened(self, event): if event.receiver == self.lock_receiver and event.receiver.remote_source.address == self.lock_name: self.acquired_state = True new_event = ApplicationEvent("lock_acquired") new_event.user_context = self.user_context self.events.trigger(new_event) def on_link_error(self, event): if event.receiver == self.lock_receiver: new_event = ApplicationEvent("lock_failed") new_event.user_context = self.user_context new_event.error = event.link.remote_condition self.events.trigger(new_event) def on_link_closed(self, event): if event.receiver == self.lock_receiver and self.acquired_state: self.acquired_state = False new_event = ApplicationEvent("lock_released") new_event.user_context = self.user_context self.events.trigger(new_event)
def __init__(self, reactor, connection, service_address): super(RequestServer, self).__init__(auto_accept=False, auto_settle=False) self.service_address = service_address self.reactor = reactor self.events = EventInjector() self.reactor.selectable(self.events) self.connection = connection self.service_receiver = self.reactor.create_receiver(self.connection, self.service_address, handler=self) self.reply_sender = self.reactor.create_sender(self.connection, None, handler=self)
class RequestServer(MessagingHandler): def __init__(self, reactor, connection, service_address): super(RequestServer, self).__init__(auto_accept=False, auto_settle=False) self.service_address = service_address self.reactor = reactor self.events = EventInjector() self.reactor.selectable(self.events) self.connection = connection self.service_receiver = self.reactor.create_receiver(self.connection, self.service_address, handler=self) self.reply_sender = self.reactor.create_sender(self.connection, None, handler=self) def stop(self): self.service_receiver.close() self.reply_sender.close() self.events.close() def fail(self, request, message): request.in_delivery.local.condition = Condition('sandpiper:bad-request', message) self.reject(request.in_delivery) def reply(self, request, properties={}, data=None): reply_message = Message(properties=properties, body=data, address=request.message.reply_to, correlation_id=request.message.correlation_id) request.out_delivery = self.reply_sender.send(reply_message) request.out_delivery._request = request def on_settled(self, event): try: request = event.delivery._request self.accept(request.in_delivery) except: pass def on_message(self, event): request = Request(event) request_event = ApplicationEvent("request") request_event.request = request request_event.server = self self.events.trigger(request_event)
def __init__(self, reactor, connection, lock_name, label=None, user_context=None): super(Lock, self).__init__(prefetch=0, auto_accept=False, auto_settle=False) self.reactor = reactor self.connection = connection self.lock_name = lock_name self.user_context = user_context self.acquired_state = False self.events = EventInjector() self.reactor.selectable(self.events) self.lock_receiver = None
def __init__(self, reactor, connection, service_address): super(RequestClient, self).__init__() self.service_address = service_address self.reactor = reactor self.events = EventInjector() self.reactor.selectable(self.events) self.connection = connection self.service_sender = None self.reply_receiver = self.reactor.create_receiver(self.connection, dynamic=True, handler=self) self.reply_to = None self.ready = False self.cid = 1 self.contexts = {}
def test_application_event_no_object_leaks(self): event_types_count = len(proton.EventType.TYPES) injector = EventInjector() p = Program(injector) c = Container(p) t = threading.Thread(target=c.run) t.start() p.on_start_.wait() object_counts = [] gc.collect() object_counts.append(len(gc.get_objects())) for i in range(100): injector.trigger(ApplicationEvent("count_up")) gc.collect() object_counts.append(len(gc.get_objects())) self.assertEqual(len(proton.EventType.TYPES), event_types_count + 1) injector.trigger(ApplicationEvent("done", subject=c)) self.assertEqual(len(proton.EventType.TYPES), event_types_count + 2) t.join() gc.collect() object_counts.append(len(gc.get_objects())) self.assertEqual(p.counter, 100) self.assertTrue( object_counts[1] - object_counts[0] <= 220, "Object counts should not be increasing too fast: {0}".format( object_counts)) self.assertTrue( object_counts[2] - object_counts[0] <= 10, "No objects should be leaking at the end: {0}".format( object_counts))
class RequestClient(MessagingHandler): def __init__(self, reactor, connection, service_address): super(RequestClient, self).__init__() self.service_address = service_address self.reactor = reactor self.events = EventInjector() self.reactor.selectable(self.events) self.connection = connection self.service_sender = None self.reply_receiver = self.reactor.create_receiver(self.connection, dynamic=True, handler=self) self.reply_to = None self.ready = False self.cid = 1 self.contexts = {} def stop(self): self.service_sender.close() self.reply_receiver.close() self.events.close() def request(self, properties={}, body=None, user_context=None): cid = self.cid self.cid += 1 request_message = Message(properties = properties, body = body, reply_to = self.reply_to, correlation_id = cid) self.contexts[cid] = user_context dlv = self.service_sender.send(request_message) dlv._cid = cid def on_link_opened(self, event): if event.receiver == self.reply_receiver: self.reply_to = self.reply_receiver.remote_source.address self.service_sender = self.reactor.create_sender(self.connection, self.service_address, handler=self) def on_sendable(self, event): if not self.ready and event.sender == self.service_sender: self.ready = True self.events.trigger(ApplicationEvent("service_ready")) def on_rejected(self, event): dlv = event.delivery context = None if dlv._cid in self.contexts: context = self.contexts[dlv._cid] event = ApplicationEvent("request_failed") event.context = context event.error = None def on_message(self, event): reply_message = event.message cid = reply_message.correlation_id context = None if cid in self.contexts: context = self.contexts[cid] new_event = ApplicationEvent("response") new_event.properties = reply_message.properties new_event.body = reply_message.body new_event.user_context = context new_event.client = self self.events.trigger(new_event)
self.transaction.commit() def on_abort(self, event): self.transaction.abort() def on_fetch(self, event): self.receiver.flow(1) def on_quit(self, event): c = self.receiver.connection self.receiver.close() c.close() try: reactor = Container(TxRecv()) events = EventInjector() reactor.selectable(events) thread = threading.Thread(target=reactor.run) thread.daemon=True thread.start() print("Enter 'fetch', 'commit' or 'abort'") while True: line = sys.stdin.readline() if line: events.trigger(ApplicationEvent(line.strip())) else: break except KeyboardInterrupt: pass
def on_abort(self, event): self.transaction.abort() def on_fetch(self, event): self.receiver.flow(1) def on_quit(self, event): c = self.receiver.connection self.receiver.close() c.close() try: reactor = Container(TxRecv()) events = EventInjector() reactor.selectable(events) thread = threading.Thread(target=reactor.run) thread.daemon = True thread.start() print("Enter 'fetch', 'commit' or 'abort'") while True: line = sys.stdin.readline() if line: events.trigger(ApplicationEvent(line.strip())) else: break except KeyboardInterrupt: pass