Example #1
0
    def test_retry_failed(self, mock_gcs):
        mock_client = mock_gcs.return_value
        mock_client.get_bucket = MagicMock()
        mock_bucket = mock_client.get_bucket.return_value
        mock_blob = mock_bucket.blob.return_value
        mock_blob.upload_from_file.side_effect = [
            ServiceUnavailable("service unavailable"),
            ServiceUnavailable("service unavailable"),
            ServiceUnavailable("service unavailable"),
            ServiceUnavailable("service unavailable"),
        ]

        test_handler = GCSHandler("test_bucket_name", back_off=0.1)
        with pytest.raises(ServiceUnavailable) as expected:
            test_handler.upload_file("test_file_obj", "test_file_path")
            assert expected.value.errors == "service unavailable"
Example #2
0
    def test_normal_flow(self, mock_gcs):
        mock_client = mock_gcs.return_value
        mock_client.get_bucket = MagicMock()
        mock_bucket = mock_client.get_bucket.return_value
        mock_blob = mock_bucket.blob.return_value
        mock_blob.upload_from_file.return_value = True

        test_handler = GCSHandler("test_bucket_name")
        result = test_handler.upload_file("test_file_obj", "test_file_path")
        assert result is True
        mock_bucket.blob.assert_called_once()
        mock_blob.upload_from_file.assert_called_once()
Example #3
0
    def test_retry_succeed(self, mock_gcs):
        mock_client = mock_gcs.return_value
        mock_client.get_bucket = MagicMock()
        mock_bucket = mock_client.get_bucket.return_value
        mock_blob = mock_bucket.blob.return_value
        mock_blob.upload_from_file.side_effect = [
            ServiceUnavailable("service unavailable"),
            True,
        ]

        test_handler = GCSHandler("test_bucket_name", back_off=0.1)
        result = test_handler.upload_file("test_file_obj", "test_file_path")
        assert result is True
        assert mock_bucket.blob.call_count == 2
        assert mock_blob.upload_from_file.call_count == 2