Search code examples
c#google-docs-api

Documents List API client for C# timing out


I'm working on a simple wrapper for the google docs api in c#. The problem I'm running into is my tests are timing out. Sometimes. When I run all of my tests (only 12 of them) then it usually hangs up on the 8th one, which is testing the delete function. After about 6.5 minutes, it continues on, but every test after it also times out after 6.5 minutes for each test. If I run the tests individually then it works fine every time.

Here is the first method that times out:

Updated to show exception handling

[TestMethod]
public void CanDeleteFile()
{
    var api = CreateApi();
    api.UploadFile("pic.jpg", "..\\..\\..\\pic.jpg", "image/jpeg");
    try
    {
        var files = api.GetDocuments();
        api.DeleteFile("pic.jpg");
        var lessFiles = api.GetDocuments();
        Assert.AreEqual(files.Count - 1, lessFiles.Count);
    }
    catch (Google.GData.Client.GDataRequestException ex)
    {
        using (StreamWriter writer = new StreamWriter("..\\..\\..\\errors.log", true))
        {
            string time = DateTime.Now.ToString();
            writer.WriteLine(time + ":\r\n\t" + ex.ResponseString);
        }
        throw ex;
    }

}

It times out on var lessFiles = api.GetDocuments(); The second call to that method. I have other methods that call that method twice, and they don't time out, but this one does.

The method that all the test methods use that times out:

    public AtomEntryCollection GetDocuments(DocumentType type = DocumentType.All, bool includeFolders = false)
    {
        checkToken();
        DocumentsListQuery query = getQueryByType(type);
        query.ShowFolders = includeFolders;
        DocumentsFeed feed = service.Query(query);
        return feed.Entries;
    }

It times out on this line DocumentsFeed feed = service.Query(query);. This would be closer to acceptable if I was requesting insane numbers of files. I'm not. I'm requesting 5 - 6 depending on what test I'm running.

Things I've tried:

  • Deleting all files from my google docs account, leaving only 1-2 files depending on the test for it to retrieve.
  • Running the tests individually (they all pass and nothing times out, but I shouldn't have to do this)
  • Checking my network speed to make sure it's not horribly slow (15mbps down 4.5mbps up)

I'm out of ideas. If anyone knows why it might start timing out on me? Any suggestions are welcome.

edit

As @gowansg suggested, I implemented exponential backoff in my code. It started failing at the same point with the same exception. I then wrote a test to send 10000 requests for a complete list of all documents in my drive. It passed without any issues without using exponential backoff. Next I modified my test class so it would keep track of how many requests were sent. My tests crash on request 11.

The complete exception:

Google.GData.Client.GDataRequestException was unhandled by user code
Message=Execution of request failed: https://docs.google.com/feeds/default/private/full
Source=GoogleDrive
StackTrace:
     at GoogleDrive.GoogleDriveApi.GetDocuments(DocumentType type, Boolean includeFolders) in C:\Users\nlong\Desktop\projects\GoogleDrive\GoogleDrive\GoogleDriveApi.cs:line 105
     at GoogleDrive.Test.GoogleDriveTests.CanDeleteFile() in C:\Users\nlong\Desktop\projects\GoogleDrive\GoogleDrive.Test\GoogleDriveTests.cs:line 77
InnerException: System.Net.WebException
     Message=The operation has timed out
     Source=System
     StackTrace:
          at System.Net.HttpWebRequest.GetResponse()
          at Google.GData.Client.GDataRequest.Execute()
     InnerException: 

another edit

It seems that I only crash after requesting the number of documents after the second upload. I'm not sure why that is, but I'm definitely going to look into my upload method.


Solution

  • If your tests pass when ran individually, but not when ran consecutively then you may be hitting a request rate limit. I noticed in your comment to JotaBe's answer you mentioned were getting request timeout exceptions. You should take a look at the http status code to figure out what to do. In the case of a 503 you should implement exceptional back off.

    Updated Suggestion

    Place a try-catch around the line that is throwing the exception and catch the Google.GData.Client.GDataRequestException. According to the source there are two properties that may be of value to you:

        /// <summary>
        /// this is the error message returned by the server
        /// </summary>
        public string ResponseString
        { ... }
    

    and

        //////////////////////////////////////////////////////////////////////
        /// <summary>Read only accessor for response</summary>
        //////////////////////////////////////////////////////////////////////
        public WebResponse Response
        { ... }
    

    Hopefully these contain some useful information for you, e.g., an HTTP Status Code.