- 
                Notifications
    
You must be signed in to change notification settings  - Fork 3.2k
 
fix AttributeException #11463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    fix AttributeException #11463
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b351dfd
              
                fix AttributeException
              
              
                xiangyan99 6e47281
              
                update type doc string
              
              
                xiangyan99 903668d
              
                fix type
              
              
                xiangyan99 bf0ea87
              
                add tests
              
              
                xiangyan99 1acb0cf
              
                update
              
              
                xiangyan99 a2e09ab
              
                update
              
              
                xiangyan99 ecd94f0
              
                update
              
              
                xiangyan99 de25b7a
              
                update
              
              
                xiangyan99 3f08ca1
              
                update
              
              
                xiangyan99 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            107 changes: 107 additions & 0 deletions
          
          107 
        
  sdk/core/azure-core/tests/azure_core_asynctests/test_stream_generator.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| from azure.core.pipeline.transport import ( | ||
| HttpRequest, | ||
| AsyncHttpResponse, | ||
| AsyncHttpTransport, | ||
| ) | ||
| from azure.core.pipeline import AsyncPipeline | ||
| from azure.core.pipeline.transport._aiohttp import AioHttpStreamDownloadGenerator | ||
| from unittest import mock | ||
| import pytest | ||
| 
     | 
||
| @pytest.mark.asyncio | ||
| async def test_connection_error_response(): | ||
| class MockTransport(AsyncHttpTransport): | ||
| def __init__(self): | ||
| self._count = 0 | ||
| 
     | 
||
| async def __aexit__(self, exc_type, exc_val, exc_tb): | ||
| pass | ||
| async def close(self): | ||
| pass | ||
| async def open(self): | ||
| pass | ||
| 
     | 
||
| async def send(self, request, **kwargs): | ||
| request = HttpRequest('GET', 'http://127.0.0.1/') | ||
| response = AsyncHttpResponse(request, None) | ||
| response.status_code = 200 | ||
| return response | ||
| 
     | 
||
| class MockContent(): | ||
| def __init__(self): | ||
| self._first = True | ||
| 
     | 
||
| async def read(self, block_size): | ||
| if self._first: | ||
| self._first = False | ||
| raise ConnectionError | ||
| return None | ||
| 
     | 
||
| class MockInternalResponse(): | ||
| def __init__(self): | ||
| self.headers = {} | ||
| self.content = MockContent() | ||
| 
     | 
||
| async def close(self): | ||
| pass | ||
| 
     | 
||
| class AsyncMock(mock.MagicMock): | ||
| async def __call__(self, *args, **kwargs): | ||
| return super(AsyncMock, self).__call__(*args, **kwargs) | ||
| 
     | 
||
| http_request = HttpRequest('GET', 'http://127.0.0.1/') | ||
| pipeline = AsyncPipeline(MockTransport()) | ||
| http_response = AsyncHttpResponse(http_request, None) | ||
| http_response.internal_response = MockInternalResponse() | ||
| stream = AioHttpStreamDownloadGenerator(pipeline, http_response) | ||
| with mock.patch('asyncio.sleep', new_callable=AsyncMock): | ||
| with pytest.raises(StopAsyncIteration): | ||
| await stream.__anext__() | ||
| 
     | 
||
| @pytest.mark.asyncio | ||
| async def test_connection_error_416(): | ||
| class MockTransport(AsyncHttpTransport): | ||
| def __init__(self): | ||
| self._count = 0 | ||
| 
     | 
||
| async def __aexit__(self, exc_type, exc_val, exc_tb): | ||
| pass | ||
| async def close(self): | ||
| pass | ||
| async def open(self): | ||
| pass | ||
| 
     | 
||
| async def send(self, request, **kwargs): | ||
| request = HttpRequest('GET', 'http://127.0.0.1/') | ||
| response = AsyncHttpResponse(request, None) | ||
| response.status_code = 416 | ||
| return response | ||
| 
     | 
||
| class MockContent(): | ||
| async def read(self, block_size): | ||
| raise ConnectionError | ||
| 
     | 
||
| class MockInternalResponse(): | ||
| def __init__(self): | ||
| self.headers = {} | ||
| self.content = MockContent() | ||
| 
     | 
||
| async def close(self): | ||
| pass | ||
| 
     | 
