|
| 1 | +import threading |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from src.practical.account_pool import AccountPool |
| 6 | + |
| 7 | + |
| 8 | +def test_account_pool_init(): |
| 9 | + accounts = ["account1", "account2", "account3"] |
| 10 | + pool = AccountPool(accounts) |
| 11 | + |
| 12 | + assert pool.size() == 3 |
| 13 | + assert pool.used_accounts() == [] |
| 14 | + |
| 15 | + |
| 16 | +def test_get_account(): |
| 17 | + accounts = ["account1", "account2", "account3"] |
| 18 | + pool = AccountPool(accounts) |
| 19 | + |
| 20 | + account = pool.get_account() |
| 21 | + assert account == "account1" # PriorityQueue returns items in the order they were added |
| 22 | + assert account in pool.used_accounts() |
| 23 | + |
| 24 | + |
| 25 | +def test_release_account(): |
| 26 | + accounts = ["account1", "account2", "account3"] |
| 27 | + pool = AccountPool(accounts) |
| 28 | + |
| 29 | + account = pool.get_account() |
| 30 | + assert account in pool.used_accounts() |
| 31 | + |
| 32 | + pool.release_account(account) |
| 33 | + assert account not in pool.used_accounts() |
| 34 | + |
| 35 | + |
| 36 | +def test_accounts_ordered_by_priority(): |
| 37 | + accounts = ["account1", "account2", "account3"] |
| 38 | + pool = AccountPool(accounts) |
| 39 | + |
| 40 | + account1 = pool.get_account() |
| 41 | + assert account1 == "account1" |
| 42 | + |
| 43 | + account2 = pool.get_account() |
| 44 | + assert account2 == "account2" |
| 45 | + |
| 46 | + pool.release_account(account1) |
| 47 | + account3 = pool.get_account() |
| 48 | + assert account3 == "account3" |
| 49 | + |
| 50 | + account4 = pool.get_account() |
| 51 | + assert account4 == "account1" |
| 52 | + |
| 53 | + |
| 54 | +def test_block_until_account_available(): |
| 55 | + accounts = ["account1"] |
| 56 | + pool = AccountPool(accounts, timeout=1) |
| 57 | + |
| 58 | + account = pool.get_account() |
| 59 | + assert account == "account1" |
| 60 | + assert account in pool.used_accounts() |
| 61 | + |
| 62 | + result = [] |
| 63 | + thread = threading.Thread(target=lambda: result.append(pool.get_account()), daemon=True) |
| 64 | + thread.start() |
| 65 | + thread.join(timeout=0.01) |
| 66 | + assert thread.is_alive() # Priority Queue should be blocking call |
| 67 | + assert result == [] |
| 68 | + |
| 69 | + pool.release_account("account1") |
| 70 | + thread.join(timeout=0.1) |
| 71 | + assert not thread.is_alive() |
| 72 | + assert result == ["account1"] |
| 73 | + assert account in pool.used_accounts() |
| 74 | + |
| 75 | + |
| 76 | +def test_timeout(): |
| 77 | + accounts = ["account1"] |
| 78 | + pool = AccountPool(accounts, timeout=0.01) |
| 79 | + |
| 80 | + account = pool.get_account() |
| 81 | + assert account in accounts |
| 82 | + |
| 83 | + with pytest.raises(AccountPool.AccountPoolTimeoutError): |
| 84 | + pool.get_account() |
| 85 | + |
| 86 | + |
| 87 | +def test_size(): |
| 88 | + pool = AccountPool([]) |
| 89 | + assert pool.size() == 0 |
| 90 | + |
| 91 | + pool.add_account("account1") |
| 92 | + assert pool.size() == 1 |
0 commit comments