1818
assert result is builder.subscription
1819
pusher.disconnect.assert_not_called()
1820
1821
+ def test_connect_raises_recorded_failure_fast(self):
1822
+ """An establish-time failure raises promptly instead of timing out.
1823
+
1824
+ The connection handler records the failure (for example a channel-auth
1825
+ rejection raised inside ``pusher.subscribe()``); the wait loop must
1826
+ surface it on its next poll and tear down the pusher, not spin until
1827
+ the deadline and report a generic timeout.
1828
+ """
1829
+ pusher = Mock()
1830
+ builder = self._make_builder(pusher)
1831
+ failure = RuntimeError("auth rejected")
1832
+ # The connection handler records the failure; on the mocked
1833
+ # subscription this is the wait loop's re-raise hook.
1834
+ builder.subscription._raise_on_connection_error.side_effect = failure
1835
+ builder._get_connection_handler = Mock( # type: ignore[method-assign]
1836
+ return_value=lambda *a, **k: None
1837
+ )
1838
+
1839
+ started = time.monotonic()
1840
+ with (
1841
+ patch.object(
1842
+ _SubcriptionBuilder, "_get_pusher_instance", return_value=pusher
1843
+ ),
1844
+ pytest.raises(RuntimeError, match="auth rejected"),
1845
+ ):
1846
+ builder.connect(timeout=15.0)
1847
+
1848
+ assert time.monotonic() - started < 5.0
1849
+ pusher.disconnect.assert_called_once()
1850
+
1851
1852
class TestSubscriptionErrorHandler:
1853
"""Tests for TriggerSubscription._handle_subscription_error."""
1885
"""Without a registered callback the frame is only logged."""
1886
subscription._handle_subscription_error('{"error": "auth failed"}')
1887
1888
+ def test_fail_subscription_invokes_callback_and_records_error(self, subscription):
1889
+ """Establish-time failures reach the callback as ``{'error': ...}``."""
1890
+ callback = Mock()
1891
+ subscription._on_subscription_error = callback
1892
+
1893
+ subscription._fail_subscription(RuntimeError("auth rejected"))
1894
+
1895
+ callback.assert_called_once_with({"error": "auth rejected"})
1896
+ assert subscription._connection_error is not None
1897
+
1898
+ def test_fail_subscription_without_callback_only_records(self, subscription):
1899
+ """The failure is recorded even when no callback is registered."""
1900
+ subscription._fail_subscription(RuntimeError("auth rejected"))
1901
+
1902
+ assert subscription._connection_error is not None
1903
+
1904
+ def test_fail_subscription_contains_callback_exception(self, subscription):
1905
+ """A faulty callback cannot break the failure path."""
1906
+ subscription._on_subscription_error = Mock(side_effect=RuntimeError("boom"))
1907
+
1908
+ subscription._fail_subscription(RuntimeError("auth rejected"))
1909
+
1910
+ assert subscription._connection_error is not None
1911
+
1912
+ def test_raise_on_connection_error_raises_recorded_error(self, subscription):
1913
+ """The wait loop re-raises the recorded failure."""
1914
+ subscription._fail_subscription(RuntimeError("auth rejected"))
1915
+
1916
+ with pytest.raises(RuntimeError, match="auth rejected"):
1917
+ subscription._raise_on_connection_error()
1918
+
1919
+ def test_raise_on_connection_error_is_noop_when_healthy(self, subscription):
1920
+ """No recorded failure means the wait loop proceeds normally."""
1921
+ subscription._raise_on_connection_error()
1922
+
1923
def test_connection_handler_binds_subscription_error(self):
1924
"""The builder binds ``pusher:subscription_error`` to the handler."""
1925
client = Mock()
1948
== builder.subscription._handle_chunked_events
1949
)
1950
1951
+ def test_connection_handler_routes_subscribe_failure_to_error_path(self):
1952
+ """Auth failures inside ``pusher.subscribe()`` reach the error path.
1953
+
1954
+ pysher performs the channel-auth request synchronously inside
1955
+ ``subscribe()``; without this routing the callback is skipped and the
1956
+ caller waits out the connect timeout for a generic error.
1957
+ """
1958
+ client = Mock()
1959
+ client.base_url = "https://api.example.com"
1960
+ builder = _SubcriptionBuilder(client=client)
1961
+ callback = Mock()
1962
+ builder.subscription._on_subscription_error = callback
1963
+ pusher = Mock()
1964
+ pusher.subscribe.side_effect = exceptions.TriggerSubscriptionAuthError(
1965
+ "auth failed"
1966
+ )
1967
+
1968
+ handler = builder._get_connection_handler(
1969
+ project_id="p", pusher=pusher, subscription=builder.subscription
1970
+ )
1971
+ handler("connection-payload") # must not raise
1972
+
1973
+ callback.assert_called_once_with({"error": "auth failed"})
1974
+ assert builder.subscription.is_alive() is False
1975
+
1976
1977
class TestPusherChannelAuth:
1978
"""Tests for the hardened pysher channel-auth request."""