<s> import asyncio <EOL> import unittest <EOL> import unittest . mock <EOL> from aiohttp import streams <EOL> class TestFlowControlStreamReader ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . stream = unittest . mock . Mock ( ) <EOL> self . transp = self . stream . transport <EOL> self . loop = asyncio . new_event_loop ( ) <EOL> asyncio . set_event_loop ( None ) <EOL> def tearDown ( self ) : <EOL> self . loop . close ( ) <EOL> def _make_one ( self , * args , ** kwargs ) : <EOL> return streams . FlowControlStreamReader ( <EOL> self . stream , limit = <NUM_LIT:1> , loop = self . loop , * args , ** kwargs ) <EOL> def test_read ( self ) : <EOL> r = self . _make_one ( ) <EOL> r . paused = True <EOL> r . feed_data ( b'<STR_LIT>' , <NUM_LIT:2> ) <EOL> res = self . loop . run_until_complete ( r . read ( <NUM_LIT:1> ) ) <EOL> self . assertEqual ( res , b'<STR_LIT:d>' ) <EOL> self . assertTrue ( self . transp . resume_reading . called ) <EOL> def test_readline ( self ) : <EOL> r = self . _make_one ( ) <EOL> r . paused = True <EOL> r . feed_data ( b'<STR_LIT>' , <NUM_LIT:5> ) <EOL> res = self . loop . run_until_complete ( r . readline ( ) ) <EOL> self . assertEqual ( res , b'<STR_LIT>' ) <EOL> self . assertTrue ( self . transp . resume_reading . called ) <EOL> def test_readany ( self ) : <EOL> r = self . _make_one ( ) <EOL> r . paused = True <EOL> r . feed_data ( b'<STR_LIT:data>' , <NUM_LIT:4> ) <EOL> res = self . loop . run_until_complete ( r . readany ( ) ) <EOL> self . assertEqual ( res , b'<STR_LIT:data>' ) <EOL> self . assertTrue ( self . transp . resume_reading . called ) <EOL> def test_readexactly ( self ) : <EOL> r = self . _make_one ( ) <EOL> r . paused = True <EOL> r . feed_data ( b'<STR_LIT>' , <NUM_LIT:8> ) <EOL> res = self . loop . run_until_complete ( r . readexactly ( <NUM_LIT:2> ) ) <EOL> self . assertEqual ( res , b'<STR_LIT>' ) <EOL> self . assertTrue ( self . transp . resume_reading . called ) <EOL> def test_feed_data ( self ) : <EOL> r = self . _make_one ( ) <EOL> r . _stream . paused = False <EOL> r . feed_data ( b'<STR_LIT>' , <NUM_LIT:8> ) <EOL> self . assertTrue ( self . transp . pause_reading . called ) <EOL> class FlowControlMixin : <EOL> def test_resume_on_init ( self ) : <EOL> stream = unittest . mock . Mock ( ) <EOL> stream . paused = True <EOL> streams . FlowControlDataQueue ( stream , limit = <NUM_LIT:1> , loop = self . loop ) <EOL> self . assertTrue ( stream . transport . resume_reading . called ) <EOL> self . assertFalse ( stream . paused ) <EOL> def test_no_transport_in_init ( self ) : <EOL> stream = unittest . mock . Mock ( ) <EOL> stream . paused = True <EOL> stream . transport = None <EOL> streams . FlowControlDataQueue ( stream , limit = <NUM_LIT:1> , loop = self . loop ) <EOL> self . assertTrue ( stream . paused ) <EOL> def test_feed_no_waiter ( self ) : <EOL> out = self . _make_one ( ) <EOL> out . feed_data ( object ( ) , <NUM_LIT:100> ) <EOL> self . assertTrue ( self . stream . transport . pause_reading . called ) <EOL> def test_feed_no_transport ( self ) : <EOL> self . stream . transport = None <EOL> out = self . _make_one ( ) <EOL> self . stream . paused = False <EOL> out . feed_data ( object ( ) , <NUM_LIT:100> ) <EOL> self . assertFalse ( self . stream . paused ) <EOL> def test_feed_with_waiter ( self ) : <EOL> self . stream . paused = False <EOL> out = self . _make_one ( ) <EOL> read_task = asyncio . Task ( out . read ( ) , loop = self . loop ) <EOL> def cb ( ) : <EOL> out . feed_data ( object ( ) , <NUM_LIT:100> ) <EOL> self . loop . call_soon ( cb ) <EOL> self . loop . run_until_complete ( read_task ) <EOL> self . assertFalse ( self . stream . transport . pause_reading . called ) <EOL> self . assertFalse ( self . stream . paused ) <EOL> def test_resume_on_read ( self ) : <EOL> out = self . _make_one ( ) <EOL> out . feed_data ( object ( ) , <NUM_LIT:100> ) <EOL> self . assertTrue ( self . stream . paused ) <EOL> self . loop . run_until_complete ( out . read ( ) ) <EOL> self . assertTrue ( self . stream . transport . resume_reading . called ) <EOL> self . assertFalse ( self . stream . paused ) <EOL> def test_resume_on_read_no_transport ( self ) : <EOL> item = object ( ) <EOL> out = self . _make_one ( ) <EOL> out . feed_data ( item , <NUM_LIT:100> ) <EOL> self . assertTrue ( self . stream . paused ) <EOL> self . stream . transport = None <EOL> res = self . loop . run_until_complete ( out . read ( ) ) <EOL> self . assertIs ( res , item ) <EOL> self . assertTrue ( self . stream . paused ) <EOL> def test_no_resume_on_read ( self ) : <EOL> out = self . _make_one ( ) <EOL> out . feed_data ( object ( ) , <NUM_LIT:100> ) <EOL> out . feed_data ( object ( ) , <NUM_LIT:100> ) <EOL> out . feed_data ( object ( ) , <NUM_LIT:100> ) <EOL> self . assertTrue ( self . stream . paused ) <EOL> self . stream . transport . reset_mock ( ) <EOL> self . loop . run_until_complete ( out . read ( ) ) <EOL> self . assertFalse ( self . stream . transport . resume_reading . called ) <EOL> self . assertTrue ( self . stream . paused ) <EOL> def test_pause_on_read ( self ) : <EOL> out = self . _make_one ( ) <EOL> out . _buffer . append ( ( object ( ) , <NUM_LIT:100> ) ) <EOL> out . _buffer . append ( ( object ( ) , <NUM_LIT:100> ) ) <EOL> out . _buffer . append ( ( object ( ) , <NUM_LIT:100> ) ) <EOL> out . _size = <NUM_LIT> <EOL> self . stream . paused = False <EOL> self . loop . run_until_complete ( out . read ( ) ) <EOL> self . assertTrue ( self . stream . transport . pause_reading . called ) <EOL> self . assertTrue ( self . stream . paused ) <EOL> def test_no_pause_on_read ( self ) : <EOL> item = object ( ) <EOL> out = self . _make_one ( ) <EOL> out . _buffer . append ( ( item , <NUM_LIT:100> ) ) <EOL> out . _size = <NUM_LIT:100> <EOL> self . stream . paused = False <EOL> res = self . loop . run_until_complete ( out . read ( ) ) <EOL> self . assertIs ( res , item ) <EOL> self . assertFalse ( self . stream . transport . pause_reading . called ) <EOL> self . assertFalse ( self . stream . paused ) <EOL> def test_no_pause_on_read_no_transport ( self ) : <EOL> item = object ( ) <EOL> out = self . _make_one ( ) <EOL> out . _buffer . append ( ( item , <NUM_LIT:100> ) ) <EOL> out . _buffer . append ( ( object ( ) , <NUM_LIT:100> ) ) <EOL> out . _buffer . append ( ( object ( ) , <NUM_LIT:100> ) ) <EOL> out . _size = <NUM_LIT> <EOL> self . stream . paused = False <EOL> self . stream . transport = None <EOL> res = self . loop . run_until_complete ( out . read ( ) ) <EOL> self . assertIs ( res , item ) <EOL> self . assertFalse ( self . stream . paused ) <EOL> class TestFlowControlDataQueue ( unittest . TestCase , FlowControlMixin ) : <EOL> def setUp ( self ) : <EOL> self . stream = unittest . mock . Mock ( ) <EOL> self . loop = asyncio . new_event_loop ( ) <EOL> asyncio . set_event_loop ( None ) <EOL> def tearDown ( self ) : <EOL> self . loop . close ( ) <EOL> def _make_one ( self , * args , ** kwargs ) : <EOL> return streams . FlowControlDataQueue ( <EOL> self . stream , limit = <NUM_LIT:1> , loop = self . loop , * args , ** kwargs ) <EOL> class TestFlowControlChunksQueue ( unittest . TestCase , FlowControlMixin ) : <EOL> def setUp ( self ) : <EOL> self . stream = unittest . mock . Mock ( ) <EOL> self . loop = asyncio . new_event_loop ( ) <EOL> asyncio . set_event_loop ( None ) <EOL> def tearDown ( self ) : <EOL> self . loop . close ( ) <EOL> def _make_one ( self , * args , ** kwargs ) : <EOL> return streams . FlowControlChunksQueue ( <EOL> self . stream , limit = <NUM_LIT:1> , loop = self . loop , * args , ** kwargs ) <EOL> def test_read_eof ( self ) : <EOL> out = self . _make_one ( ) <EOL> read_task = asyncio . Task ( out . read ( ) , loop = self . loop ) <EOL> def cb ( ) : <EOL> out . feed_eof ( ) <EOL> self . loop . call_soon ( cb ) <EOL> self . loop . run_until_complete ( read_task ) <EOL> self . assertTrue ( out . at_eof ( ) ) <EOL> def test_read_until_eof ( self ) : <EOL> item = object ( ) <EOL> out = self . _make_one ( ) <EOL> out . feed_data ( item , <NUM_LIT:1> ) <EOL> out . feed_eof ( ) <EOL> data = self . loop . run_until_complete ( out . read ( ) ) <EOL> self . assertIs ( data , item ) <EOL> thing = self . loop . run_until_complete ( out . read ( ) ) <EOL> self . assertEqual ( thing , b'<STR_LIT>' ) <EOL> self . assertTrue ( out . at_eof ( ) ) <EOL> def test_readany ( self ) : <EOL> out = self . _make_one ( ) <EOL> self . assertIs ( out . read . __func__ , out . readany . __func__ ) </s>
