title: Mock boto3 calls for unit testing
body: <p>We have a class which looks like this:</p>
  
  <pre><code>import boto3
  import json
  import time
  import sys
  
  def stop_instances_python(instanceId):
      ec2_client = boto3.client('ec2', region_name='us-east-1')
      while True:
          try:
              ec2_client.stop_instances(InstanceIds=[instanceId])
              print "EC2 instance: " + instanceId + " stopped"
              break
          except Exception, e:
              if ('RequestLimitExceeded' in str(e)):
                  print "Request throttled. Sleeping for 15s"
                  time.sleep(15)
                  res = ec2_client.describe_instances(InstanceIds=[instanceId])
                  print "instance state: " + res['Reservations'][0]['Instances'][0]['State']['Name']
                  try:
                      ec2_client.stop_instances(InstanceIds=[instanceId])
                      print "EC2 instance: " + instanceId + " stopped" ## Unable to test this
                  except Exception, e:
                      if ('RequestLimitExceeded' in str(e)):
                          print "Request throttled again. Sleeping for 10s"
                          time.sleep(10)
                      else:
                          print str(e)
                          sys.exit(1)
              else:
                  print str(e)
                  sys.exit(1)
  </code></pre>
  
  <p>We want to write unit test cases for this class. We used <code>moto</code> and <code>mock</code> to test most of the lines. We are unable to test the line with comment: <code># Unable to test this</code>. This is because every time stop request is sent, it is getting throttled. Our test case file looks as below:</p>
  
  <pre><code>from moto import mock_ec2
  from moto import mock_sts
  from mock import patch
  import botocore
  import boto3
  import mock
  from botocore.exceptions import ClientError
  
  orig = botocore.client.BaseClient._make_api_call
  
  def mock_throttling(self, operation_name, kwarg):
      if operation_name == 'StopInstances':
          parsed_response = {
              'Error': {'Code': '500', 'Message': 'RequestLimitExceeded'}}
          raise ClientError(parsed_response, operation_name)
      return orig(self, operation_name, kwarg)
  
  def mock_throttling_side_effect(self, operation_name, kwarg):
      mock_api = mock.Mock()
      parsed_response = {'Error': {'Code': '500', 'Message': 'RequestLimitExceeded'}}
      if operation_name == 'StopInstances':
          mock_api.side_effect = [
              ClientError(parsed_response, operation_name),
              orig(self, operation_name, kwarg)
          ]
          mock_api()       
      return orig(self, operation_name, kwarg)
  
  class TestEC2StopInstances:
      @mock_ec2
      @mock_sts
      @patch('time.sleep', return_value=None)
      def test_ec2_lambda_handler(self, patched_time_sleep):
          from StopInstancesPython import stop_instances_python
          client = boto3.client('ec2', region_name='us-east-1')
          response = client.run_instances(ImageId='ami-1234567', MinCount=1, MaxCount=1)
          instanceId = response['Instances'][0]['InstanceId']
          with patch(
                  'botocore.client.BaseClient._make_api_call',
                  new=mock_throttling_side_effect):
              stop_instances_python(instanceId)
  </code></pre>
  
  <p>We tried using both <code>mock_throttling</code> and <code>mock_throttling_side_effect</code>. When we use <code>mock_throttling</code>, the <code>StopInstances</code> API always throws RequestLimitExceeded exception (which is the expected behavior).
  When we use <code>mock_throttling_side_effect</code>, the instance gets stopped in the first <code>StopInstances</code> API call and also throws the RequestLimitExceeded exception and continues in the infinite loop.</p>
  
  <p>Could someone guide us on how to test all lines of this code?</p>