def test_get_rdns_privex_ns1_host(self): """Test resolving rDNS for the domains ``steemseed-fin.privex.io`` and ``ns1.privex.io``""" self.assertEqual(loop_run(helpers.get_rdns_async('ns1.privex.io')), 'ns1.privex.io') self.assertEqual( loop_run(helpers.get_rdns_async('steemseed-fin.privex.io')), 'hiveseed-fin.privex.io')
def test_awaitable_class(self): """Test :func:`.awaitable_class` converts a class - allowing async functions to be called synchronously""" class ExampleAsyncCls: async def example_async(self): return "hello world" def example_sync(self): return "hello world" wrapped_cls = helpers.awaitable_class(ExampleAsyncCls) self.assertEqual(ExampleAsyncCls.__name__, wrapped_cls.__name__) self.assertEqual(ExampleAsyncCls.__qualname__, wrapped_cls.__qualname__) self.assertEqual(ExampleAsyncCls.__module__, wrapped_cls.__module__) exm_inst = ExampleAsyncCls() wrp_inst = wrapped_cls() self.assertEqual(exm_inst.example_sync(), 'hello world') # Confirm that ExampleAsyncCls.example_async returns a coroutine, and await it using loop_run + f_await exm_async = exm_inst.example_async() self.assertTrue(inspect.iscoroutine(exm_async)) self.assertEqual(helpers.loop_run(f_await(exm_async)), 'hello world') self.assertEqual(wrp_inst.example_sync(), 'hello world') # Confirm on the wrapped class that example_async() can be ran synchronously self.assertEqual(wrp_inst.example_async(), 'hello world') # Confirm that example_async() still returns a coroutine when called from an async context, # then await it using loop_run + f_await self.assertEqual(helpers.loop_run(f_await(wrp_inst.example_async)), 'hello world')
def test_base_check_host_async_send(self): http_req = b"GET / HTTP/1.1\n\n" self.assertTrue( loop_run(netbase.check_host_async, 'files.privex.io', 80, send=http_req)) self.assertFalse( loop_run(netbase.check_host_async, 'files.privex.io', 9991))
def test_resolve_ip_v4_convert(self): """Test :func:`.resolve_ip` returns an IPv6-wrapped IPv4 address for ``microsoft.com`` when v4_convert is enabled + v6 version""" ip = loop_run( helpers.resolve_ip_async('microsoft.com', 'v6', v4_convert=True)) if ip is None: return pytest.skip( f"Skipping test TestNetResolveIP.test_resolve_ip_v4_convert as v6-wrapped IPv4 addresses " f"aren't supported on this platform.") self.assertTrue(ip.startswith('::ffff:'))
def test_awaitable_class_decorator(self): @helpers.awaitable_class class ExampleAsyncCls: async def example_async(self): return "hello world" def example_sync(self): return "hello world" wrp_inst = ExampleAsyncCls() # Confirm both example_sync and example_async work synchronously and return correct data self.assertEqual(wrp_inst.example_sync(), 'hello world') self.assertEqual(wrp_inst.example_async(), 'hello world') # Call example_async() from f_await (a proper async context) async_coro = f_await(wrp_inst.example_async) self.assertEqual(helpers.loop_run(async_coro), 'hello world')
def test_awaitable_mixin(self): """Test :class:`.AwaitableMixin` when sub-classed enables async methods to be called synchronously""" class ExampleAsyncCls(helpers.AwaitableMixin): async def example_async(self): return "hello world" def example_sync(self): return "hello world" wrp_inst = ExampleAsyncCls() # Confirm both example_sync and example_async work synchronously and return correct data self.assertEqual(wrp_inst.example_sync(), 'hello world') self.assertEqual(wrp_inst.example_async(), 'hello world') # Call example_async() from f_await (a proper async context) async_coro = f_await(wrp_inst.example_async) self.assertEqual(helpers.loop_run(async_coro), 'hello world')
def test_run_coro_thread_async(self): """ Test :func:`.run_coro_thread_async` using :func:`asyncio.gather` to confirm coroutines with blocking synchronous code are being ran simultaneously despite their blocking synchronous code. """ rcta = helpers.run_coro_thread_async async def _hello(world): # time.sleep is used in-place of a synchronous blocking function time.sleep(1) return world * 10 async def hello(): return await asyncio.gather(rcta(_hello, 5), rcta(_hello, 20), rcta(_hello, 2), rcta(_hello, 80)) start = datetime.utcnow() res = helpers.loop_run(hello()) end = datetime.utcnow() # After running 4 instances of _hello using run_coro_thread_async, check that no more than # 2 seconds have passed - if they were ran synchronously, they would've taken 4 or more seconds. self.assertLessEqual((end - start).total_seconds(), 2) self.assertListEqual(res, [50, 200, 20, 800])
def test_resolve_ips_ipv4_addr_invalid(self): """Test :func:`.resolve_ips` raises :class:`.AttributeError` when ``version`` is v4 but an IPv6 address was passed""" with self.assertRaises(AttributeError): loop_run(helpers.resolve_ips_async('2a07:e00::333', 'v4'))
def _resolve_multi(self, *addr, version='any', v4_convert=False): return loop_run( self._resolve_multi_async(*addr, version=version, v4_convert=v4_convert))
def test_resolve_ips_ipv6_addr_invalid(self): """Test :func:`.resolve_ips` raises :class:`.AttributeError` when ``version`` is v6 but an IPv4 address was passed""" with self.assertRaises(AttributeError): loop_run(helpers.resolve_ips_async('185.130.44.5', 'v6'))
def test_base_check_host_async_throw(self): with self.assertRaises(ConnectionRefusedError): loop_run(netbase.check_host_async, 'files.privex.io', 9991, throw=True)
def test_get_rdns_no_rdns_records(self): """Test :func:`.get_rdns` raises :class:`.ReverseDNSNotFound` when given a valid IP that has no rDNS records""" with self.assertRaises(helpers.ReverseDNSNotFound): loop_run(helpers.get_rdns_async('192.168.5.1'))
def test_base_check_host_async(self): self.assertTrue( loop_run(netbase.check_host_async, 'hiveseed-se.privex.io', 2001)) self.assertFalse( loop_run(netbase.check_host_async, 'hiveseed-se.privex.io', 9991))
def test_resolve_ips_hiveseed_v4(self): """Test :func:`.resolve_ips` returns only v4 for ``hiveseed-fin.privex.io`` when version is set to v4""" ips = loop_run( helpers.resolve_ips_async('hiveseed-fin.privex.io', 'v4')) self.assertEqual(len(ips), 1) self.assertEqual(ips[0], '95.216.3.171')
def test_get_rdns_invalid_domain(self): """Test :func:`.get_rdns` raises :class:`.InvalidHost` when given a non-existent domain""" with self.assertRaises(helpers.InvalidHost): loop_run(helpers.get_rdns_async('non-existent.domain.example'))
def test_resolve_ips_ipv6_addr(self): """Test :func:`.resolve_ips` returns the same IPv6 address passed to it""" ips = loop_run(helpers.resolve_ips_async('2a07:e00::333')) self.assertEqual(len(ips), 1) self.assertEqual(ips[0], '2a07:e00::333')
def test_resolve_ips_v4_convert_false(self): """Test :func:`.resolve_ips` returns an empty list for ``microsoft.com`` when v6 requested without v4_convert""" ips = loop_run( helpers.resolve_ips_async('microsoft.com', 'v6', v4_convert=False)) self.assertEqual(len(ips), 0)
def test_resolve_ip_hiveseed_v6(self): """Test :func:`.resolve_ip` returns only v6 for ``hiveseed-fin.privex.io`` when version is v6""" self.assertEqual( loop_run(helpers.resolve_ip_async('hiveseed-fin.privex.io', 'v6')), '2a01:4f9:2a:3d4::2')
def test_resolve_ip_hiveseed_v4(self): """Test :func:`.resolve_ip` returns only v4 for ``hiveseed-fin.privex.io`` when version is v4""" self.assertEqual( loop_run(helpers.resolve_ip_async('hiveseed-fin.privex.io', 'v4')), '95.216.3.171')
def test_resolve_ip_hiveseed(self): """Test :func:`.resolve_ip` returns expected either correct v4 or v6 for ``hiveseed-fin.privex.io``""" self.assertIn( loop_run(helpers.resolve_ip_async('hiveseed-fin.privex.io')), ['95.216.3.171', '2a01:4f9:2a:3d4::2'])
def test_get_rdns_privex_ns1_ip(self): """Test resolving IPv4 and IPv6 addresses into ns1.privex.io""" self.assertEqual(loop_run(helpers.get_rdns_async('2a07:e00::100')), 'ns1.privex.io') self.assertEqual(loop_run(helpers.get_rdns_async('185.130.44.3')), 'ns1.privex.io')
def test_resolve_ips_ipv4_addr(self): """Test :func:`.resolve_ips` returns the same IPv4 address passed to it""" ips = loop_run(helpers.resolve_ips_async('185.130.44.5')) self.assertEqual(len(ips), 1) self.assertEqual(ips[0], '185.130.44.5')