@@ -1792,6 +1792,15 @@ def test_send_unknown_fcm_error_code(self, status):
1792
1792
assert json .loads (recorder [0 ].body .decode ()) == body
1793
1793
1794
1794
1795
+ class _HttpMockException :
1796
+
1797
+ def __init__ (self , exc ):
1798
+ self ._exc = exc
1799
+
1800
+ def request (self , url , ** kwargs ):
1801
+ raise self ._exc
1802
+
1803
+
1795
1804
class TestBatch :
1796
1805
1797
1806
@classmethod
@@ -1803,17 +1812,21 @@ def setup_class(cls):
1803
1812
def teardown_class (cls ):
1804
1813
testutils .cleanup_apps ()
1805
1814
1806
- def _instrument_batch_messaging_service (self , app = None , status = 200 , payload = '' ):
1815
+ def _instrument_batch_messaging_service (self , app = None , status = 200 , payload = '' , exc = None ):
1807
1816
if not app :
1808
1817
app = firebase_admin .get_app ()
1818
+
1809
1819
fcm_service = messaging ._get_messaging_service (app )
1810
- if status == 200 :
1811
- content_type = 'multipart/mixed; boundary=boundary'
1820
+ if exc :
1821
+ fcm_service . _transport = _HttpMockException ( exc )
1812
1822
else :
1813
- content_type = 'application/json'
1814
- fcm_service ._transport = http .HttpMockSequence ([
1815
- ({'status' : str (status ), 'content-type' : content_type }, payload ),
1816
- ])
1823
+ if status == 200 :
1824
+ content_type = 'multipart/mixed; boundary=boundary'
1825
+ else :
1826
+ content_type = 'application/json'
1827
+ fcm_service ._transport = http .HttpMockSequence ([
1828
+ ({'status' : str (status ), 'content-type' : content_type }, payload ),
1829
+ ])
1817
1830
return fcm_service
1818
1831
1819
1832
def _batch_payload (self , payloads ):
@@ -2027,6 +2040,19 @@ def test_send_all_batch_fcm_error_code(self, status):
2027
2040
messaging .send_all ([msg ])
2028
2041
check_exception (excinfo .value , 'test error' , status )
2029
2042
2043
+ def test_send_all_runtime_exception (self ):
2044
+ exc = BrokenPipeError ('Test error' )
2045
+ _ = self ._instrument_batch_messaging_service (exc = exc )
2046
+ msg = messaging .Message (topic = 'foo' )
2047
+
2048
+ with pytest .raises (exceptions .UnknownError ) as excinfo :
2049
+ messaging .send_all ([msg ])
2050
+
2051
+ expected = 'Unknown error while making a remote service call: Test error'
2052
+ assert str (excinfo .value ) == expected
2053
+ assert excinfo .value .cause is exc
2054
+ assert excinfo .value .http_response is None
2055
+
2030
2056
2031
2057
class TestSendMulticast (TestBatch ):
2032
2058
@@ -2204,6 +2230,19 @@ def test_send_multicast_batch_fcm_error_code(self, status):
2204
2230
messaging .send_multicast (msg )
2205
2231
check_exception (excinfo .value , 'test error' , status )
2206
2232
2233
+ def test_send_multicast_runtime_exception (self ):
2234
+ exc = BrokenPipeError ('Test error' )
2235
+ _ = self ._instrument_batch_messaging_service (exc = exc )
2236
+ msg = messaging .MulticastMessage (tokens = ['foo' ])
2237
+
2238
+ with pytest .raises (exceptions .UnknownError ) as excinfo :
2239
+ messaging .send_multicast (msg )
2240
+
2241
+ expected = 'Unknown error while making a remote service call: Test error'
2242
+ assert str (excinfo .value ) == expected
2243
+ assert excinfo .value .cause is exc
2244
+ assert excinfo .value .http_response is None
2245
+
2207
2246
2208
2247
class TestTopicManagement :
2209
2248
0 commit comments