66import gzip
77import os
88import json
9+ from unittest import mock
910import uuid
1011
1112import pytest
@@ -44,7 +45,17 @@ async def test_send_logs_async(self, recorded_test, monitor_info):
4445 LogsIngestionClient , credential , endpoint = monitor_info ['dce' ])
4546 async with client :
4647 await client .upload (rule_id = monitor_info ['dcr_id' ], stream_name = monitor_info ['stream_name' ], logs = LOGS_BODY )
47- credential .close ()
48+ await credential .close ()
49+
50+ @pytest .mark .asyncio
51+ async def test_send_logs_large (self , recorded_test , monitor_info , large_data ):
52+ credential = self .get_credential (LogsIngestionClient , is_async = True )
53+ client = self .create_client_from_credential (
54+ LogsIngestionClient , credential , endpoint = monitor_info ['dce' ])
55+ async with client :
56+ await client .upload (
57+ rule_id = monitor_info ['dcr_id' ], stream_name = monitor_info ['stream_name' ], logs = large_data )
58+ await credential .close ()
4859
4960 @pytest .mark .asyncio
5061 async def test_send_logs_error (self , recorded_test , monitor_info ):
@@ -56,7 +67,7 @@ async def test_send_logs_error(self, recorded_test, monitor_info):
5667 with pytest .raises (HttpResponseError ) as ex :
5768 async with client :
5869 await client .upload (rule_id = 'bad-rule' , stream_name = monitor_info ['stream_name' ], logs = body )
59- credential .close ()
70+ await credential .close ()
6071
6172 @pytest .mark .asyncio
6273 async def test_send_logs_error_custom (self , recorded_test , monitor_info ):
@@ -76,7 +87,7 @@ async def on_error(e):
7687 await client .upload (
7788 rule_id = 'bad-rule' , stream_name = monitor_info ['stream_name' ], logs = body , on_error = on_error )
7889 assert on_error .called
79- credential .close ()
90+ await credential .close ()
8091
8192 @pytest .mark .asyncio
8293 async def test_send_logs_json_file (self , recorded_test , monitor_info ):
@@ -92,7 +103,7 @@ async def test_send_logs_json_file(self, recorded_test, monitor_info):
92103 with open (temp_file , 'r' ) as f :
93104 await client .upload (rule_id = monitor_info ['dcr_id' ], stream_name = monitor_info ['stream_name' ], logs = f )
94105 os .remove (temp_file )
95- credential .close ()
106+ await credential .close ()
96107
97108 @pytest .mark .asyncio
98109 @pytest .mark .live_test_only ("Issues recording binary streams with test-proxy" )
@@ -109,4 +120,49 @@ async def test_send_logs_gzip_file(self, monitor_info):
109120 with open (temp_file , 'rb' ) as f :
110121 await client .upload (rule_id = monitor_info ['dcr_id' ], stream_name = monitor_info ['stream_name' ], logs = f )
111122 os .remove (temp_file )
112- credential .close ()
123+ await credential .close ()
124+
125+
126+ @pytest .mark .asyncio
127+ async def test_abort_error_handler (self , monitor_info ):
128+ credential = self .get_credential (LogsIngestionClient , is_async = True )
129+ client = self .create_client_from_credential (
130+ LogsIngestionClient , credential , endpoint = monitor_info ['dce' ])
131+ body = [{"foo" : "bar" }]
132+
133+ class TestException (Exception ):
134+ pass
135+
136+ async def on_error (e ):
137+ on_error .called = True
138+ if isinstance (e .error , RuntimeError ):
139+ raise TestException ("Abort" )
140+ return
141+
142+ on_error .called = False
143+
144+ async with client :
145+ # No exception should be raised
146+ with mock .patch ("azure.monitor.ingestion.aio._operations._patch.GeneratedOps.upload" ,
147+ side_effect = ConnectionError ):
148+ await client .upload (
149+ rule_id = monitor_info ['dcr_id' ],
150+ stream_name = monitor_info ['stream_name' ],
151+ logs = LOGS_BODY ,
152+ on_error = on_error )
153+
154+ assert on_error .called
155+
156+ on_error .called = False
157+ # Exception should now be raised since error handler checked for RuntimeError.
158+ with mock .patch ("azure.monitor.ingestion.aio._operations._patch.GeneratedOps.upload" ,
159+ side_effect = RuntimeError ):
160+ with pytest .raises (TestException ):
161+ await client .upload (
162+ rule_id = monitor_info ['dcr_id' ],
163+ stream_name = monitor_info ['stream_name' ],
164+ logs = LOGS_BODY ,
165+ on_error = on_error )
166+
167+ assert on_error .called
168+ await credential .close ()
0 commit comments