|
28 | 28 | import aiofiles |
29 | 29 | import pyfakefs.fake_filesystem_unittest |
30 | 30 |
|
| 31 | +from pyfakefs.fake_filesystem_unittest import TestCase |
| 32 | + |
| 33 | +from helpful_modules.threads_or_useful_funcs import read_last_n_lines |
31 | 34 | from helpful_modules import threads_or_useful_funcs |
32 | 35 | from tests.mockable_aiofiles import MockableAioFiles |
33 | 36 | from tests.utils import generate_many_randoms |
@@ -354,7 +357,94 @@ def test_secure_fisher_yates_shuffle(self, mock_randbelow): |
354 | 357 | self.assertNotEqual(my_list, shuffled_list) |
355 | 358 | self.assertCountEqual(my_list, shuffled_list) |
356 | 359 | mock_randbelow.assert_called_with(2) |
| 360 | +import asyncio |
| 361 | +import concurrent.futures |
| 362 | +import unittest |
| 363 | + |
| 364 | +from helpful_modules.threads_or_useful_funcs import async_wait_for_future |
| 365 | + |
| 366 | + |
| 367 | +class TestAsyncWaitForFuture(unittest.IsolatedAsyncioTestCase): |
| 368 | + |
| 369 | + async def test_asyncio_future_returns_result(self): |
| 370 | + future = asyncio.Future() |
| 371 | + future.set_result("hello") |
| 372 | + |
| 373 | + result = await async_wait_for_future(future) |
| 374 | + |
| 375 | + self.assertEqual(result, "hello") |
| 376 | + |
| 377 | + async def test_asyncio_future_timeout(self): |
| 378 | + future = asyncio.Future() |
| 379 | + |
| 380 | + with self.assertRaises(asyncio.TimeoutError): |
| 381 | + await async_wait_for_future(future, timeout=0.01) |
| 382 | + |
| 383 | + async def test_concurrent_future_returns_result(self): |
| 384 | + future = concurrent.futures.Future() |
| 385 | + future.set_result(123) |
| 386 | + |
| 387 | + result = await async_wait_for_future(future, interval=0.001) |
| 388 | + |
| 389 | + self.assertEqual(result, 123) |
| 390 | + |
| 391 | + async def test_invalid_future_type(self): |
| 392 | + with self.assertRaises(TypeError): |
| 393 | + await async_wait_for_future("not a future") |
| 394 | +import asyncio |
| 395 | +import concurrent.futures |
| 396 | +import unittest |
| 397 | + |
| 398 | +from helpful_modules.threads_or_useful_funcs import async_wait_for_future |
| 399 | + |
| 400 | + |
| 401 | +class TestAsyncWaitForFuture(unittest.IsolatedAsyncioTestCase): |
357 | 402 |
|
| 403 | + async def test_asyncio_future_returns_result(self): |
| 404 | + future = asyncio.Future() |
| 405 | + future.set_result("hello") |
| 406 | + |
| 407 | + result = await async_wait_for_future(future) |
| 408 | + |
| 409 | + self.assertEqual(result, "hello") |
| 410 | + |
| 411 | + async def test_asyncio_future_timeout(self): |
| 412 | + future = asyncio.Future() |
| 413 | + |
| 414 | + with self.assertRaises(asyncio.TimeoutError): |
| 415 | + await async_wait_for_future(future, timeout=0.01) |
| 416 | + |
| 417 | + async def test_concurrent_future_returns_result(self): |
| 418 | + future = concurrent.futures.Future() |
| 419 | + future.set_result(123) |
| 420 | + |
| 421 | + result = await async_wait_for_future(future, interval=0.001) |
| 422 | + |
| 423 | + self.assertEqual(result, 123) |
| 424 | + |
| 425 | + async def test_invalid_future_type(self): |
| 426 | + with self.assertRaises(TypeError): |
| 427 | + await async_wait_for_future("not a future") |
| 428 | + |
| 429 | + |
| 430 | + |
| 431 | +class TestReadLastNLines(TestCase, unittest.IsolatedAsyncioTestCase): |
| 432 | + |
| 433 | + def setUp(self): |
| 434 | + self.setUpPyfakefs() |
| 435 | + |
| 436 | + async def test_reads_last_lines(self): |
| 437 | + self.fs.create_file( |
| 438 | + "/tmp/test.txt", |
| 439 | + contents="line1\nline2\nline3\nline4\n" |
| 440 | + ) |
| 441 | + |
| 442 | + result = await read_last_n_lines("/tmp/test.txt", 2) |
| 443 | + |
| 444 | + self.assertEqual( |
| 445 | + result, |
| 446 | + ["line3", "line4"] |
| 447 | + ) |
358 | 448 |
|
359 | 449 | if __name__ == "__main__": |
360 | 450 | unittest.main() |
0 commit comments