Best Python code snippet using localstack_python
test_client_async_await.py
Source:test_client_async_await.py
...41 await asyncio.sleep(0.2)42 if msg.reply != "":43 await nc.publish(msg.reply, msg.data * 2)44 await nc.subscribe("foo", cb=handler_foo)45 async def handler_bar(msg):46 msgs.append(msg)47 if msg.reply != "":48 await nc.publish(msg.reply, b'')49 await nc.subscribe("bar", cb=handler_bar)50 await nc.publish("foo", b'1')51 await nc.publish("foo", b'2')52 await nc.publish("foo", b'3')53 # Will be processed before the others since no head of line54 # blocking among the subscriptions.55 await nc.publish("bar", b'4')56 response = await nc.request("foo", b'hello1', timeout=1)57 self.assertEqual(response.data, b'hello1hello1')58 with self.assertRaises(TimeoutError):59 await nc.request("foo", b'hello2', timeout=0.1)60 await nc.publish("bar", b'5')61 response = await nc.request("foo", b'hello2', timeout=1)62 self.assertEqual(response.data, b'hello2hello2')63 self.assertEqual(msgs[0].data, b'1')64 self.assertEqual(msgs[1].data, b'4')65 self.assertEqual(msgs[2].data, b'2')66 self.assertEqual(msgs[3].data, b'3')67 self.assertEqual(msgs[4].data, b'hello1')68 self.assertEqual(msgs[5].data, b'hello2')69 self.assertEqual(len(errors), 0)70 await nc.close()71 @async_test72 async def test_subscription_slow_consumer_pending_msg_limit(self):73 nc = NATS()74 msgs = []75 errors = []76 async def error_handler(e):77 if type(e) is SlowConsumerError:78 errors.append(e)79 await nc.connect(error_cb=error_handler)80 async def handler_foo(msg):81 await asyncio.sleep(0.2)82 msgs.append(msg)83 if msg.reply != "":84 await nc.publish(msg.reply, msg.data * 2)85 await nc.subscribe("foo", cb=handler_foo, pending_msgs_limit=5)86 async def handler_bar(msg):87 msgs.append(msg)88 if msg.reply != "":89 await nc.publish(msg.reply, msg.data * 3)90 await nc.subscribe("bar", cb=handler_bar)91 for i in range(10):92 await nc.publish("foo", f'{i}'.encode())93 # Will be processed before the others since no head of line94 # blocking among the subscriptions.95 await nc.publish("bar", b'14')96 response = await nc.request("bar", b'hi1', 2)97 self.assertEqual(response.data, b'hi1hi1hi1')98 self.assertEqual(len(msgs), 2)99 self.assertEqual(msgs[0].data, b'14')100 self.assertEqual(msgs[1].data, b'hi1')101 # Consumed messages but the rest were slow consumers.102 self.assertTrue(4 <= len(errors) <= 5)103 for e in errors:104 self.assertEqual(type(e), SlowConsumerError)105 self.assertIn("foo", str(e))106 self.assertNotIn("bar", str(e))107 self.assertEqual(errors[0].sid, 1)108 await nc.close()109 @async_test110 async def test_subscription_slow_consumer_pending_bytes_limit(self):111 nc = NATS()112 msgs = []113 errors = []114 async def error_handler(e):115 if type(e) is SlowConsumerError:116 errors.append(e)117 await nc.connect(error_cb=error_handler)118 async def handler_foo(msg):119 await asyncio.sleep(0.2)120 msgs.append(msg)121 if msg.reply != "":122 await nc.publish(msg.reply, msg.data * 2)123 await nc.subscribe("foo", cb=handler_foo, pending_bytes_limit=10)124 async def handler_bar(msg):125 msgs.append(msg)126 if msg.reply != "":127 await nc.publish(msg.reply, msg.data * 3)128 await nc.subscribe("bar", cb=handler_bar)129 for i in range(10):130 await nc.publish("foo", f"AAA{i}".encode())131 # Will be processed before the others since no head of line132 # blocking among the subscriptions.133 await nc.publish("bar", b'14')134 response = await nc.request("bar", b'hi1', 2)135 self.assertEqual(response.data, b'hi1hi1hi1')136 self.assertEqual(len(msgs), 2)137 self.assertEqual(msgs[0].data, b'14')138 self.assertEqual(msgs[1].data, b'hi1')...
test_dispatcher.py
Source:test_dispatcher.py
...47 def test_handler_dispatcher(self):48 router = Router(dispatcher=handler_dispatcher())49 def handler_foo(_request: Request) -> Response:50 return Response("ok")51 def handler_bar(_request: Request, bar, baz) -> Dict[str, any]:52 response = Response()53 response.set_json({"bar": bar, "baz": baz})54 return response55 router.add("/foo", handler_foo)56 router.add("/bar/<int:bar>/<baz>", handler_bar)57 assert router.dispatch(Request("GET", "/foo")).data == b"ok"58 assert router.dispatch(Request("GET", "/bar/420/ed")).json == {"bar": 420, "baz": "ed"}59 with pytest.raises(NotFound):60 assert router.dispatch(Request("GET", "/bar/asfg/ed"))61 def test_handler_dispatcher_invalid_signature(self):62 router = Router(dispatcher=handler_dispatcher())63 def handler(_request: Request, arg1) -> Response: # invalid signature64 return Response("ok")65 router.add("/foo/<arg1>/<arg2>", handler)...
server.py
Source:server.py
...13 time.sleep(.075 + random.random() * .05)14 self.send_response(200)15 self.end_headers()16 self.wfile.write(b"Handled foo")17def handler_bar(self):18 logging.info("Handling bar...")19 time.sleep(.15 + random.random() * .1)20 self.send_response(200)21 self.end_headers()22 self.wfile.write(b"Handled bar")23ROUTES = {24 "/api/foo": handler_foo,25 "/api/bar": handler_bar,26}27class Handler(BaseHTTPRequestHandler):28 def do_GET(self):29 ROUTES.get(self.path, handler_404)(self)30class MultiThreadedHTTPServer(ThreadingMixIn, HTTPServer):31 pass...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!