|
8 | 8 |
|
9 | 9 |
|
10 | 10 | @pytest.mark.asyncio |
11 | | -async def test_restart_broadcasts_notice_stops_polling_and_execs(): |
| 11 | +async def test_restart_broadcasts_notice_and_execs(): |
12 | 12 | bot = MagicMock() |
13 | 13 | bot.send_message = AsyncMock() |
14 | | - dp = MagicMock() |
15 | | - dp.stop_polling = AsyncMock() |
16 | 14 | store = MagicMock() |
17 | 15 | store.get_all_chat_ids.return_value = [1, 2] |
18 | 16 |
|
19 | | - with patch("src.restart.os.execv") as execv, \ |
20 | | - patch("src.restart.asyncio.sleep", new=AsyncMock()): |
21 | | - await restart_bot(bot, dp, store, notice="restarting") |
| 17 | + with patch("src.restart.os.execv") as execv: |
| 18 | + await restart_bot(bot, store, notice="restarting") |
22 | 19 |
|
23 | 20 | assert bot.send_message.await_count == 2 |
24 | | - dp.stop_polling.assert_awaited_once() |
25 | 21 | execv.assert_called_once() |
26 | 22 |
|
27 | 23 |
|
28 | 24 | @pytest.mark.asyncio |
29 | 25 | async def test_restart_without_notice_skips_broadcast(): |
30 | 26 | bot = MagicMock() |
31 | 27 | bot.send_message = AsyncMock() |
32 | | - dp = MagicMock() |
33 | | - dp.stop_polling = AsyncMock() |
34 | 28 | store = MagicMock() |
35 | 29 | store.get_all_chat_ids.return_value = [1, 2] |
36 | 30 |
|
37 | | - with patch("src.restart.os.execv") as execv, \ |
38 | | - patch("src.restart.asyncio.sleep", new=AsyncMock()): |
39 | | - await restart_bot(bot, dp, store) |
| 31 | + with patch("src.restart.os.execv") as execv: |
| 32 | + await restart_bot(bot, store) |
40 | 33 |
|
41 | 34 | bot.send_message.assert_not_awaited() |
42 | | - dp.stop_polling.assert_awaited_once() |
| 35 | + execv.assert_called_once() |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.asyncio |
| 39 | +async def test_restart_does_not_stop_polling_or_close_session(): |
| 40 | + """Regression: stopping polling/closing the session unwinds main()'s loop |
| 41 | + and cancels this coroutine before execv runs (the restart silently becomes |
| 42 | + a clean exit). restart_bot must touch neither.""" |
| 43 | + bot = MagicMock() |
| 44 | + bot.send_message = AsyncMock() |
| 45 | + bot.session = MagicMock() |
| 46 | + bot.session.close = AsyncMock() |
| 47 | + store = MagicMock() |
| 48 | + store.get_all_chat_ids.return_value = [1] |
| 49 | + |
| 50 | + with patch("src.restart.os.execv") as execv: |
| 51 | + await restart_bot(bot, store, notice="hi") |
| 52 | + |
| 53 | + bot.session.close.assert_not_awaited() |
43 | 54 | execv.assert_called_once() |
44 | 55 |
|
45 | 56 |
|
46 | 57 | @pytest.mark.asyncio |
47 | 58 | async def test_restart_swallows_send_failures(): |
48 | 59 | bot = MagicMock() |
49 | 60 | bot.send_message = AsyncMock(side_effect=Exception("network")) |
50 | | - dp = MagicMock() |
51 | | - dp.stop_polling = AsyncMock() |
52 | 61 | store = MagicMock() |
53 | 62 | store.get_all_chat_ids.return_value = [1] |
54 | 63 |
|
55 | | - with patch("src.restart.os.execv") as execv, \ |
56 | | - patch("src.restart.asyncio.sleep", new=AsyncMock()): |
57 | | - await restart_bot(bot, dp, store, notice="hi") |
| 64 | + with patch("src.restart.os.execv") as execv: |
| 65 | + await restart_bot(bot, store, notice="hi") |
58 | 66 |
|
59 | 67 | # A failed broadcast must not prevent the restart |
60 | | - dp.stop_polling.assert_awaited_once() |
61 | 68 | execv.assert_called_once() |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.asyncio |
| 72 | +async def test_restart_execs_current_module(): |
| 73 | + bot = MagicMock() |
| 74 | + store = MagicMock() |
| 75 | + store.get_all_chat_ids.return_value = [] |
| 76 | + |
| 77 | + with patch("src.restart.os.execv") as execv: |
| 78 | + await restart_bot(bot, store) |
| 79 | + |
| 80 | + args = execv.call_args.args |
| 81 | + # os.execv(python, [python, "-m", "src.main"]) |
| 82 | + assert args[1][-2:] == ["-m", "src.main"] |
0 commit comments