1748
assert subscription.is_alive() is False
1749
assert started.wait(timeout=2)
1750
1751
+ def test_has_errored_ignores_socket_flag_while_pysher_retries(self, subscription):
1752
+ """A recoverable drop is not an error while pysher keeps retrying.
1753
+
1754
+ websocket-client's ``has_errored`` stays set through pysher's
1755
+ reconnect backoff, so ``has_errored()`` must not consult it: only
1756
+ the pysher connection thread (alive across reconnects) decides.
1757
+ """
1758
+ connection = Mock()
1759
+ connection.is_alive.return_value = True
1760
+ connection.socket = Mock()
1761
+ connection.socket.has_errored = True
1762
+ subscription._connection = connection
1763
+
1764
+ assert subscription.has_errored() is False
1765
+
1766
+ def test_has_errored_when_pysher_thread_died(self, subscription):
1767
+ """Once pysher gives up for good, the connection counts as errored.
1768
+
1769
+ After an unrecoverable ``pusher:error`` (4000-4099) pysher
1770
+ disconnects and its connection thread exits; websocket-client's
1771
+ flag may never have been set, but ``has_errored()`` must still
1772
+ report the dead connection.
1773
+ """
1774
+ connection = Mock()
1775
+ connection.is_alive.return_value = False
1776
+ connection.socket = Mock()
1777
+ connection.socket.has_errored = False
1778
+ subscription._connection = connection
1779
+
1780
+ assert subscription.has_errored() is True
1781
+
1782
+ def test_wait_forever_returns_when_pysher_gives_up(self, subscription):
1783
+ """``wait_forever`` unblocks once the pysher thread is gone."""
1784
+ connection = Mock()
1785
+ connection.is_alive.return_value = False
1786
+ subscription._connection = connection
1787
+
1788
+ waiter = threading.Thread(target=subscription.wait_forever, daemon=True)
1789
+ waiter.start()
1790
+ try:
1791
+ waiter.join(timeout=2)
1792
+ assert not waiter.is_alive()
1793
+ finally:
1794
+ subscription._alive = False
1795
+ waiter.join(timeout=2)
1796
+
1797
+ def test_restart_preserves_pysher_worker(self, subscription):
1798
+ """Manual reconnect keeps the real worker and subscription wait alive."""
1799
+ connected = threading.Event()
1800
+ reconnected = threading.Event()
1801
+ closed = threading.Event()
1802
+ done = threading.Event()
1803
+ attempts = []
1804
+
1805
+ class Socket:
1806
+ has_errored = False
1807
+
1808
+ def run_forever(self, **kwargs):
1809
+ closed.clear()
1810
+ attempts.append(threading.current_thread())
1811
+ (connected if len(attempts) == 1 else reconnected).set()
1812
+ closed.wait(timeout=5)
1813
+
1814
+ def close(self):
1815
+ closed.set()
1816
+
1817
+ connection = triggers_module.PusherConnection(
1818
+ event_handler=Mock(), url="ws://localhost", reconnect_interval=0
1819
+ )
1820
+ subscription._connection = connection
1821
+ waiter = threading.Thread(
1822
+ target=lambda: (subscription.wait_forever(), done.set()), daemon=True
1823
+ )
1824
+ restarter = threading.Thread(target=subscription.restart, daemon=True)
1825
+ with patch("pysher.connection.websocket.WebSocketApp", return_value=Socket()):
1826
+ connection.start()
1827
+ try:
1828
+ assert connected.wait(timeout=2)
1829
+ waiter.start()
1830
+ restarter.start()
1831
+ assert reconnected.wait(timeout=2)
1832
+ assert subscription.has_errored() is False
1833
+ assert attempts[:2] == [connection, connection]
1834
+ restarter.join(timeout=2)
1835
+ assert not restarter.is_alive()
1836
+ assert not done.wait(timeout=1.1)
1837
+ # Terminal worker death must end a wait already in progress.
1838
+ connection.disconnect(timeout=2)
1839
+ assert done.wait(timeout=2)
1840
+ finally:
1841
+ subscription._alive = False
1842
+ connection.disconnect(timeout=2)
1843
+ restarter.join(timeout=2)
1844
+ if waiter.ident is not None:
1845
+ waiter.join(timeout=2)
1846
+
1847
+ def test_wait_forever_survives_reconnect_backoff(self, subscription):
1848
+ """``wait_forever`` does not return early on a recoverable drop.
1849
+
1850
+ The pysher thread stays alive through its reconnect backoff even
1851
+ though the socket flagged an error; the wait must survive until
1852
+ ``stop()`` clears ``_alive``.
1853
+ """
1854
+ connection = Mock()
1855
+ connection.is_alive.return_value = True
1856
+ connection.socket = Mock()
1857
+ connection.socket.has_errored = True
1858
+ subscription._connection = connection
1859
+
1860
+ done = threading.Event()
1861
+ waiter = threading.Thread(
1862
+ target=lambda: (subscription.wait_forever(), done.set()),
1863
+ daemon=True,
1864
+ )
1865
+ waiter.start()
1866
+
1867
+ assert not done.wait(timeout=2.5)
1868
+ subscription.stop()
1869
+ assert done.wait(timeout=5)
1870
+
1871
1872
class TestSubscriptionBuilderConnectTimeout:
1873
"""Tests for _SubcriptionBuilder.connect timeout teardown."""