import unittest from async import * class AsyncCanTurnAsyncIntoSyncFunction(unittest.TestCase): def test_turn_async_to_sync(self): @force_sync async def test(): import asyncio await asyncio.sleep(0.1) return 1 + 2 self.assertEqual(test(), 3) def test_turn_sync_to_sync(self): @force_sync def test(): return 1 + 2 self.assertEqual(test(), 3) class AsyncCanTurnSyncIntoAsyncFunction(unittest.TestCase): def test_turn_sync_to_async(self): @force_async def test(): import time time.sleep(1) return True @force_sync async def main(): import asyncio # if it were to execute sequentially, it would take 10 seconds, in this case we expect to see only 1 second futures = list(map(lambda x: test(), range(10))) return await asyncio.gather(*futures) import time # take the elapsed time start = time.time() res = main() end = time.time() elapsed = end - start self.assertEqual(len(res), 10) self.assertLess(elapsed, 1.2) # a little more than 1 second is normal