How to use handle_message method in prospector

Best Python code snippet using prospector_python

test_dispatchers.py

Source:test_dispatchers.py Github

copy

Full Screen

...13 Also covers testing the ThreadPoolDispatcher with limited number of threads.14 """15 def test_priority_is_passed_to_base_class(self):16 messagebus = create_messagebus()17 def handle_message(message):18 pass19 dispatcher = SequentialDispatcher(messagebus, handle_message, priority=-7)20 self.assertEqual(dispatcher.priority, -7)21 def test_destroy_can_be_called_on_a_dispatcher_that_is_not_registered(self):22 messagebus = create_messagebus()23 def handle_message(message):24 pass25 dispatcher = SequentialDispatcher(messagebus, handle_message)26 dispatcher.destroy()27 def test_dispatcher_can_be_registered_and_deregistered_in_messagebus(self):28 messagebus = create_messagebus()29 def handle_message(message):30 pass31 dispatcher = SequentialDispatcher(messagebus, handle_message)32 try:33 dispatcher.register([defined_message])34 finally:35 dispatcher.destroy()36 def test_register_raises_exception_when_called_with_empty_message_ids(self):37 messagebus = create_messagebus()38 def handle_message(message):39 pass40 dispatcher = SequentialDispatcher(messagebus, handle_message)41 self.assertRaises(Exception, dispatcher.register, [])42 def test_that_dispatcher_can_be_created_with_lambda(self):43 messagebus = create_messagebus()44 dispatcher = SequentialDispatcher(messagebus, lambda message: None)45 try:46 dispatcher.register([defined_message])47 finally:48 dispatcher.destroy()49 def test_dispatcher_can_be_started_and_stopped(self):50 messagebus = create_messagebus()51 def handle_message(message):52 pass53 dispatcher = SequentialDispatcher(messagebus, handle_message)54 dispatcher.start()55 self.assertIsNotNone(dispatcher._executor)56 dispatcher.stop(timeout=1)57 self.assertTrue(dispatcher._stopped)58 def test_message_triggers_handle_message(self):59 messagebus = create_messagebus()60 received_messages = Queue()61 def handle_message(message):62 received_messages.put(message)63 dispatcher = SequentialDispatcher(messagebus, handle_message)64 try:65 dispatcher.register(message_ids=[defined_message])66 messagebus.trigger_event(defined_message, defined_endpoint, data='data')67 self.assertEqual(received_messages.get(timeout=1).data, 'data')68 finally:69 dispatcher.destroy()70 def test_multiple_messages_triggers_handle_message(self):71 messagebus = create_messagebus()72 received_messages = Queue()73 def handle_message(message):74 received_messages.put(message)75 dispatcher = SequentialDispatcher(messagebus, handle_message)76 try:77 dispatcher.register(message_ids=[defined_message])78 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')79 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')80 self.assertIn(received_messages.get(timeout=1).data, 'data1')81 self.assertIn(received_messages.get(timeout=1).data, 'data2')82 finally:83 dispatcher.destroy()84 def test_multiple_messages_can_be_registered_separately(self):85 messagebus = create_messagebus()86 received_messages = Queue()87 def handle_message(message):88 received_messages.put(message)89 dispatcher = SequentialDispatcher(messagebus, handle_message)90 try:91 dispatcher.register(message_ids=[defined_message])92 dispatcher.register(message_ids=[defined_message2])93 messagebus.trigger_event(defined_message, defined_endpoint2, data='data1')94 messagebus.trigger_event(defined_message2, defined_endpoint2, data='data2')95 self.assertEqual(received_messages.get(timeout=1).data, 'data1')96 self.assertEqual(received_messages.get(timeout=1).data, 'data2')97 finally:98 dispatcher.destroy()99 def test_requests_trigger_handle_message(self):100 messagebus = create_messagebus()101 def handle_message(message):102 return message.data103 dispatcher = SequentialDispatcher(messagebus, handle_message)104 try:105 dispatcher.register([defined_message])106 fs = messagebus.send_request(defined_message, defined_endpoint, data='data')107 self.assertEqual(fs[0].result(timeout=1), 'data')108 finally:109 dispatcher.destroy()110 def test_multiple_requests_trigger_handle_message(self):111 messagebus = create_messagebus()112 def handle_message(message):113 return message.data114 dispatcher = SequentialDispatcher(messagebus, handle_message)115 try:116 dispatcher.register([defined_message])117 fs1 = messagebus.send_request(defined_message, defined_endpoint, data='data1')118 fs2 = messagebus.send_request(defined_message, defined_endpoint, data='data2')119 self.assertEqual(fs1[0].result(timeout=1), 'data1')120 self.assertEqual(fs2[0].result(timeout=1), 'data2')121 finally:122 dispatcher.destroy()123 def test_active_count_increased_during_message_handling(self):124 messagebus = create_messagebus()125 active_count = Queue()126 def handle_message(message):127 active_count.put(dispatcher.get_active_count())128 dispatcher = SequentialDispatcher(messagebus, handle_message)129 try:130 dispatcher.register(message_ids=[defined_message])131 self.assertEqual(dispatcher.get_active_count(), 0)132 messagebus.trigger_event(defined_message, defined_endpoint)133 self.assertEqual(active_count.get(timeout=1), 1)134 self.assertEqual(dispatcher.get_active_count(), 0)135 finally:136 dispatcher.destroy()137 def test_optional_dispatcher_is_not_registered_if_message_is_not_defined(self):138 messagebus = create_messagebus()139 active_count = Queue()140 def handle_message(message):141 active_count.put(dispatcher.get_active_count())142 try:143 dispatcher = SequentialDispatcher(messagebus, handle_message)144 dispatcher.register(message_ids=[notdefined_message], optional=True)145 self.assertEqual(dispatcher.get_active_count(), 0)146 finally:147 dispatcher.destroy()148 def test_optional_dispatcher_is_not_registered_if_endpoint_is_not_defined(self):149 messagebus = create_messagebus()150 active_count = Queue()151 def handle_message(message):152 active_count.put(dispatcher.get_active_count())153 try:154 dispatcher = SequentialDispatcher(messagebus, handle_message)155 dispatcher.register(156 message_ids=[defined_message], endpoint_ids=[notdefined_endpoint], optional=True)157 self.assertEqual(dispatcher.get_active_count(), 0)158 finally:159 dispatcher.destroy()160 def test_message_handler_raises_exception_on_triggered_event_does_not_destroy_dispatcher_thread(161 self):162 messagebus = create_messagebus()163 received = Queue()164 def handle_message(message):165 received.put(message.data)166 raise Exception('')167 try:168 dispatcher = SequentialDispatcher(messagebus, handle_message)169 dispatcher.register(message_ids=[defined_message], endpoint_ids=[defined_endpoint])170 messagebus.trigger_event(defined_message, defined_endpoint, data=1)171 self.assertIsNotNone(dispatcher._executor)172 messagebus.trigger_event(defined_message, defined_endpoint, data=2)173 self.assertIsNotNone(dispatcher._executor)174 self.assertEqual(received.get(timeout=1), 1)175 self.assertEqual(received.get(timeout=2), 2)176 finally:177 dispatcher.destroy()178class TestConcurrentDispatcher(unittest.TestCase):179 def test_priority_is_passed_to_base_class(self):180 messagebus = create_messagebus()181 def handle_message(message):182 pass183 dispatcher = ConcurrentDispatcher(messagebus, handle_message, priority=-7)184 self.assertEqual(dispatcher.priority, -7)185 def test_destroy_can_be_called_on_a_dispatcher_that_is_not_registered(self):186 messagebus = create_messagebus()187 def handle_message(message):188 pass189 dispatcher = ConcurrentDispatcher(messagebus, handle_message)190 dispatcher.destroy()191 def test_dispatcher_can_be_registered_and_deregistered_in_messagebus(self):192 messagebus = create_messagebus()193 def handle_message(message):194 pass195 dispatcher = ConcurrentDispatcher(messagebus, handle_message)196 try:197 dispatcher.register(message_ids=[defined_message])198 finally:199 dispatcher.destroy()200 def test_dispatcher_can_be_deregistered_in_multiple_calls(self):201 messagebus = create_messagebus()202 def handle_message(message):203 pass204 dispatcher = ConcurrentDispatcher(messagebus, handle_message)205 try:206 dispatcher.register(message_ids=[defined_message, defined_message2])207 dispatcher.deregister([defined_message])208 dispatcher.deregister([defined_message2])209 finally:210 dispatcher.destroy()211 def test_message_triggers_handle_message(self):212 messagebus = create_messagebus()213 received_messages = Queue()214 def handle_message(message):215 received_messages.put(message)216 dispatcher = ConcurrentDispatcher(messagebus, handle_message)217 try:218 dispatcher.register([defined_message])219 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')220 self.assertEqual(received_messages.get(timeout=1).data, 'data1')221 finally:222 dispatcher.destroy()223 def test_multiple_messages_triggers_handle_message(self):224 messagebus = create_messagebus()225 received_messages = Queue()226 def handle_message(message):227 received_messages.put(message)228 dispatcher = ConcurrentDispatcher(messagebus, handle_message)229 try:230 dispatcher.register([defined_message])231 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')232 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')233 self.assertIn(received_messages.get(timeout=1).data, ['data1', 'data2'])234 self.assertIn(received_messages.get(timeout=1).data, ['data1', 'data2'])235 finally:236 dispatcher.destroy()237 def test_concurrent_dispatcher_is_triggered_asynchronously(self):238 messagebus = create_messagebus()239 queue1 = Queue()240 queue2 = Queue()241 completed_queue = Queue()242 def handle_message(message):243 (put_queue, get_queue, value) = message.data244 put_queue.put(value)245 completed_queue.put(get_queue.get(timeout=1))246 dispatcher = ConcurrentDispatcher(messagebus, handle_message)247 try:248 dispatcher.register([defined_message])249 messagebus.trigger_event(defined_message, defined_endpoint, data=(queue1, queue2, '1'))250 messagebus.trigger_event(defined_message, defined_endpoint, data=(queue2, queue1, '2'))251 results = set()252 results.add(completed_queue.get(timeout=1))253 results.add(completed_queue.get(timeout=2))254 self.assertEqual(results, {'1', '2'})255 finally:256 dispatcher.destroy()257 def test_requests_trigger_handle_message(self):258 messagebus = create_messagebus()259 def handle_message(message):260 return message.data261 dispatcher = ConcurrentDispatcher(messagebus, handle_message)262 try:263 dispatcher.register([defined_message])264 fs = messagebus.send_request(defined_message, defined_endpoint, data='data')265 self.assertEqual(fs[0].result(timeout=1), 'data')266 finally:267 dispatcher.destroy()268 def test_multiple_requests_trigger_handle_message(self):269 messagebus = create_messagebus()270 def handle_message(message):271 return message.data272 dispatcher = ConcurrentDispatcher(messagebus, handle_message)273 try:274 dispatcher.register([defined_message])275 fs1 = messagebus.send_request(defined_message, defined_endpoint, data='data1')276 fs2 = messagebus.send_request(defined_message, defined_endpoint, data='data2')277 self.assertEqual(fs1[0].result(timeout=1), 'data1')278 self.assertEqual(fs2[0].result(timeout=1), 'data2')279 finally:280 dispatcher.destroy()281 def test_concurrent_dispatcher_requests_are_triggered_asynchronously(self):282 messagebus = create_messagebus()283 queue1 = Queue()284 queue2 = Queue()285 completed_queue = Queue()286 def handle_message(message):287 (put_queue, get_queue, value) = message.data288 put_queue.put(value)289 completed_queue.put(get_queue.get(timeout=1))290 return value291 dispatcher = ConcurrentDispatcher(messagebus, handle_message)292 try:293 dispatcher.register([defined_message])294 futures1 = messagebus.send_request(295 defined_message, defined_endpoint, data=(queue1, queue2, '1'))296 futures2 = messagebus.send_request(297 defined_message, defined_endpoint, data=(queue2, queue1, '2'))298 results = set()299 results.add(completed_queue.get(timeout=1))300 results.add(completed_queue.get(timeout=2))301 self.assertEqual(results, {'1', '2'})302 self.assertEqual(futures1[0].result(timeout=1), '1')303 self.assertEqual(futures2[0].result(timeout=1), '2')304 finally:305 dispatcher.destroy()306 def test_active_count_increased_during_message_handling(self):307 messagebus = create_messagebus()308 active_count = Queue()309 thread1_done = Queue()310 thread2_done = Queue()311 def handle_message(message):312 (put_queue, get_queue) = message.data313 active_count.put(dispatcher.get_active_count())314 put_queue.put(True)315 get_queue.get(timeout=1)316 dispatcher = ConcurrentDispatcher(messagebus, handle_message)317 try:318 dispatcher.register(message_ids=[defined_message])319 self.assertEqual(dispatcher.get_active_count(), 0)320 futures1 = messagebus.send_request(321 defined_message, defined_endpoint, data=(thread1_done, thread2_done))322 self.assertEqual(active_count.get(timeout=1), 1)323 futures2 = messagebus.send_request(324 defined_message, defined_endpoint, data=(thread2_done, thread1_done))325 self.assertEqual(active_count.get(timeout=1), 2)326 futures1.wait(timeout=1)[0].result(timeout=1)327 futures2.wait(timeout=1)[0].result(timeout=1)328 self.assertEqual(dispatcher.get_active_count(), 0)329 finally:330 dispatcher.destroy()331 def test_optional_dispatcher_is_not_registered_if_message_is_not_defined(self):332 messagebus = create_messagebus()333 active_count = Queue()334 def handle_message(message):335 active_count.put(dispatcher.get_active_count())336 try:337 dispatcher = ConcurrentDispatcher(messagebus, handle_message)338 dispatcher.register(message_ids=[notdefined_message], optional=True)339 self.assertEqual(dispatcher.get_active_count(), 0)340 finally:341 dispatcher.destroy()342 def test_optional_dispatcher_is_not_registered_if_endpoint_is_not_defined(self):343 messagebus = create_messagebus()344 active_count = Queue()345 def handle_message(message):346 active_count.put(dispatcher.get_active_count())347 try:348 dispatcher = ConcurrentDispatcher(messagebus, handle_message)349 dispatcher.register(350 message_ids=[defined_message], endpoint_ids=[notdefined_endpoint], optional=True)351 self.assertEqual(dispatcher.get_active_count(), 0)352 finally:353 dispatcher.destroy()354class TestCallbackDispatcher(unittest.TestCase):355 def test_priority_is_passed_to_base_class(self):356 messagebus = create_messagebus()357 def handle_message(message):358 pass359 dispatcher = CallbackDispatcher(messagebus, handle_message, priority=-7)360 self.assertEqual(dispatcher.priority, -7)361 def test_destroy_can_be_called_on_a_dispatcher_that_is_not_registered(self):362 messagebus = create_messagebus()363 def handle_message(message):364 pass365 dispatcher = CallbackDispatcher(messagebus, handle_message)366 dispatcher.destroy()367 def test_callback_can_be_registered_and_deregistered(self):368 messagebus = create_messagebus()369 def handle_message(message):370 pass371 dispatcher = CallbackDispatcher(messagebus, handle_message)372 try:373 dispatcher.register(message_ids=[defined_message])374 finally:375 dispatcher.destroy()376 def test_message_triggers_handle_message(self):377 messagebus = create_messagebus()378 received_messages = Queue()379 def handle_message(message):380 received_messages.put(message)381 dispatcher = CallbackDispatcher(messagebus, handle_message)382 try:383 dispatcher.register(message_ids=[defined_message])384 messagebus.trigger_event(defined_message, defined_endpoint, data='data')385 self.assertEqual(received_messages.get(timeout=1).data, 'data')386 finally:387 dispatcher.destroy()388 def test_requests_trigger_handle_message(self):389 messagebus = create_messagebus()390 def handle_message(message):391 return message.data392 dispatcher = CallbackDispatcher(messagebus, handle_message)393 try:394 dispatcher.register([defined_message])395 fs = messagebus.send_request(defined_message, defined_endpoint, data='data')396 self.assertEqual(fs[0].result(timeout=1), 'data')397 finally:398 dispatcher.destroy()399 def test_active_count_increased_during_message_handling(self):400 messagebus = create_messagebus()401 active_count = Queue()402 def handle_message(message):403 active_count.put(dispatcher.get_active_count())404 dispatcher = CallbackDispatcher(messagebus, handle_message)405 try:406 dispatcher.register(message_ids=[defined_message])407 self.assertEqual(dispatcher.get_active_count(), 0)408 messagebus.trigger_event(defined_message, defined_endpoint)409 self.assertEqual(active_count.get(timeout=1), 1)410 self.assertEqual(dispatcher.get_active_count(), 0)411 finally:412 dispatcher.destroy()413 def test_optional_dispatcher_is_not_registered_if_message_is_not_defined(self):414 messagebus = create_messagebus()415 active_count = Queue()416 def handle_message(message):417 active_count.put(dispatcher.get_active_count())418 try:419 dispatcher = CallbackDispatcher(messagebus, handle_message)420 dispatcher.register(message_ids=[notdefined_message], optional=True)421 self.assertEqual(dispatcher.get_active_count(), 0)422 finally:423 dispatcher.destroy()424 def test_optional_dispatcher_is_not_registered_if_endpoint_is_not_defined(self):425 messagebus = create_messagebus()426 active_count = Queue()427 def handle_message(message):428 active_count.put(dispatcher.get_active_count())429 try:430 dispatcher = CallbackDispatcher(messagebus, handle_message)431 dispatcher.register(432 message_ids=[defined_message], endpoint_ids=[notdefined_endpoint], optional=True)433 self.assertEqual(dispatcher.get_active_count(), 0)434 finally:435 dispatcher.destroy()436class TestDispatcherOnMethod(unittest.TestCase):437 def setUp(self):438 self._received_messages = Queue()439 def handle_message(self, message):440 self._received_messages.put(message)441 def test_method_can_be_registered_with_self_as_instance(self):442 messagebus = create_messagebus()443 dispatcher = ConcurrentDispatcher(messagebus, self.handle_message)444 try:445 dispatcher.register([defined_message])446 messagebus.trigger_event(defined_message, defined_endpoint, data='data')447 self.assertEqual(self._received_messages.get(timeout=1).data, 'data')448 finally:449 dispatcher.destroy()450class TestLocalMessageQueue(unittest.TestCase):451 def test_messages_added_after_dispatcher_is_registered_are_available(self):452 messagebus = create_messagebus()453 with LocalMessageQueue(messagebus, [defined_message]) as local_queue:454 messagebus.trigger_event(defined_message, defined_endpoint, data='data')455 message = local_queue.get(timeout=1)456 self.assertEqual(message.data, 'data')457 def test_messages_added_before_dispatcher_is_registered_are_not_available(self):458 messagebus = create_messagebus()459 messagebus.trigger_event(defined_message2, defined_endpoint, data='data')460 with LocalMessageQueue(messagebus, [defined_message, defined_message2]) as local_queue:461 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')462 message = local_queue.get(timeout=1)463 self.assertEqual(message.data, 'data2')464 def test_messages_available_in_order_they_were_added(self):465 messagebus = create_messagebus()466 with LocalMessageQueue(467 messagebus, [defined_message, defined_message2, defined_message3]) as local_queue:468 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')469 messagebus.trigger_event(defined_message2, defined_endpoint2, data='data2')470 messagebus.trigger_event(defined_message3, defined_endpoint2, data='data3')471 message1 = local_queue.get(timeout=1)472 self.assertEqual(message1.data, 'data1')473 message2 = local_queue.get(timeout=1)474 self.assertEqual(message2.data, 'data2')475 message3 = local_queue.get(timeout=1)476 self.assertEqual(message3.data, 'data3')477 def test_messages_added_only_when_matches(self):478 messagebus = create_messagebus()479 def match(message):480 return message.data == 'data2'481 with LocalMessageQueue(messagebus, [defined_message, defined_message2],482 match=match) as local_queue:483 messagebus.trigger_event(defined_message, defined_endpoint, data='data')484 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')485 message = local_queue.get(timeout=1)486 self.assertEqual(message.data, 'data2')487 def test_empty_returns_true_if_the_queue_is_empty(self):488 messagebus = create_messagebus()489 with LocalMessageQueue(messagebus, [defined_message]) as local_queue:490 self.assertTrue(local_queue.empty())491 def test_empty_returns_false_if_the_queue_is_not_empty(self):492 messagebus = create_messagebus()493 with LocalMessageQueue(messagebus, [defined_message]) as local_queue:494 messagebus.trigger_event(defined_message, defined_endpoint, data='data')495 self.assertFalse(local_queue.empty())496 def test_queue_get_can_be_unblocked_from_another_thread(self):497 messagebus = create_messagebus()498 with LocalMessageQueue(messagebus, [defined_message]) as local_queue:499 with self.assertRaises(QueueUnblockedException):500 local_queue.unblock()501 local_queue.get(timeout=1)502class TestMessageFilter(unittest.TestCase):503 def test_filter_can_be_applied_to_dispatcher(self):504 messagebus = create_messagebus()505 received_messages = Queue()506 def match(message):507 return message.data == 'data2'508 @MessageFilter(match)509 def handle_message(message):510 received_messages.put(message)511 dispatcher = SequentialDispatcher(messagebus, handle_message)512 try:513 dispatcher.register([defined_message])514 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')515 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')516 self.assertEqual(received_messages.get(timeout=1).data, 'data2')517 finally:518 dispatcher.destroy()519class TestComponentsOnHandlers(unittest.TestCase):520 def load_components(self):521 @component(scope='message')522 class MessageScoped(object):523 def __init__(self):524 self.calls = 0525 def __call__(self, *args, **kwargs):526 self.calls += 1527 return self.calls528 @component(scope='dispatcher')529 class DispatcherScoped(object):530 def __init__(self):531 self.calls = 0532 def __call__(self, *args, **kwargs):533 self.calls += 1534 return self.calls535 @component(scope='session')536 class SessionScoped(object):537 def __init__(self):538 self.calls = 0539 def __call__(self, *args, **kwargs):540 self.calls += 1541 return self.calls542 def setUp(self):543 self.component_manager = ComponentManager()544 self.component_manager.clear_component_registry()545 self.load_components()546 def test_sequential_dispatcher_requires_component(self):547 messagebus = create_messagebus()548 comp_counts = Queue()549 @requires(comp='MessageScoped')550 def handle_message(message, comp):551 comp_counts.put(comp())552 dispatcher = SequentialDispatcher(messagebus, handle_message)553 try:554 dispatcher.register([defined_message])555 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')556 self.assertEqual(comp_counts.get(timeout=1), 1)557 finally:558 dispatcher.destroy()559 def test_concurrent_dispatcher_requires_component(self):560 messagebus = create_messagebus()561 comp_counts = Queue()562 @requires(comp='MessageScoped')563 def handle_message(message, comp):564 comp_counts.put(comp())565 dispatcher = ConcurrentDispatcher(messagebus, handle_message)566 try:567 dispatcher.register([defined_message])568 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')569 self.assertEqual(comp_counts.get(timeout=1), 1)570 finally:571 dispatcher.destroy()572 def test_callback_dispatcher_requires_component(self):573 messagebus = create_messagebus()574 comp_counts = Queue()575 @requires(comp='MessageScoped')576 def handle_message(message, comp):577 comp_counts.put(comp())578 dispatcher = CallbackDispatcher(messagebus, handle_message)579 try:580 dispatcher.register([defined_message])581 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')582 self.assertEqual(comp_counts.get(timeout=1), 1)583 finally:584 dispatcher.destroy()585 def test_multiple_calls_give_different_message_scoped_components(self):586 messagebus = create_messagebus()587 comp_counts = Queue()588 @requires(comp='MessageScoped')589 def handle_message(message, comp):590 comp_counts.put(comp())591 dispatcher = CallbackDispatcher(messagebus, handle_message)592 try:593 dispatcher.register([defined_message])594 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')595 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')596 self.assertEqual(comp_counts.get(timeout=1), 1)597 self.assertEqual(comp_counts.get(timeout=1), 1)598 finally:599 dispatcher.destroy()600 def test_multiple_calls_give_same_dispatcher_scoped_component(self):601 messagebus = create_messagebus()602 comp_counts_a = Queue()603 comp_counts_b = Queue()604 @requires(comp='DispatcherScoped')605 def handle_message_a(message, comp):606 comp_counts_a.put(comp())607 @requires(comp='DispatcherScoped')608 def handle_message_b(message, comp):609 comp_counts_b.put(comp())610 dispatcher_a = CallbackDispatcher(messagebus, handle_message_a)611 dispatcher_b = CallbackDispatcher(messagebus, handle_message_b)612 try:613 try:614 dispatcher_a.register([defined_message])615 dispatcher_b.register([defined_message])616 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')617 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')618 self.assertEqual(comp_counts_a.get(timeout=1), 1)619 self.assertEqual(comp_counts_a.get(timeout=1), 2)620 self.assertEqual(comp_counts_b.get(timeout=1), 1)621 self.assertEqual(comp_counts_b.get(timeout=1), 2)622 finally:623 dispatcher_a.destroy()624 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')625 assert comp_counts_a.empty()626 self.assertEqual(comp_counts_b.get(timeout=1), 3)627 finally:628 dispatcher_b.destroy()629 def test_multiple_calls_give_same_session_scoped_component(self):630 messagebus = create_messagebus()631 comp_counts = Queue()632 @requires(comp='SessionScoped')633 def handle_message(message, comp):634 comp_counts.put(comp())635 dispatcher = CallbackDispatcher(messagebus, handle_message)636 try:637 dispatcher.register([defined_message])638 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')639 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')640 self.assertEqual(comp_counts.get(timeout=1), 1)641 self.assertEqual(comp_counts.get(timeout=1), 2)642 finally:643 dispatcher.destroy()644 def test_message_entity_is_used_to_fixate_entities(self):645 messagebus = create_messagebus()646 calls = Queue()647 @component(name='A')648 class a1():649 entity = 'a1'650 @component(name='A')651 class a2():652 entity = 'a2'653 @requires(a='A')654 def handle_message(message, a):655 calls.put(a.entity)656 dispatcher = CallbackDispatcher(messagebus, handle_message)657 try:658 dispatcher.register([defined_message], entities=['a1', 'a2'])659 messagebus.trigger_event(defined_message, defined_endpoint, entity='a2', data='data1')660 self.assertEqual(calls.get(timeout=1), 'a2')661 messagebus.trigger_event(defined_message, defined_endpoint, entity='a1', data='data2')662 self.assertEqual(calls.get(timeout=1), 'a1')663 finally:664 dispatcher.destroy()665 def test_combination_of_fixated_and_not_fixated_entities(self):666 messagebus = create_messagebus()667 calls = Queue()668 @component(name='A')669 class a1():670 entity = 'a1'671 @component(name='A', can=['2'])672 class a2():673 entity = 'a2'674 @requires(a='A')675 @requires(a2='A', can=['2'], fixate_entities=False)676 def handle_message(message, a, a2):677 calls.put(a.entity)678 calls.put(a2.entity)679 dispatcher = CallbackDispatcher(messagebus, handle_message)680 try:681 dispatcher.register([defined_message], entities=['a1', 'a2'])682 messagebus.trigger_event(defined_message, defined_endpoint, entity='a2', data='data1')683 self.assertEqual(calls.get(timeout=1), 'a2')684 self.assertEqual(calls.get(timeout=1), 'a2')685 messagebus.trigger_event(defined_message, defined_endpoint, entity='a1', data='data2')686 self.assertEqual(calls.get(timeout=1), 'a1')687 self.assertEqual(calls.get(timeout=1), 'a2')688 finally:689 dispatcher.destroy()690class TestThreadPoolDispatcherWithMultipleWorkers(unittest.TestCase):691 def test_priority_is_passed_to_base_class(self):692 messagebus = create_messagebus()693 def handle_message(message):694 pass695 dispatcher = ThreadPoolDispatcher(messagebus, handle_message, priority=-7)696 self.assertEqual(dispatcher.priority, -7)697 def test_destroy_can_be_called_on_a_dispatcher_that_is_not_registered(self):698 messagebus = create_messagebus()699 def handle_message(message):700 pass701 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)702 dispatcher.destroy()703 def test_dispatcher_can_be_registered_and_deregistered_in_messagebus(self):704 messagebus = create_messagebus()705 def handle_message(message):706 pass707 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)708 try:709 dispatcher.register(message_ids=[defined_message])710 finally:711 dispatcher.destroy()712 def test_dispatcher_can_be_deregistered_in_multiple_calls(self):713 messagebus = create_messagebus()714 def handle_message(message):715 pass716 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)717 try:718 dispatcher.register(message_ids=[defined_message, defined_message2])719 dispatcher.deregister([defined_message])720 dispatcher.deregister([defined_message2])721 finally:722 dispatcher.destroy()723 def test_message_triggers_handle_message(self):724 messagebus = create_messagebus()725 received_messages = Queue()726 def handle_message(message):727 received_messages.put(message)728 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)729 try:730 dispatcher.register([defined_message])731 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')732 self.assertEqual(received_messages.get(timeout=1).data, 'data1')733 finally:734 dispatcher.destroy()735 def test_multiple_messages_triggers_handle_message(self):736 messagebus = create_messagebus()737 received_messages = Queue()738 def handle_message(message):739 received_messages.put(message)740 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)741 try:742 dispatcher.register([defined_message])743 messagebus.trigger_event(defined_message, defined_endpoint, data='data1')744 messagebus.trigger_event(defined_message, defined_endpoint, data='data2')745 self.assertIn(received_messages.get(timeout=1).data, ['data1', 'data2'])746 self.assertIn(received_messages.get(timeout=1).data, ['data1', 'data2'])747 finally:748 dispatcher.destroy()749 def test_concurrent_dispatcher_is_triggered_asynchronously(self):750 messagebus = create_messagebus()751 queue1 = Queue()752 queue2 = Queue()753 completed_queue = Queue()754 def handle_message(message):755 (put_queue, get_queue, value) = message.data756 put_queue.put(value)757 completed_queue.put(get_queue.get(timeout=1))758 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)759 try:760 dispatcher.register([defined_message])761 messagebus.trigger_event(defined_message, defined_endpoint, data=(queue1, queue2, '1'))762 messagebus.trigger_event(defined_message, defined_endpoint, data=(queue2, queue1, '2'))763 results = set()764 results.add(completed_queue.get(timeout=1))765 results.add(completed_queue.get(timeout=2))766 self.assertEqual(results, {'1', '2'})767 finally:768 dispatcher.destroy()769 def test_requests_trigger_handle_message(self):770 messagebus = create_messagebus()771 def handle_message(message):772 return message.data773 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)774 try:775 dispatcher.register([defined_message])776 fs = messagebus.send_request(defined_message, defined_endpoint, data='data')777 self.assertEqual(fs[0].result(timeout=1), 'data')778 finally:779 dispatcher.destroy()780 def test_multiple_requests_trigger_handle_message(self):781 messagebus = create_messagebus()782 def handle_message(message):783 return message.data784 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)785 try:786 dispatcher.register([defined_message])787 fs1 = messagebus.send_request(defined_message, defined_endpoint, data='data1')788 fs2 = messagebus.send_request(defined_message, defined_endpoint, data='data2')789 self.assertEqual(fs1[0].result(timeout=1), 'data1')790 self.assertEqual(fs2[0].result(timeout=1), 'data2')791 finally:792 dispatcher.destroy()793 def test_concurrent_dispatcher_requests_are_triggered_asynchronously(self):794 messagebus = create_messagebus()795 queue1 = Queue()796 queue2 = Queue()797 completed_queue = Queue()798 def handle_message(message):799 (put_queue, get_queue, value) = message.data800 put_queue.put(value)801 completed_queue.put(get_queue.get(timeout=1))802 return value803 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)804 try:805 dispatcher.register([defined_message])806 futures1 = messagebus.send_request(807 defined_message, defined_endpoint, data=(queue1, queue2, '1'))808 futures2 = messagebus.send_request(809 defined_message, defined_endpoint, data=(queue2, queue1, '2'))810 results = set()811 results.add(completed_queue.get(timeout=1))812 results.add(completed_queue.get(timeout=2))813 self.assertEqual(results, {'1', '2'})814 self.assertEqual(futures1[0].result(timeout=1), '1')815 self.assertEqual(futures2[0].result(timeout=1), '2')816 finally:817 dispatcher.destroy()818 def test_active_count_increased_during_message_handling(self):819 messagebus = create_messagebus()820 active_count = Queue()821 thread1_done = Queue()822 thread2_done = Queue()823 def handle_message(message):824 (put_queue, get_queue) = message.data825 active_count.put(dispatcher.get_active_count())826 put_queue.put(True)827 get_queue.get(timeout=1)828 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)829 try:830 dispatcher.register(message_ids=[defined_message])831 self.assertEqual(dispatcher.get_active_count(), 0)832 futures1 = messagebus.send_request(833 defined_message, defined_endpoint, data=(thread1_done, thread2_done))834 self.assertEqual(active_count.get(timeout=1), 1)835 futures2 = messagebus.send_request(836 defined_message, defined_endpoint, data=(thread2_done, thread1_done))837 self.assertEqual(active_count.get(timeout=1), 2)838 futures1.wait(timeout=1)[0].result(timeout=1)839 futures2.wait(timeout=1)[0].result(timeout=1)840 self.assertEqual(dispatcher.get_active_count(), 0)841 finally:842 dispatcher.destroy()843 def test_optional_dispatcher_is_not_registered_if_message_is_not_defined(self):844 messagebus = create_messagebus()845 active_count = Queue()846 def handle_message(message):847 active_count.put(dispatcher.get_active_count())848 try:849 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)850 dispatcher.register(message_ids=[notdefined_message], optional=True)851 self.assertEqual(dispatcher.get_active_count(), 0)852 finally:853 dispatcher.destroy()854 def test_optional_dispatcher_is_not_registered_if_endpoint_is_not_defined(self):855 messagebus = create_messagebus()856 active_count = Queue()857 def handle_message(message):858 active_count.put(dispatcher.get_active_count())859 try:860 dispatcher = ThreadPoolDispatcher(messagebus, handle_message)861 dispatcher.register(862 message_ids=[defined_message], endpoint_ids=[notdefined_endpoint], optional=True)863 self.assertEqual(dispatcher.get_active_count(), 0)864 finally:...

Full Screen

Full Screen

infobot.py

Source:infobot.py Github

copy

Full Screen

...13#Коля14@bot.message_handler(regexp="коля")15@bot.message_handler(regexp="никол")16@bot.message_handler(regexp="пес")17def handle_message(message):18 photo = open('photo/kolya.jpg', 'rb')19 if message.from_user.id == 456193135:20 bot.send_photo(456193135, photo)21 bot.send_message(456193135, 'https://vk.com/vky96')22 else:23 bot.send_photo(message.chat.id, photo)24 bot.send_message(message.chat.id, 'https://vk.com/vky96')25#Мирослав26@bot.message_handler(regexp="миросл")27@bot.message_handler(regexp="матыл")28@bot.message_handler(regexp="мотыл")29def handle_message(message):30 photo = open('photo/mir.jpg', 'rb')31 if message.from_user.id == 456193135:32 bot.send_photo(456193135, photo)33 bot.send_message(456193135, 'https://vk.com/id248493275')34 else:35 bot.send_photo(message.chat.id, photo)36 bot.send_message(message.chat.id, 'https://vk.com/id248493275')37#Архип38@bot.message_handler(regexp="архип")39@bot.message_handler(regexp="анто")40def handle_message(message):41 photo = open('photo/arh.jpg', 'rb')42 if message.from_user.id == 456193135:43 bot.send_photo(456193135, photo)44 bot.send_message(456193135, 'https://vk.com/id186287106')45 else:46 bot.send_photo(message.chat.id, photo)47 bot.send_message(message.chat.id, 'https://vk.com/id186287106')48#Гера49@bot.message_handler(regexp="герма")50@bot.message_handler(regexp="аста")51@bot.message_handler(regexp="гера")52def handle_message(message):53 photo = open('photo/gera.jpg', 'rb')54 if message.from_user.id == 456193135:55 bot.send_photo(456193135, photo)56 bot.send_message(456193135, 'https://vk.com/astap_g')57 else:58 bot.send_photo(message.chat.id, photo)59 bot.send_message(message.chat.id, 'https://vk.com/astap_g')60#Соня61@bot.message_handler(regexp="соня")62@bot.message_handler(regexp="софья")63@bot.message_handler(regexp="колдор")64def handle_message(message):65 photo = open('photo/sonya.jpg', 'rb')66 if message.from_user.id == 456193135:67 bot.send_photo(456193135, photo)68 bot.send_message(456193135, 'https://vk.com/id137993867')69 else:70 bot.send_photo(message.chat.id, photo)71 bot.send_message(message.chat.id, 'https://vk.com/id137993867')72#Игорь73@bot.message_handler(regexp="игорь")74@bot.message_handler(regexp="танаг")75def handle_message(message):76 photo = open('photo/igor.jpg', 'rb')77 if message.from_user.id == 456193135:78 bot.send_photo(456193135, photo)79 bot.send_message(456193135, 'https://vk.com/igr_tng')80 else:81 bot.send_photo(message.chat.id, photo)82 bot.send_message(message.chat.id, 'https://vk.com/igr_tng')83#Некит84@bot.message_handler(regexp="чж")85@bot.message_handler(regexp="некит")86@bot.message_handler(regexp="никит")87def handle_message(message):88 photo = open('photo/nekit.jpg', 'rb')89 if message.from_user.id == 456193135:90 bot.send_photo(456193135, photo)91 bot.send_message(456193135, 'https://vk.com/nekitazhao')92 else:93 bot.send_photo(message.chat.id, photo)94 bot.send_message(message.chat.id, 'https://vk.com/nekitazhao')95#Артем96@bot.message_handler(regexp="артем")97@bot.message_handler(regexp="мочал")98def handle_message(message):99 photo = open('photo/artem.jpg', 'rb')100 if message.from_user.id == 456193135:101 bot.send_photo(456193135, photo)102 bot.send_message(456193135, 'https://vk.com/temamochalov')103 else:104 bot.send_photo(message.chat.id, photo)105 bot.send_message(message.chat.id, 'https://vk.com/temamochalov')106#Алена107@bot.message_handler(regexp="ален")108@bot.message_handler(regexp="гулим")109def handle_message(message):110 photo = open('photo/alena.jpg', 'rb')111 if message.from_user.id == 456193135:112 bot.send_photo(456193135, photo)113 bot.send_message(456193135, 'https://vk.com/id171454612')114 else:115 bot.send_photo(message.chat.id, photo)116 bot.send_message(message.chat.id, 'https://vk.com/id171454612')117#Конопленко118@bot.message_handler(regexp="алин")119@bot.message_handler(regexp="конопл")120def handle_message(message):121 photo = open('photo/alina.jpg', 'rb')122 if message.from_user.id == 456193135:123 bot.send_photo(456193135, photo)124 bot.send_message(456193135, 'https://vk.com/id168970759')125 else:126 bot.send_photo(message.chat.id, photo)127 bot.send_message(message.chat.id, 'https://vk.com/id168970759')128#Машраб129@bot.message_handler(regexp="машр")130@bot.message_handler(regexp="ахма")131def handle_message(message):132 photo = open('photo/mashrab.jpg', 'rb')133 if message.from_user.id == 456193135:134 bot.send_photo(456193135, photo)135 bot.send_message(456193135, 'https://vk.com/id314553215')136 else:137 bot.send_photo(message.chat.id, photo)138 bot.send_message(message.chat.id, 'https://vk.com/id314553215')139#Макеев140@bot.message_handler(regexp="макее")141@bot.message_handler(regexp="дима")142@bot.message_handler(regexp="дмит")143@bot.message_handler(regexp="макеша")144def handle_message(message):145 photo = open('photo/dima.jpg', 'rb')146 if message.from_user.id == 456193135:147 bot.send_photo(456193135, photo)148 bot.send_message(456193135, 'https://vk.com/id193289545')149 else:150 bot.send_photo(message.chat.id, photo)151 bot.send_message(message.chat.id, 'https://vk.com/id193289545')152#Могилевец153@bot.message_handler(regexp="ваня")154@bot.message_handler(regexp="иван")155@bot.message_handler(regexp="могил")156@bot.message_handler(regexp="могел")157def handle_message(message):158 photo = open('photo/vanya.jpg', 'rb')159 if message.from_user.id == 456193135:160 bot.send_photo(456193135, photo)161 bot.send_message(456193135, 'https://vk.com/ivan_mogilevec')162 else:163 bot.send_photo(message.chat.id, photo)164 bot.send_message(message.chat.id, 'https://vk.com/ivan_mogilevec')165#Катя Андреева166@bot.message_handler(regexp="андре")167def handle_message(message):168 photo = open('photo/andreeva.jpg', 'rb')169 if message.from_user.id == 456193135:170 bot.send_photo(456193135, photo)171 bot.send_message(456193135, 'https://vk.com/id404808913')172 else:173 bot.send_photo(message.chat.id, photo)174 bot.send_message(message.chat.id, 'https://vk.com/id404808913')175#Катя Яблонская176@bot.message_handler(regexp="яблонс")177def handle_message(message):178 photo = open('photo/yabl.jpg', 'rb')179 if message.from_user.id == 456193135:180 bot.send_photo(456193135, photo)181 bot.send_message(456193135, 'https://vk.com/id482067881')182 else:183 bot.send_photo(message.chat.id, photo)184 bot.send_message(message.chat.id, 'https://vk.com/id482067881')185#Даша186@bot.message_handler(regexp="доро")187@bot.message_handler(regexp="даша")188@bot.message_handler(regexp="дарья")189def handle_message(message):190 photo = open('photo/dasha.jpg', 'rb')191 if message.from_user.id == 456193135:192 bot.send_photo(456193135, photo)193 bot.send_message(456193135, 'https://vk.com/id163148142')194 else:195 bot.send_photo(message.chat.id, photo)196 bot.send_message(message.chat.id, 'https://vk.com/id163148142')197#Власова198@bot.message_handler(regexp="власова")199def handle_message(message):200 photo = open('photo/dasha.jpg', 'rb')201 if message.from_user.id == 456193135:202 bot.send_photo(456193135, photo)203 bot.send_message(456193135, 'https://vk.com/id163148142')204 else:205 bot.send_photo(message.chat.id, photo)206 bot.send_message(message.chat.id, 'https://vk.com/id163148142')207#Кати208@bot.message_handler(regexp="Катя")209@bot.message_handler(regexp="Екатер")210def handle_message(message):211 if message.from_user.id == 456193135:212 bot.send_message(456193135, 'Фамилия?')213 else:214 bot.send_message(message.chat.id, 'Фамилия?')215@bot.message_handler(func=lambda message: True)216def upper(message):217 if message.from_user.id == 456193135:218 bot.send_message(456193135, 'ты админ')219 else:220 bot.send_message(message.chat.id, 'нет такого васичка')...

Full Screen

Full Screen

9218f99ba35da225ca11.py

Source:9218f99ba35da225ca11.py Github

copy

Full Screen

...33 basic interface that ``singledispatch`` does:34 35 >>> class A:36 ... @dispatchmethod37 ... def handle_message(self, message):38 ... # Fallback code...39 ... pass40 ... @handle_message.register(int)41 ... def _(self, message):42 ... # Special int handling code...43 ... pass44 ...45 >>> a = A()46 >>> a.handle_message(42)47 # Runs "Special int handling code..."48 49 Note that using ``singledispatch`` in these cases is impossible, since it tries to dispatch50 on the ``self`` argument, not the ``message`` argument. This is technically a double51 dispatch, since both the type of ``self`` and the type of the second argument are used to52 determine what function to call - for example:53 54 >>> class A:55 ... @dispatchmethod56 ... def handle_message(self, message):57 ... print('A other', message)58 ... pass59 ... @handle_message.register(int)60 ... def _(self, message):61 ... print('A int', message)62 ... pass63 ...64 >>> class B:65 ... @dispatchmethod66 ... def handle_message(self, message):67 ... print('B other', message)68 ... pass69 ... @handle_message.register(int)70 ... def _(self, message):71 ... print('B int', message)72 ...73 >>> def do_stuff(A_or_B):74 ... A_or_B.handle_message(42)75 ... A_or_B.handle_message('not an int')76 77 On one hand, either the ``dispatchmethod`` defined in ``A`` or ``B`` is used depending78 upon what object one passes to ``do_stuff()``, but on the other hand, ``do_stuff()``79 causes different versions of the dispatchmethod (found in either ``A`` or ``B``) 80 to be called (both the fallback and the ``int`` versions are implicitly called).81 82 Note that this should be fully compatable with ``singledispatch`` in any other respects83 (that is, it exposes the same attributes and methods).84 """85 dispatcher = singledispatch(func)86 def register(type, func=None):87 if func is not None:88 return dispatcher.register(type, func)89 else:90 def _register(func):91 return dispatcher.register(type)(func)92 93 return _register94 def dispatch(type):95 return dispatcher.dispatch(type)96 def wrapper(inst, dispatch_data, *args, **kwargs):97 cls = type(dispatch_data)98 impl = dispatch(cls)99 return impl(inst, dispatch_data, *args, **kwargs)100 wrapper.register = register101 wrapper.dispatch = dispatch102 wrapper.registry = dispatcher.registry103 wrapper._clear_cache = dispatcher._clear_cache104 update_wrapper(wrapper, func)105 return wrapper106class Foo:107 @dispatchmethod108 def handle_message(self, message):109 print('Unknown message:', message)110 @handle_message.register(A)111 def _(self, message):112 print('A:', message.x)113 @handle_message.register(B)114 def _(self, message):115 print('B:', message.y)116 @handle_message.register(C)117 def _(self, message):118 print('C:', message.z)119x = Foo()120x.handle_message(12)121x.handle_message(A('a'))122x.handle_message(B('b'))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run prospector automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful