def test_populate_queue(self, mock_queue, mock_session, mock_executor): mock_session.return_value = None mock_queue.return_value = mock_queue mock_page = {'NextPageToken': None, 'Values': []} mock_session._fetch_page.return_value = {'Page': mock_page} mock_queue.full.return_value = False read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = mock_queue read_ahead_cursor._populate_queue() mock_session._fetch_page.assert_called_once_with( MOCK_TRANSACTION_ID, MOCK_STATEMENT_RESULT.get('NextPageToken')) mock_queue.put.assert_called_once_with(mock_page, timeout=0.05)
def test_populate_queue_client_error(self, mock_session, mock_logger_debug, mock_executor): mock_logger_debug.return_value = None mock_session.return_value = None mock_session._fetch_page.side_effect = ClientError( MOCK_CLIENT_ERROR_MESSAGE, MOCK_MESSAGE) read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = Queue(1) read_ahead_cursor._queue.put('value to be removed') read_ahead_cursor._populate_queue() mock_logger_debug.assert_called_once() self.assertIsInstance(read_ahead_cursor._queue.get(), ClientError) self.assertEqual(read_ahead_cursor._queue.qsize(), 0)
def test_populate_queue(self, mock_queue, mock_session, mock_executor): mock_session.return_value = None mock_queue.return_value = mock_queue mock_session._fetch_page.return_value = MOCK_FETCH_PAGE_RESULT_WITHOUT_TOKEN mock_queue.full.return_value = False read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = mock_queue read_ahead_cursor._populate_queue() next_page_token = MOCK_STATEMENT_RESULT.get('FirstPage').get( 'NextPageToken') mock_session._fetch_page.assert_called_once_with( MOCK_TRANSACTION_ID, next_page_token) mock_queue.put.assert_called_once_with( MOCK_FETCH_PAGE_RESULT_WITHOUT_TOKEN, timeout=0.05)
def test_read_ahead_queue_with_query_stats(self, mock_session, mock_executor): mock_statement_result_1 = generate_statement_result( 1, 2, 3, MOCK_TOKEN, True) mock_statement_result_2 = generate_statement_result( 1, 2, 3, MOCK_TOKEN, False) mock_statement_result_3 = generate_statement_result( 1, 2, 3, None, False) def fetch_page(txn_id, token): statement_results = [ mock_statement_result_2, mock_statement_result_3 ] statement_result = statement_results[fetch_page.page_num] fetch_page.page_num += 1 return statement_result fetch_page.page_num = 0 mock_session.return_value = None mock_session._fetch_page.side_effect = fetch_page mock_read_ahead = 3 read_ahead_cursor = ReadAheadCursor(mock_statement_result_1, mock_session, MOCK_TRANSACTION_ID, mock_read_ahead, mock_executor) read_ahead_cursor._value_holder_to_ion_value = MagicMock( name='_value_holder_to_ion_value') read_ahead_cursor._populate_queue() # Queue should be populated with the next two pages self.assertEqual(read_ahead_cursor._queue.qsize(), mock_read_ahead - 1) # Even if queue is populated with the next two pages, query stats should only total the first page here assert_query_stats(self, read_ahead_cursor, 1, 2, 3) read_ahead_cursor._next_page() # Query stats should only total the first page and second page here assert_query_stats(self, read_ahead_cursor, 2, 4, 6) read_ahead_cursor._next_page() # Query stats should total all three pages assert_query_stats(self, read_ahead_cursor, 3, 6, 9)
def test_populate_queue_result_closed_error(self, mock_session, mock_logger_debug, mock_executor): def close_parent_txn(txn_id, token): read_ahead_cursor._is_open = False return MOCK_STATEMENT_RESULT mock_logger_debug.return_value = None mock_session.return_value = None mock_session._fetch_page.side_effect = close_parent_txn read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = Queue(1) read_ahead_cursor._queue.put('value to be removed') read_ahead_cursor._populate_queue() self.assertEqual(mock_logger_debug.call_count, 2) self.assertIsInstance(read_ahead_cursor._queue.get(), ResultClosedError) self.assertEqual(read_ahead_cursor._queue.qsize(), 0)