||
| class AsyncMock(mock.MagicMock): | ||
| async def __call__(self, *args, **kwargs): | ||
| return super(AsyncMock, self).__call__(*args, **kwargs) | ||
| 
     | 
||
| http_request = HttpRequest('GET', 'http://127.0.0.1/') | ||
| pipeline = AsyncPipeline(MockTransport()) | ||
| http_response = AsyncHttpResponse(http_request, None) | ||
| http_response.internal_response = MockInternalResponse() | ||
| stream = AioHttpStreamDownloadGenerator(pipeline, http_response) | ||
| with mock.patch('asyncio.sleep', new_callable=AsyncMock): | ||
| with pytest.raises(ConnectionError): | ||
| await stream.__anext__() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| import requests | ||
| from azure.core.pipeline.transport import ( | ||
| HttpRequest, | ||
| HttpResponse, | ||
| HttpTransport, | ||
| ) | ||
| from azure.core.pipeline import Pipeline, PipelineResponse | ||
| from azure.core.pipeline.transport._requests_basic import StreamDownloadGenerator | ||
| try: | ||
| from unittest import mock | ||
| except ImportError: | ||
| import mock | ||
| import pytest | ||
| 
     | 
||
| def test_connection_error_response(): | ||
| class MockTransport(HttpTransport): | ||
| def __init__(self): | ||
| self._count = 0 | ||
| 
     | 
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| pass | ||
| def close(self): | ||
| pass | ||
| def open(self): | ||
| pass | ||
| 
     | 
||
| def send(self, request, **kwargs): | ||
| request = HttpRequest('GET', 'http://127.0.0.1/') | ||
| response = HttpResponse(request, None) | ||
| response.status_code = 200 | ||
| return response | ||
| 
     | 
||
| def next(self): | ||
| self.__next__() | ||
| 
     | 
||
| def __next__(self): | ||
| if self._count == 0: | ||
| self._count += 1 | ||
| raise requests.exceptions.ConnectionError | ||
| 
     | 
||
| class MockInternalResponse(): | ||
| def iter_content(self, block_size): | ||
| return MockTransport() | ||
| 
     | 
||
| def close(self): | ||
| pass | ||
| 
     | 
||
| http_request = HttpRequest('GET', 'http://127.0.0.1/') | ||
| pipeline = Pipeline(MockTransport()) | ||
| http_response = HttpResponse(http_request, None) | ||
| http_response.internal_response = MockInternalResponse() | ||
| stream = StreamDownloadGenerator(pipeline, http_response) | ||
| with mock.patch('time.sleep', return_value=None): | ||
| with pytest.raises(StopIteration): | ||
| stream.__next__() | ||
| 
     | 
||
| def test_connection_error_416(): | ||
| class MockTransport(HttpTransport): | ||
| def __init__(self): | ||
| self._count = 0 | ||
| 
     | 
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| pass | ||
| def close(self): | ||
| pass | ||
| def open(self): | ||
| pass | ||
| 
     | 
||
| def send(self, request, **kwargs): | ||
| request = HttpRequest('GET', 'http://127.0.0.1/') | ||
| response = HttpResponse(request, None) | ||
| response.status_code = 416 | ||
| return response | ||
| 
     | 
||
| def next(self): | ||
| self.__next__() | ||
| 
     | 
||
| def __next__(self): | ||
| if self._count == 0: | ||
| self._count += 1 | ||
| raise requests.exceptions.ConnectionError | ||
| 
     | 
||
| class MockInternalResponse(): | ||
| def iter_content(self, block_size): | ||
| return MockTransport() | ||
| 
     | 
||
| def close(self): | ||
| pass | ||
| 
     | 
||
| http_request = HttpRequest('GET', 'http://127.0.0.1/') | ||
| pipeline = Pipeline(MockTransport()) | ||
| http_response = HttpResponse(http_request, None) | ||
| http_response.internal_response = MockInternalResponse() | ||
| stream = StreamDownloadGenerator(pipeline, http_response) | ||
| with mock.patch('time.sleep', return_value=None): | ||
| with pytest.raises(requests.exceptions.ConnectionError): | ||
| stream.__next__() | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are AsyncHttpTransport typing/docstring correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have typing/docstring for AsyncHttpTransport. So it is not incorrect. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be the opportunity to add them, but yeah optional :p